---
agent:
  instructions: "Search the site by keyword or question. The search API returns Markdown."
  endpoint: "https://platform.invideo.io/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Streaming chat answers

> Read chat output, the Platform request ID, and usage and cost in OpenAI-compatible server-sent events.

# Streaming

Set `"stream": true` and the answer comes as server-sent events while the model
writes it, in OpenAI's `chat.completion.chunk` format. The stream ends with
`data: [DONE]`.

```bash
curl -N https://platform-api.invideo.io/v1/chat/completions \
  -H "Authorization: Bearer $INVIDEO_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "gemini-4-carbon", "stream": true, "messages": [{"role": "user", "content": "Write a haiku about rain."}]}'
```

```text
data: {"id":"0193b0c4-7640-72d1-91e7-e11f44de3708","object":"chat.completion.chunk","created":1795680000,"model":"gemini-4-carbon","choices":[{"index":0,"delta":{"content":"Soft"},"finish_reason":null}]}

data: {"id":"0193b0c4-7640-72d1-91e7-e11f44de3708","object":"chat.completion.chunk","created":1795680000,"model":"gemini-4-carbon","choices":[{"index":0,"delta":{"content":" rain on"},"finish_reason":null}]}

data: {"id":"0193b0c4-7640-72d1-91e7-e11f44de3708","object":"chat.completion.chunk","created":1795680000,"model":"gemini-4-carbon","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":14,"completion_tokens":19,"total_tokens":33,"cost":"0.000101","cost_usd":"0.000101","invideo_cost":{"lines":[{"item":"request","quantity":"1","unit_price_usd":"0.000101","amount_usd":"0.000101"}],"total_usd":"0.000101"}},"cost_usd":"0.000101"}

data: {"id":"0193b0c4-7640-72d1-91e7-e11f44de3708","object":"chat.completion.chunk","created":1795680000,"model":"gemini-4-carbon","choices":[],"usage":{"prompt_tokens":14,"completion_tokens":19,"total_tokens":33}}

data: [DONE]
```

```python
from openai import OpenAI
import os

client = OpenAI(base_url="https://platform-api.invideo.io/v1", api_key=os.environ["INVIDEO_API_KEY"])

stream = client.chat.completions.create(
    model="gemini-4-carbon",
    messages=[{"role": "user", "content": "Write a haiku about rain."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
```

## What the chunks hold

- `delta.content`: the next piece of the answer.
- `delta.reasoning_content`: the next piece of the model's reasoning, for models that give it.
- `delta.tool_calls`: tool calls as they are written (`index`, then `id`, `function.name` and pieces of `function.arguments`), as OpenAI sends them.
- The finishing choice carries `finish_reason`. Its chunk carries `cost_usd` and `usage`, including `usage.cost_usd` and `usage.invideo_cost` with the charge lines and total.
- A usage chunk with `choices: []` follows before `[DONE]`. You can read usage without setting `stream_options`.

A chunk with `{"error": {...}}` ends the stream. See [Errors](/docs/errors) for error codes. Errors before the stream opens arrive as HTTP errors.

## Closing early

If you close the connection early, the model finishes in the background. Platform bills the tokens used and records the request in Logs. Lower `max_tokens` to cap the length of the answer. Get the stored answer with `GET /v1/chat/completions/{id}` if your connection drops. To resume a live stream from a cursor, use [Responses background streaming](/docs/chat/responses#resume-a-responses-stream).

The stream opens as soon as the request is accepted. Lines that start with `:`
are keep-alive comments while a model thinks; SSE clients skip them.

Every JSON chunk’s `id` is the Platform request ID and matches the `x-request-id` response header. Use it to find the request in Logs. Read the ID from the JSON or header. The stream has no SSE `id:` lines or separate `request_id` field.
