# Streaming

Server-sent events of a streamed answer, the last chunk with usage, and errors in a stream.

Source: https://docs.noduma.dev/api/streaming/

With `"stream": true`, the answer arrives as server-sent events (`Content-Type: text/event-stream`) as the PC writes it. Each event is a line `data: <JSON>` followed by an empty line, and the stream ends with `data: [DONE]`.

**curl**

```sh
curl -N https://app.noduma.dev/v1/chat/completions \
  -H "Authorization: Bearer $NODUMA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen2.5-7b-instruct-q4_k_m", "stream": true,
       "messages": [{"role": "user", "content": "Count to five."}]}'
```

**Python**

```python
stream = client.chat.completions.create(
    model="qwen2.5-7b-instruct-q4_k_m",
    messages=[{"role": "user", "content": "Count to five."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)
```

**JavaScript**

```js
const stream = await client.chat.completions.create({
  model: 'qwen2.5-7b-instruct-q4_k_m',
  messages: [{ role: 'user', content: 'Count to five.' }],
  stream: true,
});
for await (const chunk of stream) process.stdout.write(chunk.choices[0]?.delta?.content ?? '');
```

## The events

```text
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","created":1764460800,"model":"qwen2.5-7b-instruct-q4_k_m","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

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

data: {"id":"chatcmpl-...","object":"chat.completion.chunk", ... "choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":14,"completion_tokens":12,"total_tokens":26,"noduma_cost":15,"noduma_price_per_1k_tokens":1000}}

data: [DONE]
```

1. The first chunk carries the role.
2. Text chunks carry `delta.content`.
3. When the answer calls tools, one chunk carries all `delta.tool_calls`, each with its `index`, `id`, `type` and `function`.
4. The last chunk before `[DONE]` carries `finish_reason` and `usage`, with the answer's cost. You need not ask for it with `stream_options`.

The response headers include `Noduma-Routing`, the routing mode used, and `Cache-Control: no-store`.

## Errors

- **Before the first chunk**, an error is an ordinary JSON error with its HTTP status, as without streaming.
- **After the stream started**, an error arrives as an event, `data: {"error": {"message": "...", "type": "...", "code": "..."}}`, and the stream ends. The status is already `200`, so read the events.

## When a PC fails mid-answer

If the PC that answers fails, the coordinator moves the request to another PC serving the model that can continue an answer, and that PC continues it from where it stopped: you see a pause, not a repeat. When no PC can continue it, the stream ends with an error event.

## Proxies

If you put a proxy in front of your client, it must not buffer the event stream. Noduma's own answers carry `X-Accel-Buffering: no` for nginx.
