---
title: Streaming completions
description: Stream completion events over Server-Sent Events through the Zorveus gateway.
---

<ApiEndpoint method="POST" path="/v1/chat/completions" auth="api_key" />

Setting `"stream": true` streams tokens incrementally as Server-Sent Events (SSE) chunks, allowing your UI to display characters as they are generated.

<KeyInserter type="api" label="API Key Tester" placeholder="zrv_your_api_key" />

---

## Code examples

<CodeGroup titles={["Python (OpenAI SDK)", "TypeScript (OpenAI SDK)", "cURL"]}>

```python
from openai import OpenAI

client = OpenAI(
    base_url="https://api.zorveus.com/v1",
    api_key="zrv_your_api_key",
)

stream = client.chat.completions.create(
    model="openai/gpt-4.1-mini",
    messages=[
        {"role": "user", "content": "Write a haiku about distributed systems."}
    ],
    stream=True,
)

for chunk in stream:
    content = chunk.choices[0].delta.content or ""
    print(content, end="", flush=True)

print()
```

```typescript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api.zorveus.com/v1",
  apiKey: "zrv_your_api_key",
});

async function main() {
  const stream = await client.chat.completions.create({
    model: "openai/gpt-4.1-mini",
    messages: [
      { role: "user", content: "Write a haiku about distributed systems." },
    ],
    stream: true,
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || "");
  }
  process.stdout.write("\n");
}

main();
```

```bash
curl https://api.zorveus.com/v1/chat/completions \
  -N \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer zrv_your_api_key" \
  -d '{
    "model": "openai/gpt-4.1-mini",
    "messages": [
      {"role": "user", "content": "Write a haiku about distributed systems."}
    ],
    "stream": true
  }'
```

</CodeGroup>

---

## Usage accounting for streams

Zorveus records the final provider outcome and available token usage after the stream completes or disconnects. A client disconnect does not guarantee that the upstream provider stopped before producing more billable output.

Preserve incomplete SSE lines between network chunks. Treat a stream as complete only after `data: [DONE]` or a documented terminal event. If you retry after a disconnect, the provider can bill both requests.
