> For the complete documentation index, see [llms.txt](https://docs.avis.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.avis.xyz/api-reference/introduction/streaming.md).

# Streaming

Streaming is opt-in and works differently on each surface — the OpenAI and Anthropic surfaces toggle it with a body field, the Gemini surface uses a separate URL.

### Enabling / disabling streaming

| Surface                                   | How to stream                                                                                                                                                            | How to get a single JSON response |
| ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------- |
| OpenAI (`chat/completions`, `/responses`) | `"stream": true` in the request body                                                                                                                                     | `"stream": false` (default)       |
| OpenAI (`images/generations`)             | `"stream": true` — only honored when the resolved model's provider supports streamed image events; otherwise the request still completes, just as a single JSON response | `"stream": false` (default)       |
| OpenAI (`videos`)                         | Not applicable — video generation is always async (submit + poll), see OpenAI compatibility                                                                              | n/a                               |
| Anthropic (`messages`)                    | `"stream": true` in the request body                                                                                                                                     | `"stream": false` (default)       |
| Gemini                                    | Call `models/{model}:streamGenerateContent` instead of `models/{model}:generateContent`                                                                                  | Call `:generateContent`           |

All streamed responses are sent as `Content-Type: text/event-stream` with `Cache-Control: no-cache, no-transform` and `Connection: keep-alive`.

### SSE format & chunk structure

#### OpenAI (`chat/completions`, `/responses`)

Standard OpenAI-style SSE: one `data:` line per chunk, each a JSON delta.

```json
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"Hel"}}]}

data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"lo!"}}]}

data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":9,"completion_tokens":2,"total_tokens":11}}

data: [DONE]
```

**Done signal:** a literal `data: [DONE]` line, always sent as the final event — Avis appends this itself even if the upstream chunk stream ends without one.

#### Anthropic (`messages`)

Native Anthropic event stream — this endpoint is a thin proxy, so the event sequence is exactly what the upstream Anthropic-compatible provider sends, unmodified:

```json
event: message_start
data: {"type":"message_start","message":{"id":"msg_abc","role":"assistant","model":"anthropic/claude-sonnet-4-5","usage":{"input_tokens":10,"output_tokens":1}}}

event: content_block_start
data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello!"}}

event: content_block_stop
data: {"type":"content_block_stop","index":0}

event: message_delta
data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":12}}

event: message_stop
data: {"type":"message_stop"}
```

**Done signal:** the `event: message_stop` event. There is no `[DONE]` line on this surface.

#### Gemini (`:streamGenerateContent`)

Also a thin proxy — each `data:` line is a JSON chunk in the native Gemini shape:

```json
data: {"candidates":[{"content":{"parts":[{"text":"Hello"}],"role":"model"}}]}

data: {"candidates":[{"content":{"parts":[{"text":"!"}],"role":"model"}},"finishReason":"STOP"],"usageMetadata":{"promptTokenCount":2,"candidatesTokenCount":10}}
```

**Done signal:** none. The Gemini API doesn't emit an explicit terminal event — the client detects completion when the connection closes (and, if present, the last chunk's `finishReason`).

### Handling a dropped connection

If the client disconnects mid-stream, there is **no resume mechanism** on any surface — no continuation token, no `previous_response_id`-style replay. If you need the rest of the response, retry the request from the beginning.

Billing is unaffected by a client-side disconnect: usage is extracted from a background copy of the stream that keeps draining independently of the client connection, so a partially-received stream on the client still settles correctly against whatever the upstream actually generated.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.avis.xyz/api-reference/introduction/streaming.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
