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:
Streaming on the OpenAI-compatible routes
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_usageitself, whether you sent it or not. The last chunk before[DONE]has ausagefield and an emptychoices; code that readschoices[0]without checking has to allow for it. - On
/v1/responsesthat option is not sent; the token counts are in theresponse.completedevent. - A streamed answer has no
X-1xai-Cost-Tomanheader. TakeX-Request-Idfrom the headers and, once the stream has ended, read the amount fromGET /v1/usage?request_id=…(the cost of a streamed call).
Streaming on the Anthropic and Gemini routes
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.