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

# Claude Sonnet 5

> 通过 Anyfast 调用 Anthropic 的 Claude Sonnet 5。最新 Sonnet 模型，具备 1M 上下文、自适应思维和强大的编码与智能体能力。

Claude Sonnet 5 是 Anthropic 最新的 Sonnet 模型，通过 Anyfast 以原生 Anthropic Messages API 提供服务。它在保持 Sonnet 级速度的同时，进一步增强了编码、智能体执行、长上下文推理和复杂分析能力，适合生产级助手、开发者工具和多步骤任务工作流。

## 核心能力

* **1M 上下文窗口** — 默认 1M Token，最大输出 Token 为 128K
* **自适应思维** — 默认开启，可通过 `output_config.effort` 控制深度，或使用 `thinking: {"type": "disabled"}` 关闭
* **API 形状不变** — 请求、响应和流式输出与 Claude Sonnet 4.6 保持一致
* **新的 tokenizer** — 相同文本大约会比 Claude Sonnet 4.6 多 30% Token
* **模型 ID** — 使用 `claude-sonnet-5`

## 快速示例

<CodeGroup>
  ```bash cURL theme={null}
  curl https://www.anyfast.ai/v1/messages \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "model": "claude-sonnet-5",
      "max_tokens": 1024,
      "messages": [
        { "role": "user", "content": "写一个 Python 函数合并两个有序数组。" }
      ]
    }'
  ```

  ```python Python theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key="YOUR_API_KEY",
      base_url="https://www.anyfast.ai"
  )

  message = client.messages.create(
      model="claude-sonnet-5",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "写一个 Python 函数合并两个有序数组。"}
      ]
  )

  print(message.content[0].text)
  ```

  ```python 流式输出 theme={null}
  import anthropic

  client = anthropic.Anthropic(
      api_key="YOUR_API_KEY",
      base_url="https://www.anyfast.ai"
  )

  with client.messages.stream(
      model="claude-sonnet-5",
      max_tokens=1024,
      messages=[
          {"role": "user", "content": "解释异步编程和同步编程的区别。"}
      ],
  ) as stream:
      for text in stream.text_stream:
          print(text, end="")
  ```
</CodeGroup>

## 参数说明

| 参数               | 类型      | 必填 | 说明                                                                                  |
| ---------------- | ------- | -- | ----------------------------------------------------------------------------------- |
| `model`          | string  | 是  | 固定为 `claude-sonnet-5`                                                               |
| `messages`       | array   | 是  | `{ role, content }` 对象数组                                                            |
| `max_tokens`     | integer | 是  | 最大生成 Token 数。Claude Sonnet 5 最多支持 128K 输出 Token。                                    |
| `output_config`  | object  | 否  | 使用 `{"effort":"low" \| "medium" \| "high" \| "xhigh" \| "max"}` 控制自适应思维深度。默认 `high` |
| `thinking`       | object  | 否  | 省略时使用默认自适应思维，或使用 `{"type":"disabled"}` 关闭思维。                                        |
| `stream`         | boolean | 否  | 开启 SSE 流式传输，默认 `false`                                                              |
| `stop_sequences` | array   | 否  | 触发停止生成的序列                                                                           |

<Note>
  Claude Sonnet 5 默认使用自适应思维。请用 `output_config.effort` 调整推理深度（`low`、`medium`、`high`、`xhigh` 或 `max`）。手动扩展思维（`thinking: {type: "enabled", budget_tokens: N}`）会返回 400 错误，`temperature`、`top_p`、`top_k` 设为非默认值也会返回 400 错误。要关闭思维时，请使用 `thinking: {type: "disabled"}`。
</Note>

<Note>
  Claude Sonnet 5 使用了新的 tokenizer。相同文本大约会比 Claude Sonnet 4.6 多 30% Token，所以迁移前要重新统计提示词和 `max_tokens` 预算。
</Note>

<Note>
  涉及被禁止或高风险网络安全主题的请求可能会被拒绝。被拒绝时会返回 `stop_reason: "refusal"`。
</Note>

<Card title="API 参考" icon="code" href="/zh/api-reference/model-api/anthropic/claude-sonnet-5">
  查看 Claude Sonnet 5 的交互式 API Playground。
</Card>

<script src="/feedback.js" />
