> ## Documentation Index
> Fetch the complete documentation index at: https://docs.qingbo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# 流式输出

> 使用 SSE 实时接收文本生成结果

## 概述

文本生成端点支持 Server-Sent Events (SSE) 流式输出，设置 `stream: true` 即可逐 token 接收结果。

## 支持流式的端点

* `/v1/chat/completions` — OpenAI 兼容
* `/v1/messages` — Claude Messages
* `/v1/responses` — OpenAI Responses API

## 使用示例

<CodeGroup>
  ```python Python theme={"system"}
  from openai import OpenAI

  client = OpenAI(
      base_url="https://www.qingbo.dev/v1",
      api_key="your-api-key"
  )

  stream = client.chat.completions.create(
      model="gpt-4o",
      messages=[{"role": "user", "content": "写一首短诗"}],
      stream=True
  )

  for chunk in stream:
      if chunk.choices[0].delta.content:
          print(chunk.choices[0].delta.content, end="")
  ```

  ```javascript JavaScript theme={"system"}
  import OpenAI from 'openai';

  const client = new OpenAI({
    baseURL: 'https://www.qingbo.dev/v1',
    apiKey: 'your-api-key'
  });

  const stream = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: '写一首短诗' }],
    stream: true
  });

  for await (const chunk of stream) {
    process.stdout.write(chunk.choices[0]?.delta?.content || '');
  }
  ```
</CodeGroup>

## SSE 数据格式

每个 chunk 是一行 `data: {json}` 格式的 SSE 事件：

```
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"你"},"index":0}]}

data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"好"},"index":0}]}

data: [DONE]
```

流结束时会收到 `data: [DONE]`。

## 末帧 usage 与计费（cost）

`[DONE]` 之前的最后一个数据帧携带本次调用的用量与扣费：

```
data: {"id":"chatcmpl-xxx","choices":[],"usage":{"prompt_tokens":12,"completion_tokens":8,"total_tokens":20,"cost":150}}

data: [DONE]
```

* `usage.cost` 是本次调用实际扣除的额度（quota，整数），按调用对账以它为准
* 默认就会下发；显式传 `stream_options: {"include_usage": false}` 时不下发
* 使用 OpenAI SDK 时，末帧的 `chunk.usage` 即为该数据
