Overview
Anannas supports streaming responses via Server-Sent Events (SSE). Enable streaming by settingstream: true in your request. The Anannas API returns incremental text deltas as tokens are generated, enabling real-time chat interfaces.
Enabling Streaming
Set thestream parameter to true:
Response Format
Streaming responses use Server-Sent Events (SSE) withContent-Type: text/event-stream. Each event contains a JSON object following the OpenAI streaming format.
Event Structure
Each SSE event has this format:Example Stream
Implementation Examples
Python
TypeScript/JavaScript
Using OpenAI SDK
The OpenAI SDK handles streaming automatically:SSE Comments
The stream may include comment lines for keep-alive:Cancellation
Cancel streaming requests by closing the connection. Billing stops immediately for supported providers when the connection is closed.Python
TypeScript
Tool Calls in Streams
Tool calls are streamed incrementally. Thedelta.tool_calls array contains partial tool call information:
arguments strings to reconstruct the complete JSON.
Error Handling
Errors in streaming responses are sent as regular SSE events:error field in parsed JSON objects.
Finish Reasons
The final chunk includesfinish_reason:
stop: Natural completion or stop sequencelength: Reachedmax_tokenstool_calls: Model requested tool executioncontent_filter: Content filtered by safety systemsnull: Stream incomplete
Usage Tracking
Streaming responses include usage information in the final chunk or a separate event. Accumulate token counts across chunks for accurate usage tracking.Best Practices
- Buffer Management: Accumulate deltas client-side for display
- Error Recovery: Implement retry logic for network failures
- Connection Management: Handle connection drops gracefully
- Token Counting: Track usage from final chunk or usage event
- Cancellation: Always cancel streams when user navigates away
See Also
- API Overview - Complete request/response schemas
- Parameters - All available parameters