Skip to main content

Overview

Anannas supports streaming responses via Server-Sent Events (SSE). Enable streaming by setting stream: true in your request. The Anannas API returns incremental text deltas as tokens are generated, enabling real-time chat interfaces.

Enabling Streaming

Set the stream parameter to true:

Response Format

Streaming responses use Server-Sent Events (SSE) with Content-Type: text/event-stream. Each event contains a JSON object following the OpenAI streaming format.

Event Structure

Each SSE event has this format:
The JSON object structure:

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:
These can be ignored per the SSE specification. They serve as connection keep-alive signals.

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. The delta.tool_calls array contains partial tool call information:
Accumulate arguments strings to reconstruct the complete JSON.

Error Handling

Errors in streaming responses are sent as regular SSE events:
Handle errors by checking for the error field in parsed JSON objects.

Finish Reasons

The final chunk includes finish_reason:
  • stop: Natural completion or stop sequence
  • length: Reached max_tokens
  • tool_calls: Model requested tool execution
  • content_filter: Content filtered by safety systems
  • null: 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

  1. Buffer Management: Accumulate deltas client-side for display
  2. Error Recovery: Implement retry logic for network failures
  3. Connection Management: Handle connection drops gracefully
  4. Token Counting: Track usage from final chunk or usage event
  5. Cancellation: Always cancel streams when user navigates away

See Also