Why Streaming?
Streaming enables:- Real-time user feedback
- Progressive rendering
- Lower perceived latency
- Interruptible generation
- Tool call visibility
Basic Streaming
Text Stream
Stream text chunks as they’re generated:Full Stream
Access all stream events (text, tool calls, etc.):Stream Results
Theexecute() function returns a stream result object:
Getting Complete Text
Wait for the full response:Getting Usage Info
Finish Reason
Stream Types
Different chunk types in the full stream:Swarm Streaming
Theswarm() function provides high-level streaming for multi-agent systems:
Swarm Stream Result
Getting Messages
Abort Signals
Cancel streaming execution:Progress Tracking
Track tool calls and agent transitions:Buffering Strategies
Character Buffering
Buffer chunks for smoother output:Line Buffering
Wait for complete lines:Time Buffering
Flush periodically:UI Integration
React Integration
Server-Sent Events (SSE)
WebSocket Streaming
Error Handling
Stream Errors
Partial Results
Access partial text on error:Performance Tips
Chunk Size
Larger chunks = fewer events, less overhead
Buffer Wisely
Balance smoothness vs latency
Async Iterators
Use
for await...of for backpressureMemory
Clear old chunks from memory
Best Practices
Error Boundaries
Always wrap streams in try-catch
Abort Signals
Provide cancellation for long tasks
Progress Indicators
Show tool calls and status updates
Graceful Degradation
Fall back to complete text on errors
Next Steps
Agent Package
Full API reference
Examples
See streaming examples