Streaming
Learn how to stream AI responses in real-time using Server-Sent Events for a better user experience.
What is Streaming?
Streaming allows you to receive AI responses token-by-token as they're generated, rather than waiting for the complete response. This creates a better user experience, especially for long responses.
Instead of showing a loading spinner for 10 seconds, users see text appearing in real-time - just like ChatGPT.
Why Use Streaming?
- Better UX: Users see responses immediately instead of waiting
- Perceived Performance: Feels faster even if total time is the same
- Early Cancellation: Users can stop generation if they got what they need
- Real-time Feedback: Users know the AI is working, not stuck
How Streaming Works
Cencori uses Server-Sent Events (SSE) for streaming:
- Your app calls
cencori.ai.chatStream() - Cencori forwards the request to the AI provider
- As the provider generates tokens, Cencori streams them back
- Your app displays each chunk as it arrives
- Stream ends when generation completes
Client → Cencori → AI Provider ↓ ↓ ↓ Chunk 1 ← Chunk 1 ← Token 1, 2, 3 Chunk 2 ← Chunk 2 ← Token 4, 5, 6 ... ... ... Done ← Done ← Complete
Basic Usage
stream-example.ts
React/Next.js Example
Server Route (API)
app/api/chat/route.ts
Client Component
components/Chat.tsx
Node.js Server Example
server.ts
Stream Chunk Format
Each chunk in the stream contains:
chunk-format.ts
Example chunks:
example-chunks.json
Error Handling
Handle errors gracefully during streaming:
error-handling.ts
Best Practices
- Show Loading State: Display a typing indicator before first chunk
- Handle Connection Drops: Implement reconnection logic
- Allow Cancellation: Let users stop generation early
- Buffer Chunks: Avoid too many DOM updates by batching small chunks
- Error Messages: Show user-friendly errors if stream fails
- Timeout Protection: Set maximum generation time
Provider Support
All Cencori providers support streaming:
| Provider | Streaming | Notes |
|---|---|---|
| OpenAI | ✅ Full support | Native SSE streaming |
| Anthropic | ✅ Full support | Native SSE streaming |
| Google Gemini | ✅ Full support | Converted to SSE format |
| Custom | ✅ If compatible | Must support streaming |
Troubleshooting
Stream Not Starting
- Verify API key is valid
- Check model supports streaming
- Ensure proper headers are set
Stream Cuts Off Early
- Check server timeout settings
- Verify
maxTokensisn't too low - Look for connection drops

