> ## Documentation Index
> Fetch the complete documentation index at: https://docs.moxus.cloud/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-5.4-mini",
  "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

`-N` disables cURL output buffering so each SSE chunk appears as soon as it arrives. Replace `sk-your-key` in the command with your actual key.

```bash theme={null}
curl -N https://moxus.cloud/v1/chat/completions \
  -H "Authorization: Bearer sk-your-key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4-mini",
    "messages": [{"role": "user", "content": "Write a short poem about spring."}],
    "stream": true
  }'
```

## Python example

Replace `sk-your-key` in `API_KEY` with your actual key, then iterate over the returned stream:

```python theme={null}
import httpx
from openai import OpenAI

API_KEY = "sk-your-key"

client = OpenAI(
    api_key=API_KEY,
    base_url="https://moxus.cloud/v1",
    # Connect directly to Moxus AI without reading system or terminal proxy variables.
    http_client=httpx.Client(trust_env=False, timeout=60.0),
)

for chunk in client.chat.completions.create(
    model="gpt-5.4-mini",
    messages=[{"role": "user", "content": "Write a short poem about spring."}],
    stream=True,
    # Optional: receive token usage in the final chunk. Remove stream_options when it is not needed or unsupported.
    stream_options={"include_usage": True},
):
    if chunk.usage:
        print(f"\nUsage: {chunk.usage}")
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta.content or ""
    print(delta, end="", flush=True)

print()
```

## Node.js example

Replace `sk-your-key` in `API_KEY` with your actual key:

```javascript theme={null}
import OpenAI from "openai";

const API_KEY = "sk-your-key";

const client = new OpenAI({
  apiKey: API_KEY,
  baseURL: "https://moxus.cloud/v1",
});

const stream = await client.chat.completions.create({
  model: "gpt-5.4-mini",
  messages: [{ role: "user", content: "Write a short poem about spring." }],
  stream: true,
  // Optional: receive token usage in the final chunk. Remove stream_options when it is not needed or unsupported.
  stream_options: { include_usage: true },
});

for await (const chunk of stream) {
  if (chunk.usage) {
    console.log("\nUsage:", chunk.usage);
  }
  const delta = chunk.choices[0]?.delta?.content || "";
  process.stdout.write(delta);
}
```

## 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)
