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

# 推理模型（Reasoning）

> 了解算秩推理模型的适用场景、thinking_budget、reasoning 字段、流式调用、输出预算和计费注意事项。

推理（Reasoning）模型面向更复杂的任务场景，例如数学解题、代码生成、逻辑分析和多步推理。它们提供兼容 OpenAI 的接口，并支持流式输出，便于接入现有应用。

代表性模型包括：

* `Qwen/Qwen3-32B`
* `Qwen/Qwen3.6-27B`
* `Qwen/Qwen3.5-397B-A17B`
* `kimi-k2.6`

完整模型列表和价格请以 [模型广场](https://console.siflow.cn/model-inference/models) 为准。

## 核心能力

* **结构化思考**：通过思维链（CoT）把复杂问题拆解成更小、更清晰的步骤。
* **知识融合**：将通用知识与领域特定上下文相结合，以提升覆盖面与准确性。
* **自我纠错**：在生成过程中加入校验和反思过程，提升结果可靠性。
* **多模态处理**：部分模型支持混合输入（文本、代码、公式等）；具体请参见相应的模型详情。

## 使用说明

* 在投入生产前，请查看每个模型的上下文限制、是否支持 `thinking_budget`、定价以及并发限制。
* 部分模型会在单独的 `reasoning` 字段中返回推理内容；但并非所有模型都会返回 `reasoning` 字段。部分通用对话模型，例如 `deepseek-ai/DeepSeek-V4-Pro`，会直接给出答案，而不会单独暴露推理过程。

## 关键参数

### 参数说明

* **请求参数**

  * `thinking_budget`：用于内部推理的 token 预算，可用来平衡推理深度与响应延迟。
  * `max_completion_tokens`：面向用户可见输出的 token 上限，用于避免响应过长。

* **上下文**

  不同模型支持的最大上下文长度可通过 [模型广场](https://console.siflow.cn/model-inference/models) 查看。

* **响应字段**

  * `reasoning`：思维链内容，在响应对象中与 `content` 同级。
  * `content`：面向最终用户展示的最终答案内容。

### 使用建议

* `thinking_budget` 是一个提示（hint），而不是硬性限制。有些模型会参考它来调整推理量，也有些模型可能只部分采用，甚至忽略这一设置，因此不要把它当作精确限额使用。
* 推理过程和最终答案共享 `max_completion_tokens` 预算。如果 `max_completion_tokens` 设置过小，推理内容可能会耗尽预算，导致 `content` 为空，且 `finish_reason: length`。建议适当放宽 `max_completion_tokens`，或通过 `thinking_budget` 控制推理长度，为最终答案预留足够空间。
* 如果输出超过 `max_completion_tokens`，或总输入超过 `context_length`，响应将被截断，且 `finish_reason` 将被设置为 `length`。

## 注意事项

* **流式与非流式**：需要长输出或实时反馈时使用流式；需要一次性拿到完整结果时使用非流式。
* **延迟与稳定性**：可通过调整 `thinking_budget`、`max_completion_tokens` 和客户端超时时间，降低 504 错误和请求超时的风险。
* **配额与并发**：结合定价，参考 [模型广场](https://console.siflow.cn/model-inference/models) 配置限流策略；必要时在客户端实现指数退避。

## 计费

* **公式**：总费用 =（输入 token 数 × 输入单价）+（输出 token 数 × 输出单价）。
* **价格**：各模型价格请在 [模型广场](https://console.siflow.cn/model-inference/models) 的模型详情页查看。

## 调用示例

示例使用环境变量读取 API Key，避免将密钥写入代码。

```bash theme={null}
export API_KEY="YOUR_API_KEY"
```

### 流式请求

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

BASE_URL = "https://api.siflow.cn/model-api"
API_KEY = os.environ["API_KEY"]

client = OpenAI(base_url=BASE_URL, api_key=API_KEY)

messages = [{"role": "user", "content": "Which is the largest anime website in China?"}]

content = ""
reasoning = ""

resp = client.chat.completions.create(
    model="Qwen/Qwen3-32B",
    messages=messages,
    stream=True,
    max_completion_tokens=4096,
    extra_body={"thinking_budget": 1024},
)

for chunk in resp:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if getattr(delta, "content", None):
        content += delta.content
        print(delta.content, end="", flush=True)
    if getattr(delta, "reasoning", None):
        reasoning += delta.reasoning

# Optionally persist the reasoning text for debugging/audit

# Round 2 (continue the conversation)
messages.append({"role": "assistant", "content": content})
messages.append({"role": "user", "content": "Continue"})

resp2 = client.chat.completions.create(
    model="Qwen/Qwen3-32B",
    messages=messages,
    stream=True,
)

for chunk in resp2:
    if not chunk.choices:
        continue
    delta = chunk.choices[0].delta
    if getattr(delta, "content", None):
        print(delta.content, end="", flush=True)
```

### 非流式请求

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

BASE_URL = "https://api.siflow.cn/model-api"
API_KEY = os.environ["API_KEY"]

client = OpenAI(base_url=BASE_URL, api_key=API_KEY)

messages = [{"role": "user", "content": "Which is the largest anime website in China?"}]

resp = client.chat.completions.create(
    model="Qwen/Qwen3.5-397B-A17B",
    messages=messages,
    stream=False,
    max_completion_tokens=4096,
    extra_body={"thinking_budget": 1024},
)

content = resp.choices[0].message.content
reasoning = getattr(resp.choices[0].message, "reasoning", "")
finish_reason = resp.choices[0].finish_reason

# Round 2 (continue the conversation)
messages.append({"role": "assistant", "content": content})
messages.append({"role": "user", "content": "Continue"})

resp2 = client.chat.completions.create(
    model="Qwen/Qwen3.5-397B-A17B",
    messages=messages,
    stream=False,
)

print(resp2.choices[0].message.content)
```

## 常见问题

* **如何处理超长文本？**
  调整 `max_completion_tokens` 并启用 `stream=True` 以降低超时风险；上下文限制因模型而异，具体请以 [模型广场](https://console.siflow.cn/model-inference/models) 为准。

* **如果思维链过长而被截断怎么办？**
  降低 `thinking_budget` 或增大客户端超时时间，并确保将 `max_completion_tokens` 设置为一个合理的值。

* **为什么我看不到 `reasoning`？**
  只有部分推理模型会返回该字段；请参见各模型的文档。
