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

# 推理 Token

> 理解並使用 DeepSeek R1、Claude 延伸思考和 o4-mini 等模型中的推理（思考）Token。

某些模型在生成最終回應之前會進行內部鏈式思考推理。這些推理步驟消耗的 Token 稱為**推理 Token**——它們會影響成本和延遲，但預設情況下使用者不可見。

## 支援的模型

| 模型                            | 推理支援   |
| ----------------------------- | ------ |
| `openai/o4-mini`              | 始終開啟推理 |
| `openai/o3`                   | 始終開啟推理 |
| `anthropic/claude-sonnet-4-6` | 可選延伸思考 |
| `anthropic/claude-opus-4-6`   | 可選延伸思考 |
| `deepseek/deepseek-r1`        | 始終開啟推理 |
| `google/gemini-2.5-pro`       | 可選思考模式 |
| `google/gemini-2.5-flash`     | 可選思考模式 |

## 推理 Token 在使用量中的體現

推理 Token 作為 `completion_tokens_details` 的一部分，在 `usage` 物件中回報：

```json theme={null}
{
  "usage": {
    "prompt_tokens": 150,
    "completion_tokens": 520,
    "total_tokens": 670,
    "completion_tokens_details": {
      "reasoning_tokens": 400
    }
  }
}
```

在此範例中，520 個補全 Token 中有 400 個用於內部推理。只有剩餘的 120 個 Token 出現在可見回應中。

## 推理 Token 的計費

推理 Token 按該模型的**補全 Token 費率**計費。它們被納入 `completion_tokens` 進行計費——詳細分類僅供參考。

ARouter 不對上游服務商的推理 Token 計數進行任何修改，直接透傳。

## 控制推理行為

### OpenAI o 系列（o4-mini、o3）

o 系列模型的推理始終開啟。使用 `reasoning_effort` 控制模型推理的程度：

```json theme={null}
{
  "model": "openai/o4-mini",
  "reasoning_effort": "high",
  "messages": [...]
}
```

有效值：`"low"`、`"medium"`、`"high"`。力度越高 = 推理 Token 越多 = 品質和成本越高。

### Anthropic 延伸思考

透過在請求中傳遞 `thinking` 來啟用延伸思考：

```python theme={null}
import anthropic

client = anthropic.Anthropic(
    api_key="your-arouter-key",
    base_url="https://api.arouter.ai/anthropic",
)

response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=16000,
    thinking={
        "type": "enabled",
        "budget_tokens": 10000,
    },
    messages=[{"role": "user", "content": "請一步步解決：..."}],
)
```

`budget_tokens` 限制了可用於思考的最大 Token 數。思考內容作為回應中的獨立區塊回傳。

### DeepSeek R1

DeepSeek R1 的推理始終開啟。該模型在常規 `content` 旁邊回傳一個 `reasoning_content` 欄位：

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

client = OpenAI(
    api_key="your-arouter-key",
    base_url="https://api.arouter.ai/v1",
)

response = client.chat.completions.create(
    model="deepseek/deepseek-r1",
    messages=[{"role": "user", "content": "證明 √2 是無理數。"}],
)

# 推理內容（如果服務商暴露）
print(response.choices[0].message.reasoning_content)
# 最終答案
print(response.choices[0].message.content)
```

### Google Gemini 思考

透過 `thinking` 參數為 Gemini 2.5 模型啟用思考：

```python theme={null}
response = client.chat.completions.create(
    model="google/gemini-2.5-flash",
    messages=[...],
    extra_body={
        "thinking": {
            "type": "enabled",
            "budget_tokens": 5000
        }
    }
)
```

## 活動匯出與推理 Token

[活動匯出](/zh-Hant/guides/administration/activity-export)包含推理 Token 的詳細資料，便於準確追蹤其對總成本的貢獻。在匯出摘要中，推理 Token 包含在補全 Token 中。

## 最佳實踐

* **從 `"low"` 或 `"medium"` 力度開始** 使用 o 系列模型，除非您需要最高推理品質。這可以顯著降低成本和延遲。
* **為 Anthropic 和 Gemini 思考模型設定 `budget_tokens` 上限**，以避免在複雜查詢上產生意外的大額帳單。
* **在活動記錄中監控推理 Token 佔比**。推理 Token 與輸出 Token 的高比例對複雜任務是正常的，但可能表示模型在簡單查詢上過度思考。
* **不要為了節省成本而停用推理**，對於真正需要多步推理的任務——輸出品質會顯著下降。
