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

# Streaming

> Receive a chat completion as server-sent events: event format, final usage, timeouts and errors.

Set `stream: true` on a [chat completions](https://docs.fortytwo.network/docs/chat-completions.md) request to receive the response as server-sent events.

## Event format

* The response has the content type `text/event-stream`.
* Each event's `data` is a JSON object with `object: "chat.completion.chunk"`. The new text is in `choices[].delta.content`.
* Reasoning arrives in `delta.reasoning` and `delta.reasoning_details`. Tool call arguments arrive in `delta.tool_calls`.
* The stream ends with the line `data: [DONE]`.

## Usage on the final chunk

**stream\_options.include\_usage** `boolean`

`true` adds a final chunk with `usage`, sent before `data: [DONE]`. Without it, the stream has no usage data. Billing is the same either way.

## Read a stream

```python Python theme={null}
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["FORTYTWO_API_KEY"],
    base_url="https://api.fortytwo.network/v1",
)

model_id = "qwen/qwen3.8-27b"

stream = client.chat.completions.create(
    model=model_id,
    messages=[{"role": "user", "content": "Describe the tide in three sentences."}],
    stream=True,
    stream_options={"include_usage": True},
)

usage = None

for chunk in stream:
    if chunk.usage is not None:
        usage = chunk.usage
    for choice in chunk.choices:
        if choice.delta.content:
            print(choice.delta.content, end="", flush=True)

print()
print(usage)
```

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

const client = new OpenAI({
  apiKey: process.env.FORTYTWO_API_KEY,
  baseURL: "https://api.fortytwo.network/v1",
});

const modelId = "qwen/qwen3.8-27b";

const stream = await client.chat.completions.create({
  model: modelId,
  messages: [{ role: "user", content: "Describe the tide in three sentences." }],
  stream: true,
  stream_options: { include_usage: true },
});

let usage;

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

console.log();
console.log(usage);
```

## Timeouts

* A stream can be silent for up to 4 minutes, for example while the model reads a long prompt. Set the client read timeout above 4 minutes. A shorter timeout can close a stream that is still working.
* If nothing arrives for 4 minutes, the stream ends with an `upstream_timeout` error event.
* A request can run for up to 60 minutes — see [Tiers and limits](https://docs.fortytwo.network/docs/limits.md#how-long-a-request-may-run). A stream that reaches the limit also ends with `upstream_timeout`.

## Errors during a stream

If the request fails before the first event, the response is an ordinary error with an HTTP status and a JSON body — see [Errors](https://docs.fortytwo.network/docs/errors.md).

If it fails after the stream started, the last event is an error, and the stream closes. A timeout has the code `upstream_timeout` and the message "The request exceeded its time limit."; any other failure has `upstream_error`:

```json theme={null}
{
  "error": {
    "message": "The request could not be completed.",
    "type": "server_error",
    "param": null,
    "code": "upstream_error"
  },
  "request_id": "req_…"
}
```

A stream that ends without `data: [DONE]` and without an error event lost its connection. A stopped stream cannot be resumed; a new request is charged separately — see [What is billed](https://docs.fortytwo.network/docs/pricing.md#what-is-billed). The `x-request-id` response header identifies the request for [support](https://docs.fortytwo.network/docs/support.md).


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