Streaming Hooks
Streaming hooks let you process content as it arrives in real-time during streaming responses - perfect for displaying progress, filtering content, or detecting tool calls.Overview
Streaming hooks provide 7 execution points:| Hook | Fires | Use Case |
|---|---|---|
on_chunk | Every chunk | Real-time display, filtering |
on_content | Content chunks | Text processing, display |
on_tool_detect | Tool call detected | Tool call awareness |
on_tool_start | Tool call starts | Tool execution tracking |
on_tool_delta | Tool arg delta | Parameter building |
on_tool_complete | Tool call complete | Tool execution start |
on_tool_result | Tool result | Result processing |
Hook Registration
Decorator Syntax
Subscribe API
Stream Hook Context
Every streaming hook receives aStreamExecContext with:
Delta Property
Thedelta property intelligently extracts text from chunks:
Execution Flow
On Chunk Hook
Fires for every chunk received during streaming:On Content Hook
Fires specifically for content chunks (not tool-related):On Tool Detect Hook
Fires when a tool call is first detected in the stream:On Tool Start Hook
Fires when a tool call chunk starts:On Tool Delta Hook
Fires for each tool argument delta during streaming:On Tool Complete Hook
Fires when a tool call is complete:On Tool Result Hook
Fires when tool result is received:Usage Patterns
Pattern 1: Real-Time Display
Display streaming content as it arrives:Pattern 2: Progress Tracking
Track streaming progress with visual indicator:Pattern 3: Content Filtering
Filter sensitive content in real-time:Pattern 4: Chunk Buffering
Buffer chunks for processing:Pattern 5: Tool Call Monitoring
Monitor tool calls during streaming:Pattern 6: Streaming Analytics
Collect analytics during streaming:Chunk Types
Streaming hooks handle different chunk types:Content Chunks
Tool Start Chunks
Tool Delta Chunks
Tool Complete Chunks
Tool Result Chunks
Best Practices
Use on_chunk for universal streaming
Use on_chunk for universal streaming
Keep streaming hooks fast
Keep streaming hooks fast
Use delta property for text extraction
Use delta property for text extraction
Check chunk_type for specific processing
Check chunk_type for specific processing
Performance Considerations
- Hook overhead: Less than 2ms per chunk
- Chunk frequency: Can be 50-100+ chunks/second
- Async support: Async hooks run concurrently
- Buffer impact: Large buffers (100+ chunks) reduce hook calls
Integration with Agent Methods
Streaming hooks work with bothstream() and astream():

