Streaming Events
A session produces a sequence of typed events. Every event has a type
discriminator field.
Listening (planned)
Section titled “Listening (planned)”// Specific event typeconst unsub = session.on("token", (event) => { process.stdout.write(event.content);});
// All eventssession.onAny((event) => console.log(event.type, event));
// Manual controlsession.startStreaming();session.stopStreaming();
// Async iterator (auto-connects, auto-disconnects)for await (const event of session.stream()) { // ...}Event types
Section titled “Event types”| Type | Description | Key fields |
|---|---|---|
token | Streaming token | content, accumulated |
message | Complete message | role, content, timestamp |
tool_call | Tool invocation | toolName, arguments, callId |
tool_result | Tool execution result | toolName, callId, success, output |
human_input_needed | Agent needs human input | inputId, question, category, choices |
status_change | Session status changed | previousStatus, newStatus |
cost_update | Token/cost usage update | totalCost, totalTokens |
subagent_spawned | A subagent was created | subagentId, scope, specialization |
subagent_complete | A subagent finished | subagentId, status, report |
complete | Session completed | report, cost |
error | An error occurred | message, code |
Handling subagents
Section titled “Handling subagents”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.
Next steps
Section titled “Next steps”- REST API — the underlying HTTP surface.