Skip to main content
Streaming returns model output in small chunks as it is generated. Use it for chat interfaces, long-form generation, agent logs, and any workflow where users should see progress immediately.

Enable streaming

Set stream to true in the request body:
{
  "model": "gpt-4o",
  "messages": [{"role": "user", "content": "Write a short poem about spring."}],
  "stream": true
}

Response format

Streaming responses use server-sent events. Each event starts with data: and contains a partial completion chunk:
data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Spring"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" arrives"},"finish_reason":null}]}

data: {"id":"chatcmpl-xxx","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
Key points:
  • Incremental text is in choices[0].delta.content.
  • Concatenate all delta.content values in order to build the final answer.
  • The final normal chunk includes a finish_reason such as stop.
  • The stream ends with data: [DONE].

cURL example

curl https://moxus.ai/v1/chat/completions \
  -H "Authorization: Bearer sk-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{"role": "user", "content": "Write a short poem about spring."}],
    "stream": true
  }'

Python example

from openai import OpenAI

client = OpenAI(
    api_key="sk-your-key",
    base_url="https://moxus.ai/v1",
)

stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a short poem about spring."}],
    stream=True,
)

for chunk in stream:
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)

print()

Node.js example

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "sk-your-key",
  baseURL: "https://moxus.ai/v1",
});

const stream = await client.chat.completions.create({
  model: "gpt-4o",
  messages: [{ role: "user", content: "Write a short poem about spring." }],
  stream: true,
});

for await (const chunk of stream) {
  const delta = chunk.choices[0]?.delta?.content || "";
  process.stdout.write(delta);
}

Include usage data

Streaming responses may not include token usage by default. If the model supports it, request a final usage chunk:
{
  "model": "gpt-4o",
  "messages": [{"role": "user", "content": "Hello"}],
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}
Support for stream_options depends on the selected model.

When to use streaming

ScenarioRecommendation
Chat UI or interactive assistantUse streaming
Long articles, reports, or summariesUse streaming
Background batch processingNon-streaming is simpler
Strict JSON parsingStream only if you concatenate and parse after completion

Next steps