Events

Overview

One callback receives everything the engine does, as it does it: the text as it streams, each tool before and after, each permission asked and answered, each turn's cost and context.

onEventnever blocks the loopchildren carry parentId
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

await agent.run({
  inputs: "Read the notes and list what is unfinished.",
  onEvent: (event) => {
    switch (event.type) {
      case "text_delta": process.stdout.write(event.delta); break;
      case "tool_start": console.log("→", event.tool, event.args); break;
      case "tool_end":   console.log("←", event.tool, event.ok ? "ok" : "failed", event.summary); break;
      case "turn":       console.log(`${event.totalCredits} credits · ${event.contextTokens} tokens`); break;
      case "done":       console.log(event.stopReason); break;
    }
  },
});
import sys

def on_event(event):
    kind = event["type"]
    if kind == "text":
        sys.stdout.write(event["text"])
    elif kind == "tool_start":
        print("→", event["tool"], event["args"])
    elif kind == "tool_end":
        print("←", event["tool"], "ok" if event["ok"] else "failed", event["summary"])
        print(f"{agent.usage['credits']} credits · {agent.context['tokens']} tokens")
    elif kind == "done":
        print(event["stopReason"])

agent.run(inputs="Read the notes and list what is unfinished.", on_event=on_event)

Every event

TypeWhenPage
commandA /skill at the start of the message was expanded—
mentionAn @path or #storage was attached—
checkpointThe workspace was photographed before your messagecheckpoint
checkpoint_partialThat snapshot could not take everythingcheckpoint_partial
rewoundA run({ edit }) went back firstrewound
compactedThe older conversation became a summary—
retryA turn failed for a passing reason and is tried again—
fallbackA model gave up and the next one is used—
text_deltaA piece of the answer, as it streamstext
turnA model turn finishedturn
textThe complete text of a turntext
permissionSomething is being askedpermission
permission_resultYou answeredpermission
tool_startA tool is about to runtool_start
tool_endIt finished, failed or was refusedtool_end
todoThe plan changedtodo
questionThe agent is asking you somethingquestion
question_resultYou answered itquestion
errorThe run is about to failerror
doneThe run endeddone

The order in a step

checkpoint                 (once, before your message is taken)
  turn                     one per step
    text_delta …           while the answer streams
    text                   the complete text, if there was any
    permission             only if something must be asked
    permission_result
    tool_start
    tool_end
    todo                   if the plan changed
  turn                     next step…
done

Your callback cannot break the run

Anything it throws is swallowed

A failing interface must not stop an agent mid-task, so an exception from onEvent is caught and ignored. The consequence is yours to know: an error in your handler leaves no trace. Log inside it if you need one.

It is awaited nowhere either — returning a promise does not make the loop wait. Do slow work outside the callback.

A subagent's events arrive here too

They carry parentId — the identifier of the task call that started them — and agent, its name. Their own id is prefixed with the task's, so two runs never collide. Test parentId before treating an event as the main run's.

Two ways to get the text

text_delta streams it as it is produced; text arrives once with the whole of that turn. Use the first for a live display and the second as the record — and do not append both, or every answer appears twice.