Skip to main content

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 Registration

Decorator Syntax

Subscribe API

Stream Hook Context

Every streaming hook receives a StreamExecContext with:

Delta Property

The delta 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

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 both stream() and astream():

Error Handling

Errors in streaming hooks don’t stop the stream:

What’s Next?

Tool Hooks

Tool execution lifecycle hooks

Message Hooks

Message handling and modification

Subscribe API

Dynamic hook registration

Event Streaming

Learn about agent.events()