Skip to main content
Streaming provides real-time feedback as agents execute, allowing you to display progress to users and handle responses incrementally.

Basic streaming

Use agent.stream() to stream agent responses:

Stream iterators

Polos provides multiple ways to consume streamed responses:

Text chunks

Stream only the text content:
Use case: Display text to users in real-time, like ChatGPT’s streaming interface.

All events

Access full event stream including tool calls, steps, and metadata:
Use case: Build rich UIs that show tool execution progress, reasoning steps, etc.

Final text

Get the complete accumulated text after streaming finishes:
Use case: When you need the complete response but want streaming for progress indication.

Complete result

Get the full result object with usage stats, tool calls, and metadata:
Use case: Analytics, logging, cost tracking, debugging.

Lifecycle events

By default, agents emit lifecycle events during execution - regardless of whether you use agent.run() or agent.stream(). These events help you track progress and display status to users.

Event types

agent_start
  • Marks the beginning of agent execution
  • Contains agent ID and initial configuration
agent_finish
  • Marks the end of execution
  • Includes usage statistics (tokens, cost)
  • Contains final result
step_start
  • Indicates a workflow step has begun - for example, LLM or tool call
step_finish
  • Confirms step completion
  • Includes step output and duration
text_delta (only with agent.stream())
  • Incremental text chunks as the LLM generates them
  • Real-time content streaming
tool_call (only with agent.stream())
  • Emitted when the agent asks for a tool execution
  • Includes tool name and arguments
  • The results of the tool execution are available via step_finish event when the tool finishes execution

Using lifecycle events

Example output:

Stream with agent.run()

Even when using agent.run() (non-streaming), lifecycle events are still emitted. You can listen to them separately:

Building UIs with streaming

Basic text streaming UI

Rich progress UI

Error handling in streams

Streams automatically handle errors and emit error events:

Key takeaways

  • stream.text_chunks / stream.textChunks - Simple text-only streaming
  • stream.events - Full access to lifecycle events, tool calls, and metadata
  • stream.text() - Get complete accumulated text
  • stream.result() - Get full result with usage and tool information
  • Lifecycle events - Emitted for both agent.run() and agent.stream()
  • Rich UIs - Use events to show tool execution, progress, and real-time updates