# 推理与思考

> 在 BigModel.io 上控制和读取模型推理：传入 reasoning_effort 或 thinking，读取 reasoning_content 与思考块，并保持多轮对话历史合法。

网页版: https://bigmodel.io/zh-cn/docs/reasoning · 更新于 2026-10-18

推理模型会先思考再作答。根据接口不同，你可以用 `reasoning_effort` 或 `thinking` 控制思考，并以 `reasoning_content` 或思考块的形式拿到思考内容。

| 接口 | 控制参数 | 响应中的思考内容 |
|---|---|---|
| `/v1/chat/completions` | `reasoning_effort`，或厂商自有字段（如 `thinking`） | `message.reasoning_content`（流式：`delta.reasoning_content`） |
| `/v1/messages` | `thinking: {"type": "enabled", "budget_tokens": N}`、`output_config.effort` | `thinking` 内容块 |
| `/v1/responses` | `reasoning: {"effort": "..."}` | reasoning 输出项 |

模型接受哪些参数因模型而异：有的始终推理，有的需要参数开启，有的不推理。请查阅所用模型厂商的文档；模型目录中的推理模型可能带有「推理」标签。

## Chat Completions：读取 reasoning_content

在 GLM、DeepSeek 分组上，请求体原样透传，`reasoning_effort`、`thinking` 等厂商字段会按你发送的内容到达模型，响应中的 `reasoning_content` 也原样返回。在转换协议的分组上，`reasoning_effort` 会保留，其他厂商字段会被丢弃。

```python [Python]
import os
from openai import OpenAI

client = OpenAI(base_url="https://bigmodel.io/v1", api_key=os.environ["BIGMODEL_API_KEY"])

completion = client.chat.completions.create(
    model="deepseek-v4-pro",
    messages=[{"role": "user", "content": "1001 是质数吗？"}],
    reasoning_effort="high",  # 仅在模型支持时传
)
msg = completion.choices[0].message
print("思考：", getattr(msg, "reasoning_content", None))
print("回答：", msg.content)
```

```js [Node.js]
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://bigmodel.io/v1", apiKey: process.env.BIGMODEL_API_KEY });

const completion = await client.chat.completions.create({
  model: "deepseek-v4-pro",
  messages: [{ role: "user", content: "1001 是质数吗？" }],
  reasoning_effort: "high", // 仅在模型支持时传
});
const msg = completion.choices[0].message;
console.log("思考：", msg.reasoning_content);
console.log("回答：", msg.content);
```

```bash [cURL]
curl https://bigmodel.io/v1/chat/completions \
  -H "Authorization: Bearer $BIGMODEL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"deepseek-v4-pro","messages":[{"role":"user","content":"1001 是质数吗？"}]}'
```

`reasoning_content` 不在 OpenAI 的 schema 中，SDK 的类型定义里没有它。OpenAI SDK 会保留未知字段，所以可以像上面这样读取。流式输出时，与 `delta.content` 一起读取 `chunk.choices[0].delta.reasoning_content`。

要发送 SDK 不认识的厂商字段，Python 中使用 `extra_body`，例如 `extra_body={"thinking": {"type": "enabled"}}`。字段结构请以厂商文档为准。

### LangChain

LangChain 的 `ChatOpenAI` 不会返回 `reasoning_content`，思考内容会丢失。如需思考内容，请像上面那样直接调用 OpenAI SDK，或在支持 Messages 的分组上用 `ChatAnthropic` 走 Messages API。见 [LangChain](https://bigmodel.io/zh-cn/docs/langchain)。

## Messages API：思考块

在支持 `/v1/messages` 的分组上，使用 Anthropic 的 `thinking` 参数，`budget_tokens` 必须小于 `max_tokens`。

```python [Python]
import os
from anthropic import Anthropic

client = Anthropic(base_url="https://bigmodel.io", api_key=os.environ["BIGMODEL_API_KEY"])

message = client.messages.create(
    model="glm-5.3",
    max_tokens=4096,
    thinking={"type": "enabled", "budget_tokens": 2048},
    messages=[{"role": "user", "content": "1001 是质数吗？"}],
)
for block in message.content:
    if block.type == "thinking":
        print("思考：", block.thinking)
    elif block.type == "text":
        print("回答：", block.text)
```

在 Anthropic 平台分组上，Responses 的 `reasoning.effort` 会被换算成思考预算。

## 多轮对话

- **Chat Completions。** 各厂商对是否可以回传之前轮次的 `reasoning_content` 规定不同：有的会拒绝，有的在工具调用期间要求回传。请遵循厂商规则；拿不准时只回传 `content`。
- **Messages。** 之前的 assistant 内容要原样回传，包括思考块及其签名。修改、重排或删掉部分思考块会导致 409 `malformed_session_state`。

## 费用

思考会让回复更慢、更长。思考 token 计入供应方报告的用量中。[用量](https://bigmodel.io/usage)页面会显示每条请求的推理强度。

推理 token 按供应方在用量字段中报告的数量计费：网关按供应方返回的输出 token（`completion_tokens` 或 `output_tokens`）以输出价扣费，不会另行统计思考内容。

## 验证配置

用推理模型运行 Python Chat Completions 示例。`思考：` 后应输出文字而不是 `None`。如果是 `None`，说明模型不返回思考内容，或需要参数开启。

## 常见问题排查

**`reasoning_content` 始终为空或 `None`。** 模型不推理、需要厂商参数（如 `thinking`）开启，或你用的库丢弃了该字段（LangChain `ChatOpenAI`）。先用 OpenAI SDK 测试。

**与 `reasoning_effort` 或 `thinking` 相关的 400。** 模型不接受该参数或取值。去掉它，或使用厂商文档中的取值。

**409 `malformed_session_state`。** Messages 历史中的思考块被改动，或工具 id 对不上。原样回传 assistant 消息，或开启新对话。

**回复被截断且 `content` 为空。** 模型把 `max_tokens` 都用在了思考上。调大 `max_tokens`，或降低推理强度或 `budget_tokens`。

**`/v1/messages` 返回 404。** Key 的分组只支持 Chat Completions，请在 `/v1/chat/completions` 上读取 `reasoning_content`。

## 相关文档

- [流式输出](https://bigmodel.io/zh-cn/docs/streaming)：`reasoning_content` 增量。
- [工具调用](https://bigmodel.io/zh-cn/docs/tool-calling)：配合推理模型使用工具。
- [Chat Completions API 参考](https://bigmodel.io/zh-cn/docs/api-chat-completions)与 [Messages API 参考](https://bigmodel.io/zh-cn/docs/api-messages)。
