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

Prerequisites

  • An Auriko API key
  • Python 3.10+ with the OpenAI SDK (pip install openai) or the auriko SDK (pip install auriko)
    • OR Node.js 18+ with the OpenAI SDK (npm install openai) or @auriko/sdk (npm install @auriko/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.
PropertyPythonTypeScriptAvailable
Token usagestream.usagestream.usageAfter iteration
Routing infostream.routing_metadatastream.routing_metadataAfter iteration
Response headersstream.response_headersstream.responseHeadersImmediately
Close connectionstream.close()stream.close()Any time
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.