Streaming
Stream LLM responses in real-time for better user experience
Display AI responses as they generate, creating a natural chat experience.
graph LR
subgraph "Streaming Response"
U[💬 User] --> A[🤖 Agent]
A --> S[⚡ Stream]
S --> |word by word| D[📺 Display]
end
classDef user fill:#6366F1,stroke:#7C90A0,color:#fff
classDef agent fill:#F59E0B,stroke:#7C90A0,color:#fff
classDef output fill:#10B981,stroke:#7C90A0,color:#fff
class U user
class A,S agent
class D outputQuick 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
sequenceDiagram
participant User
participant Agent
participant LLM
User->>Agent: chat("question")
Agent->>LLM: Request (stream=true)
loop Each token
LLM-->>Agent: StreamEvent::DeltaText
Agent-->>User: Display token
end
LLM-->>Agent: StreamEvent::StreamEnd
Agent-->>User: CompleteStream 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.