Skip to main content
A long agent run looks broken until it finishes. Streaming lets you show progress instead: which tool the agent just called, what it came back with, and the answer as soon as it exists.

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 the stream_chat method:

Stream Event Types

Agentor emits structured events during streaming:
Output for the run above:
Each 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:
Example JSON event:

HTTP Streaming

Serve streaming responses over HTTP:

Built-in Server Streaming

Agentor’s built-in server supports streaming out of the box:
Client request with streaming:

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.
Drain the generator. If you break out of an astream loop early, the run never finishes — its trace is not exported, and its saved log has no terminal event.

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 needs text_delta events, so it uses AgentLoop:

Multi-Agent Streaming

Stream from multiple agents concurrently:

WebSocket Streaming

For bidirectional streaming, use WebSockets:
WebSocket client:

Error Handling

Handle streaming errors gracefully:

Best Practices

Performance Tips

  • Use serialize=True (default) when sending over network
  • Use serialize=False for 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

Last modified on August 28, 2026