# Streaming and reasoning

## Enable a stream

Set `stream: true` in the request. Chat Completions, Responses, legacy Completions and Messages use server-sent events. Ollama-compatible endpoints stream newline-delimited JSON.

~~~bash
curl --no-buffer --fail-with-body -sS https://site6.eokgg.win/v1/chat/completions \
  -H "Authorization: Bearer $SITE6_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "deepseek-v4.1-flash-1017",
    "messages": [{"role": "user", "content": "Write three short sentences about caching."}],
    "stream": true,
    "stream_options": {"include_usage": true},
    "max_tokens": 256
  }'
~~~

## Collect text and handle completion

| Endpoint | Text arrives in | Terminal signal |
|---|---|---|
| Chat Completions | `choices[].delta.content` | `finish_reason` followed by `[DONE]` |
| Responses | `response.output_text.delta` events | `response.completed` or `response.incomplete` |
| Messages | `content_block_delta` events | `message_stop` |
| Legacy Completions | `choices[].text` | `finish_reason` followed by `[DONE]` |
| Ollama | `message.content` or `response` | JSON record with `done: true` |

An SSE read can contain part of an event or several events. Split complete SSE events before parsing their JSON. Collect tool arguments by their call ID or tool index, then parse the complete argument object. Surface an `error` event even when the HTTP status was already 200.

`X-Site6-Stream-Mode` reports `incremental` or `buffered`. Requests that need a validated structured result, a hosted tool result or several choices may buffer some or all of the response. Cancel the client’s request to release its active slot and cancel work that can still be stopped.

## Reasoning and usage

Use `thinking: true` or a model’s supported `reasoning_effort` to request reasoning. Chat can include `reasoning_content` and Responses can return a reasoning item. Keep reasoning separate from final answer text. `reasoning_metadata` reports the mode and measured counts when available. `thinking: false` suppresses reasoning output.

Usage in a Chat stream is available on the final usage chunk when `stream_options.include_usage` is enabled. Responses puts final usage on its completed response. A null or missing count means the complete count is unavailable.

## Build a streaming client

This Node.js example streams a Responses request and checks the terminal event before treating the answer as complete. Use the [OpenAI SDK setup](/examples/#set-up-an-sdk) and set `SITE6_API_KEY`.

~~~javascript
import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://site6.eokgg.win/v1",
  apiKey: process.env.SITE6_API_KEY,
});
const controller = new AbortController();
process.once("SIGINT", () => controller.abort());

const stream = await client.responses.create({
  model: process.env.SITE6_MODEL ?? "deepseek-v4.1-flash-1017",
  input: "Explain three ways to cache an API response.",
  stream: true,
  max_output_tokens: 800,
  store: false,
}, { signal: controller.signal });

let terminal;
for await (const event of stream) {
  if (event.type === "response.output_text.delta") process.stdout.write(event.delta);
  if (event.type === "response.completed" || event.type === "response.incomplete") {
    terminal = event.response;
  }
  if (event.type === "response.failed") {
    throw new Error(event.response.error?.message ?? "The model request failed");
  }
  if (event.type === "error") throw new Error(event.message ?? "The stream failed");
}

if (!terminal) throw new Error("The stream ended before a terminal event");
if (terminal.status === "incomplete") {
  console.error("\nPartial answer:", terminal.incomplete_details?.reason);
}
process.stdout.write("\n");
~~~

Distinguish the first response byte, the first text delta and the terminal event in your timings. A heartbeat or early HTTP headers can arrive while the model is still working. If your UI shows tool calls, accumulate each call's argument deltas until its arguments are complete.
