Skip to main content
Arbytra streams chat completions over Server-Sent Events (SSE). Set stream: true and iterate over chunks as they arrive.

Prerequisites

  • An Arbytra API key
  • Python 3.10+ with the OpenAI SDK (pip install openai) or the arbytra SDK (pip install arbytra)
    • OR Node.js 18+ with the OpenAI SDK (npm install openai) or @arbytra/sdk (npm install @arbytra/sdk)

Stream responses

Stream a chat completion response:

Stream asynchronously

Stream with the async client:

Stream events

Each chunk contains:

Handle final chunks

The last content chunk carries finish_reason. A trailing chunk carries usage on every stream. You don’t need to set stream_options.
The final streaming chunk always contains token usage. Setting stream_options.include_usage explicitly is harmless but unnecessary.

Stream properties

The stream object exposes usage, routing metadata, and response headers after iteration completes. Use the stream as a context manager to ensure the connection is released:
Use a context manager for automatic cleanup:
routing_metadata and usage arrive on separate trailing chunks after all content chunks. Consume the stream to completion to access them.
In TypeScript, you can only iterate a stream once. A second attempt throws an error.

Stream with tools

Reassemble streamed tool call chunks into complete function calls:
See Tool Calling Guide for function definitions and multi-turn tool conversations.

Stream with routing options

Pass routing options to a streaming request:

Handle stream errors

Catch errors during streaming:
See Error Handling Guide for retry strategies and circuit breakers.

SSE format

Raw SSE events look like this. The stream ends with usage and routing_metadata events before [DONE].
The trailing events before [DONE] carry usage and routing_metadata with choices: []. SDKs expose these as stream.usage and stream.routing_metadata after iteration.
Streams may carry SSE comment lines (for example : ARBYTRA PROCESSING) during quiet phases, such as a reasoning model’s thinking phase. Comment lines start with a colon; spec-compliant SSE parsers skip them.