Runner.run_stream(). Instead of waiting for the full response, you receive events as they happen: incremental text, tool lifecycle updates, and terminal status.
Quick example
Event reference
run_stream() emits AgentStreamEvent values with these types:
| Event type | When it fires | Key fields |
|---|---|---|
text_delta | Incremental text chunks from streaming or fallback path | event.text_delta, event.step |
step_started | New step in the agent loop | event.step, event.state |
tool_started | A tool call is about to execute | event.tool_name, event.tool_call_id, event.step |
tool_completed | A tool call finished | event.tool_name, event.tool_call_id, event.tool_success, event.tool_output, event.tool_error |
tool_deferred | A tool call was accepted but deferred | event.tool_name, event.tool_call_id, event.tool_ticket_id, event.data.resume_hint |
tool_background_resolved | A deferred tool finished successfully | event.tool_name, event.tool_ticket_id, event.tool_output |
tool_background_failed | A deferred tool failed/expired | event.tool_name, event.tool_ticket_id, event.tool_error |
completed | Run finished | event.result (full AgentResult) |
error | Stream/runtime bridge error | event.error |
Streaming modes
- If the provider supports streaming, AFK forwards model deltas as
text_delta. - If the provider is non-streaming, AFK emits chunked fallback
text_deltafrom final text so UI behavior stays consistent.
Stream control
AgentStreamHandle is read-only. For lifecycle controls (pause(), resume(), cancel(), interrupt()), use run_handle().
Background tools example
Error handling
error events indicate stream/runtime bridge failures. Tool failures are reported through tool_completed (tool_success=False) and do not necessarily fail the run.