# Streaming

> The recommended mode for every integration.


Pass `stream: true` to receive standard `chat.completion.chunk` SSE, terminated by `data: [DONE]`. Because agent turns can run for minutes, streaming is the recommended mode for every integration — a stream is unambiguous about liveness where a long POST is not.

## What the stream contains

1. **An immediate first chunk** (`delta: {"role": "assistant", "content": ""}`) — fast first byte even while the agent environment spins up.
2. **SSE comment lines** (`: keepalive`) during silent agent work. Every OpenAI SDK ignores them per the SSE spec — but don't buffer the stream through proxies that time out on first-byte silence.
3. **One or more `delta.content` chunks** carrying the reply.
4. **The final chunk with `finish_reason`** — always carries the `x_ravchat` block, even without `stream_options`.
5. With **`stream_options.include_usage`** — one extra chunk before `[DONE]` with `choices: []` and populated `usage` (including `usage.cost`). All other chunks carry `usage: null`.

<Note>
In v1 the reply currently arrives as a **single content chunk** (whole-message), not token-by-token; finer-grained incremental deltas are a planned fast-follow. Write your client to consume any number of `delta.content` chunks — as every OpenAI SDK already does — and rely on `finish_reason` (not chunk count) for completion. Nothing changes when incremental deltas land.
</Note>

## Reading credits from a stream

Streaming responses can't carry `usage.cost` in a header — headers are sent before the turn runs. Read credits from the final chunk's `x_ravchat.credits` or from the `include_usage` usage chunk.

```python
stream = client.chat.completions.create(
    model="ravchat",
    messages=[{"role": "user", "content": "Explain bitachon in two sentences."}],
    stream=True,
    stream_options={"include_usage": True},
)
text, cost = "", None
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        text += chunk.choices[0].delta.content
    if chunk.usage:  # final usage chunk
        cost = (chunk.usage.model_extra or {}).get("cost")
print(text, "\ncredits:", cost)
```
