Skip to content

Streaming Events

A session produces a sequence of typed events. Every event has a type discriminator field.

// Specific event type
const unsub = session.on("token", (event) => {
process.stdout.write(event.content);
});
// All events
session.onAny((event) => console.log(event.type, event));
// Manual control
session.startStreaming();
session.stopStreaming();
// Async iterator (auto-connects, auto-disconnects)
for await (const event of session.stream()) {
// ...
}
TypeDescriptionKey fields
tokenStreaming tokencontent, accumulated
messageComplete messagerole, content, timestamp
tool_callTool invocationtoolName, arguments, callId
tool_resultTool execution resulttoolName, callId, success, output
human_input_neededAgent needs human inputinputId, question, category, choices
status_changeSession status changedpreviousStatus, newStatus
cost_updateToken/cost usage updatetotalCost, totalTokens
subagent_spawnedA subagent was createdsubagentId, scope, specialization
subagent_completeA subagent finishedsubagentId, status, report
completeSession completedreport, cost
errorAn error occurredmessage, code

When an agent parallelizes work, you’ll see subagent_spawned and subagent_complete events alongside the main stream. Use subagentId to group a subagent’s tokens and tool calls in your UI.

  • REST API — the underlying HTTP surface.