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
| Type | When | Page |
|---|---|---|
command | A /skill at the start of the message was expanded | — |
mention | An @path or #storage was attached | — |
checkpoint | The workspace was photographed before your message | checkpoint |
checkpoint_partial | That snapshot could not take everything | checkpoint_partial |
rewound | A run({ edit }) went back first | rewound |
compacted | The older conversation became a summary | — |
retry | A turn failed for a passing reason and is tried again | — |
fallback | A model gave up and the next one is used | — |
text_delta | A piece of the answer, as it streams | text |
turn | A model turn finished | turn |
text | The complete text of a turn | text |
permission | Something is being asked | permission |
permission_result | You answered | permission |
tool_start | A tool is about to run | tool_start |
tool_end | It finished, failed or was refused | tool_end |
todo | The plan changed | todo |
question | The agent is asking you something | question |
question_result | You answered it | question |
error | The run is about to fail | error |
done | The run ended | done |
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…
doneYour callback cannot break the run
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.
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.