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

# Streaming

> Stream writer-0.1's answers as server-sent events on Chat Completions and the Responses API: chunk and event formats, usage, timeouts and cancelling.

Set `stream: true` to receive the answer as it is written, as server-sent events in OpenAI's format. Without it, one JSON response arrives when generation finishes. Both modes have the same output allowance.

Stream when answers are long or when people watch them appear. A JSON response can take minutes for a long answer, so set your HTTP client timeout above the gateway's 720-second model deadline, or stream.

## Chat Completions

```ts TypeScript theme={"system"}
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://platform.usenotra.com/v1",
  apiKey: process.env.NOTRA_LABS_API_KEY,
});

const stream = await client.chat.completions.create({
  model: "writer-0.1",
  messages: [{ role: "user", content: "Write a changelog entry for faster search." }],
  stream: true,
  stream_options: { include_usage: true },
});

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

```bash curl theme={"system"}
curl -N https://platform.usenotra.com/v1/chat/completions \
  -H "Authorization: Bearer $NOTRA_LABS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "writer-0.1",
    "messages": [{ "role": "user", "content": "Write a changelog entry for faster search." }],
    "stream": true
  }'
```

Each event is a `chat.completion.chunk`. The plan arrives first, as `delta.reasoning_content`, then the answer as `delta.content`. Tool calls stream as `delta.tool_calls`. The stream ends with `data: [DONE]`.

The last chunk carries `usage` only when you send `stream_options: { include_usage: true }`. That chunk has an empty `choices` array.

## Responses API

```ts theme={"system"}
const stream = await client.responses.create({
  model: "writer-0.1",
  input: "Write a changelog entry for faster search.",
  stream: true,
});

for await (const event of stream) {
  if (event.type === "response.output_text.delta") process.stdout.write(event.delta);
}
```

Events follow OpenAI's Responses format: `response.created` and `response.in_progress`, then `response.output_item.added`, `response.content_part.added` and `response.output_text.delta` for the answer, and `response.function_call_arguments.delta` and `.done` for function calls. The last event is `response.completed`, or `response.incomplete` when the output limit ended the answer. The Responses API doesn't stream the plan.

## Cancelling

Close the connection to stop generation. You pay for the tokens already sent, and the request shows up in Logs with status 499 `client_closed`. The API never returns that status to you.

## Errors during a stream

Errors before the stream starts come back as a JSON error with an HTTP status. If the model stream breaks off after that, the connection ends with an error, without `data: [DONE]` (Responses: without `response.completed`), and the request appears in Logs with `upstream_error` or `upstream_timeout`. Retry it. See [Errors](https://www.usenotra.com/docs/platform/api-reference/errors.md).


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