> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moxus.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Streaming responses

> Stream model output incrementally instead of waiting for the full response.

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:

```json theme={null}
{
  "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:

```text theme={null}
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

```bash theme={null}
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

```python theme={null}
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

```javascript theme={null}
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:

```json theme={null}
{
  "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

| Scenario                             | Recommendation                                            |
| ------------------------------------ | --------------------------------------------------------- |
| Chat UI or interactive assistant     | Use streaming                                             |
| Long articles, reports, or summaries | Use streaming                                             |
| Background batch processing          | Non-streaming is simpler                                  |
| Strict JSON parsing                  | Stream only if you concatenate and parse after completion |

## Next steps

* [Function calling](/en/guide/function-calling)
* [Structured output](/en/guide/structured-output)
