IN ENGLISH · API DOCS

Streaming responses (SSE) in the API

With "stream": true the answer arrives as Server-Sent Events, as the model's vendor sends them. On /v1/chat/completions the gateway switches stream_options.include_usage on by itself, so the last chunk always carries the token counts, and the bill is computed from them. If you close the connection in the middle of an answer, generation runs to its end and is billed in full.

Last updated:

[01]STREAMING

Streaming on the OpenAI-compatible routes

Pythonpython
from openai import OpenAI

client = OpenAI(base_url="https://1xai.ir/v1", api_key="1xai-...")

stream = client.chat.completions.create(
    model="gpt-5-mini",
    messages=[{"role": "user", "content": "Hello"}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
    if chunk.usage:  # the last chunk
        print("\n", chunk.usage.prompt_tokens, chunk.usage.completion_tokens)
  • Each event line is sent to you as it arrives from the vendor, without buffering, and the stream ends with data: [DONE].
  • The gateway sets stream_options.include_usage itself, whether you sent it or not. The last chunk before [DONE] has a usage field and an empty choices; code that reads choices[0] without checking has to allow for it.
  • On /v1/responses that option is not sent; the token counts are in the response.completed event.
  • A streamed answer has no X-1xai-Cost-Toman header. Take X-Request-Id from the headers and, once the stream has ended, read the amount from GET /v1/usage?request_id=… (the cost of a streamed call).
[02]STREAMING

Streaming on the Anthropic and Gemini routes

  • On /anthropic/v1/messages with "stream": true you receive Anthropic's own events (message_start, content_block_delta, message_delta and the rest), unchanged (Anthropic).
  • On Gemini, call :streamGenerateContent; with ?alt=sse the answer is SSE and the last chunk has usageMetadata (Gemini).
[03]STREAMING

Dropped connections and time limits

  • Closing the connection does not stop generation: the answer is produced to its end on the vendor's side and its full cost is charged. To keep an answer short, set max_tokens.
  • The vendor has 10 minutes to answer. Do not set your own client's timeout lower than that, or you will lose long answers and still pay for them.
  • An error that happens before the stream starts is an ordinary JSON response with an error status, not an event (errors).

1xAi is an independent gateway. It is not affiliated with, endorsed by, or a reseller for OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, Z.ai, Alibaba Qwen, MiniMax or Tencent Hunyuan. Requests are relayed upstream rather than answered by models of our own, so each provider's own usage policies apply to what their models will and will not do, and their model availability is theirs to change. Current reachability is published on the status page.