Why Stream?
- Immediate feedback: users see each step rather than a spinner
- Tool visibility: show what the agent is doing and why it is taking a while
- Cancellation: stop early once you have what you need
stream_chat() streams steps, not tokens: whole messages, tool calls, and tool results as each one happens. For token-by-token text, see token-level streaming below.Quick Start
Enable streaming with thestream_chat method:
Stream Event Types
Agentor emits structured events during streaming:AgentOutput carries:
str
Always
"run_item_stream_event".str | None
Text: a tool’s return value on a
tool_output event, the agent’s answer on the final one.ToolAction | None
name and type for a tool call (tool_called) or its result (tool_output).None
Reserved.
stream_chat() never sets it — see token-level streaming.None
Reserved. Not populated today.
JSON Serialization
Get events as JSON strings for easy transmission:HTTP Streaming
Serve streaming responses over HTTP:Built-in Server Streaming
Agentor’s built-in server supports streaming out of the box:A2A Protocol Streaming
Stream responses using the A2A protocol:Advanced Streaming Patterns
Token-level streaming
stream_chat() gives you steps. For text as the model produces it, use AgentLoop — the engine underneath Agentor — and ask for stream_text=True:
astream emits the engine’s raw events: run_start, generation, text_delta, tool_call, tool_result, message, and run_end. The same events power tracing and durable runs.
Filter to just the answer
Progress Tracking
Count the steps as they go by:Buffered Streaming
Batch tokens before writing them, to cut down on network round trips. This needstext_delta events, so it uses AgentLoop:
Multi-Agent Streaming
Stream from multiple agents concurrently:WebSocket Streaming
For bidirectional streaming, use WebSockets:Error Handling
Handle streaming errors gracefully:Best Practices
Performance Tips
- Use
serialize=True(default) when sending over network - Use
serialize=Falsefor local processing to avoid JSON overhead - Buffer small chunks for better network efficiency
- Set appropriate timeouts based on expected response time
- Close streams properly to free resources
Next Steps
- Serve streaming agents behind your own infrastructure
- Enable observability to monitor stream performance
- Learn about agent communication with streaming A2A
