Streaming
Set stream: true on /v1/chat/completions and the reply arrives as server-sent events, token by token, in the OpenAI chunk format.
Request a stream
curl
curl https://api.airuntime.dev/v1/chat/completions \
-H "Authorization: Bearer $TOKENS_PAY_KEY" \
-H "Content-Type: application/json" \
-N -d '{
"model": "deepseek-flash",
"stream": true,
"stream_options": {"include_usage": true},
"messages": [{"role": "user", "content": "Write a haiku about prepaid credit."}]
}'
Python
from openai import OpenAI
import os
client = OpenAI(base_url="https://api.airuntime.dev/v1", api_key=os.environ["TOKENS_PAY_KEY"])
stream = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": "Write a haiku about prepaid credit."}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
if chunk.usage:
print("\n", chunk.usage.total_tokens, "tokens")
Node
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.airuntime.dev/v1", apiKey: process.env.TOKENS_PAY_KEY });
const stream = await client.chat.completions.create({
model: "deepseek-flash",
messages: [{ role: "user", content: "Write a haiku about prepaid credit." }],
stream: true,
stream_options: { include_usage: true },
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
if (chunk.usage) console.log("\n", chunk.usage.total_tokens, "tokens");
}
What arrives
- Each event is
data: {…}with"object": "chat.completion.chunk"; text is inchoices[0].delta.content, and thinking (on reasoning models) inchoices[0].delta.reasoning_content. - Lines that start with a colon (
: keep-alive) keep the connection open while the model thinks. SDKs skip them. - The stream ends with
data: [DONE].
Usage and cost in the stream
Usage is sent only when you ask for it: add "stream_options": {"include_usage": true}. The last chunk before [DONE] then has usage with prompt_tokens, completion_tokens, prompt_tokens_details.cached_tokens, completion_tokens_details.reasoning_tokens when the provider reports it, and airuntime.cost_usd_micros. stream_options is allowed only with stream: true.
If the provider stops mid-stream
The stream ends with an error event and no [DONE]:
SSE
data: {"error":{"message":"The model provider interrupted the stream.","type":"server_error","code":"stream_interrupted"}}
A stream waits up to 10 minutes between pieces of output, and that clock restarts whenever the provider makes progress; there is no limit on total length. See what a failed request costs.