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

# 创建聊天补全

> 基于当前生产行为，使用 POST /v1/chat/completions 创建聊天补全

> 更新日期：2026-06-06

# 创建聊天补全

```
POST /v1/chat/completions
```

根据输入的消息列表生成模型回复，支持非流式和流式响应。

本文页只写入 `2026-03-23` 已在 Crazyrouter 生产环境复核过的通用行为。

## 认证

```text theme={null}
Authorization: Bearer YOUR_API_KEY
```

## 核心参数

| 参数                | 类型             | 必填 | 说明                                                  |
| ----------------- | -------------- | -- | --------------------------------------------------- |
| `model`           | string         | 是  | 模型名称，如 `gpt-5.5`、`claude-opus-4-8`、`gemini-3.1-pro` |
| `messages`        | array          | 是  | 消息列表                                                |
| `stream`          | boolean        | 否  | 是否启用流式输出                                            |
| `max_tokens`      | integer        | 否  | 最大输出 Token 数                                        |
| `temperature`     | number         | 否  | 采样温度                                                |
| `response_format` | object         | 否  | 结构化输出格式约束                                           |
| `tools`           | array          | 否  | 工具列表                                                |
| `tool_choice`     | string\|object | 否  | 工具选择策略                                              |
| `stream_options`  | object         | 否  | 流式附加选项，例如 `include_usage`                           |

<Note>
  不同模型对可选参数的支持程度不同。写客户端时应优先依赖通用字段，不要假设所有模型都完整支持同一组高级参数。
</Note>

***

## 非流式请求

```bash cURL theme={null}
curl https://api.crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.5",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Explain artificial intelligence in one sentence."
      }
    ],
    "max_tokens": 64
  }'
```

当前生产返回的典型结构：

```json theme={null}
{
  "object": "chat.completion",
  "model": "gpt-5.5",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "...",
        "reasoning_content": null,
        "tool_calls": null
      },
      "finish_reason": "stop"
    }
  ]
}
```

### 需要特别注意

* `message.content` 是最稳定的最终文本字段
* `message.tool_calls` 只在工具调用时出现
* `message.reasoning_content` 可能存在键，但当前不应视为稳定必有内容

***

## 流式请求

```bash cURL theme={null}
curl https://api.crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.5",
    "messages": [
      {
        "role": "user",
        "content": "Explain AI in one short sentence."
      }
    ],
    "stream": true,
    "stream_options": {
      "include_usage": true
    },
    "max_tokens": 64
  }'
```

本轮生产复核确认：

* 流式返回 `chat.completion.chunk`
* SSE 以 `data: ...` 形式逐块发送
* 流结束时仍是 `data: [DONE]`

当前收到的 SSE 片段形态类似：

```text theme={null}
data: {"object":"chat.completion.chunk","model":"gpt-5.5","choices":[{"delta":{"role":"assistant","content":"AI","reasoning_content":null,"tool_calls":null}}]}
```

```text theme={null}
data: [DONE]
```

***

## Python 示例

```python Python theme={null}
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.crazyrouter.com/v1"
)

stream = client.chat.completions.create(
    model="gpt-5.5",
    messages=[
        {"role": "user", "content": "Explain AI in one short sentence."}
    ],
    stream=True,
    stream_options={"include_usage": True},
    max_tokens=64
)

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

***

## 当前建议

* 普通文本对话：`/v1/chat/completions`
* 需要 reasoning 摘要或 OpenAI 风格 web search：优先 `/v1/responses`
* 需要更复杂的能力判定时，先看对应能力页，不要只看本页

相关页面：

* [聊天完成对象](/chat/openai/overview)
* [推理模型](/chat/openai/reasoning)
* [Web 搜索](/chat/openai/web-search)
