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

# Streaming

> Stream a reply as server-sent events, ask for the usage chunk at the end, and handle an error that arrives mid-stream.

Set `"stream": true` to receive the reply as the model writes it. Morse answers with `Content-Type: text/event-stream`. Each event is a line `data: <json>` with one `chat.completion.chunk`, and the stream ends with `data: [DONE]`.

```bash theme={null}
curl -N https://api.trymorse.ai/v1/chat/completions \
  -H "Authorization: Bearer $MORSE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "glm-5.3",
    "messages": [{"role": "user", "content": "Write a haiku about rain."}],
    "stream": true,
    "stream_options": {"include_usage": true}
  }'
```

The `-N` flag stops curl from buffering the events.

With the Python SDK:

```python theme={null}
stream = client.chat.completions.create(
    model="glm-5.3",
    messages=[{"role": "user", "content": "Write a haiku about rain."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices:
        print(chunk.choices[0].delta.content or "", end="")
```

## Get the token counts

Morse sends a final chunk with `usage` only when the request sets `stream_options.include_usage` to `true`. No other chunk carries `usage`. The final chunk has an empty `choices` list.

## Ignore the padding

Each chunk has a top-level `obfuscation` field, a random string of 16 to 64 characters that pads the chunk. It holds no data. Ignore it.

On some reasoning models, Morse also sends a `: keepalive` comment every 25 seconds while the model thinks. Clients that follow the server-sent events specification ignore comments.

## Handle an error mid-stream

If the model fails after the stream starts, the status is already `200`. Morse then sends one event with the [error envelope](/errors) and ends the stream without `data: [DONE]`:

```json theme={null}
{"error":{"message":"The upstream model server failed.","type":"upstream_error","param":null,"code":"upstream_error"}}
```

Treat a stream that ends without `data: [DONE]` as incomplete.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.