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

# Stream Your First Response

> Stream replies as they are written.

Streaming returns the reply token by token over Server-Sent Events (SSE): a long-lived HTTP response where each message begins with `data:`. The SDKs handle the framing.

## Turn It On

Set `stream: true` on the same chat request:

<CodeGroup>
  ```bash curl theme={"theme":{"light":"github-light","dark":"github-dark"}}
  curl -N "https://app.trynito.ai/v1/chat/completions" \
    -H "Authorization: Bearer $NITO_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "z-ai/glm-5.2",
      "stream": true,
      "messages": [{ "role": "user", "content": "Write a haiku about privacy." }]
    }'
  ```

  ```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
  from openai import OpenAI

  client = OpenAI(api_key="z_sk_your_key_here", base_url="https://app.trynito.ai/v1")

  stream = client.chat.completions.create(
      model="z-ai/glm-5.2",
      messages=[{"role": "user", "content": "Write a haiku about privacy."}],
      stream=True,
  )
  for chunk in stream:
      delta = chunk.choices[0].delta
      if delta.content:
          print(delta.content, end="", flush=True)
  ```

  ```typescript TypeScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
  import OpenAI from "openai";

  const client = new OpenAI({ apiKey: "z_sk_your_key_here", baseURL: "https://app.trynito.ai/v1" });

  const stream = await client.chat.completions.create({
    model: "z-ai/glm-5.2",
    messages: [{ role: "user", content: "Write a haiku about privacy." }],
    stream: true,
  });
  for await (const chunk of stream) {
    const delta = chunk.choices[0]?.delta?.content;
    if (delta) process.stdout.write(delta);
  }
  ```
</CodeGroup>

`-N` turns off curl's buffering so tokens show as they arrive.

## The Stream

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
data: {"id":"...","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"Sealed "},"finish_reason":null}]}

data: {"id":"...","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"and private"},"finish_reason":null}]}

data: {"id":"...","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
```

* New text is in `choices[0].delta.content`. Concatenate the chunks to rebuild the reply.
* `delta.role: "assistant"` appears once, on the first chunk that carries content or reasoning.
* The last content chunk sets `finish_reason`: `stop`, `length`, `tool_calls`, or `error`.
* `data: [DONE]` ends the stream. It is not JSON.
* `: keep-alive` comment lines can appear. Ignore them.

## Usage

Usage is known only when generation finishes. Ask for it:

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{ "model": "z-ai/glm-5.2", "stream": true, "stream_options": { "include_usage": true }, "messages": [ ... ] }
```

One extra chunk with an empty `choices` array and a `usage` object arrives before `data: [DONE]`.

## Mid-Stream Errors

Once the stream starts, HTTP `200` is already sent. A later failure arrives as a final chunk with a top-level `error` object, an empty `delta`, and `finish_reason: "error"`, and the stream ends without `data: [DONE]`. Check each chunk for `error` as well as the status. A failure before any byte is written returns an ordinary HTTP error.

## Related

<CardGroup cols={3}>
  <Card title="Stream Format Reference" icon="book" href="/api/api-reference/streaming-format/server-sent-events">
    Every chunk field.
  </Card>

  <Card title="Make Your First Request" icon="message" href="/api/get-started/make-your-first-request">
    Non-streaming.
  </Card>

  <Card title="Tool Call Streaming" icon="wrench" href="/api/api-reference/streaming-format/tool-call-streaming">
    Tool calls in fragments.
  </Card>
</CardGroup>


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