Streaming
Stream LLM responses in real-time for better user experience
Display AI responses as they generate, creating a natural chat experience.
Quick Start
- Enable Streaming
rust use praisonai::Agent; let agent = Agent::new() .name("Assistant") .instructions("You are helpful") .stream(true) // Enable streaming .build()?; agent.chat("Tell me a story").await?; // Response appears word by word - Disable Streaming
rust use praisonai::Agent; let agent = Agent::new() .name("Assistant") .stream(false) // Wait for complete response .build()?;
How It Works
Stream Events
Events emitted during streaming.
| Event | Description |
|---|---|
RequestStart | API call initiated |
HeadersReceived | HTTP headers arrived |
FirstToken | First content (TTFT marker) |
DeltaText | Text content chunk |
DeltaToolCall | Tool call in progress |
ToolCallEnd | Tool call complete |
LastToken | Final content chunk |
StreamEnd | Stream completed |
Error | Error occurred |
Configuration
| Option | Type | Default | Description |
|---|---|---|---|
stream | bool | true | Enable response streaming |
Best Practices
Enable for chat interfaces
Streaming provides immediate feedback, improving perceived performance.
Disable for batch processing
When processing many requests, disable streaming for efficiency.