Why Streaming?
Better UX
Users see responses appear in real-time instead of waiting for the full response
Tool Visibility
Show when the agent is using tools (checking weather, searching, etc.)
Lower Perceived Latency
First token appears quickly, even for long responses
Progressive Rendering
Display partial results while the agent continues working
Basic Streaming
Themessages.stream() method returns an async generator that yields events:
Event Types
The stream yields different event types as the response progresses:| Event Type | Description | Data Fields |
|---|---|---|
message.start | Agent began generating a response | - |
content | A chunk of text content | text |
tool.start | Agent started using a tool | name, display |
tool.complete | Tool finished executing | name, result |
message.complete | Full response is ready | messageId |
error | An error occurred | error |
done | Stream is complete | - |
Complete Event Handling
Here’s how to handle all event types:React Example
Here’s how to build a streaming chat component in React:Node.js CLI Example
A complete command-line chat application:Error Handling in Streams
Always wrap streaming in try-catch:Cancelling Streams
To cancel a stream early, you can break out of the loop:Best Practices
Show Loading States
Display an indicator when waiting for
message.startHandle Tool Events
Show users when the agent is using tools for transparency
Buffer Content
For smooth rendering, consider buffering small chunks
Handle Disconnects
Implement reconnection logic for long-running streams