Skip to content

Anthropic (Claude)

Anthropic 构建了 Claude 模型系列,并通过 API 提供访问。 在 OpenClaw 中,您可以使用 API 密钥或 setup-token 进行身份验证。

选项 A:Anthropic API 密钥

最适合: 标准 API 访问和基于使用量的计费。 在 Anthropic 控制台中创建您的 API 密钥。

CLI 设置

bash
openclaw onboard
# choose: Anthropic API key

# or non-interactive
openclaw onboard --anthropic-api-key "$ANTHROPIC_API_KEY"

配置片段

json5
{
  env: { ANTHROPIC_API_KEY: "sk-ant-..." },
  agents: { defaults: { model: { primary: "anthropic/claude-opus-4-5" } } },
}

提示词缓存(Anthropic API)

OpenClaw 支持 Anthropic 的提示词缓存功能。这仅限 API;订阅身份验证不支持缓存设置。

配置

在您的模型配置中使用 cacheRetention 参数:

缓存持续时间描述
none无缓存禁用提示词缓存
short5 分钟API 密钥身份验证的默认值
long1 小时扩展缓存(需要 beta 标志)
json5
{
  agents: {
    defaults: {
      models: {
        "anthropic/claude-opus-4-5": {
          params: { cacheRetention: "long" },
        },
      },
    },
  },
}

默认值

使用 Anthropic API 密钥身份验证时,OpenClaw 会自动为所有 Anthropic 模型应用 cacheRetention: "short"(5 分钟缓存)。您可以通过在配置中显式设置 cacheRetention 来覆盖此设置。

旧参数

仍支持较旧的 cacheControlTtl 参数以保持向后兼容性:

  • "5m" 映射到 short
  • "1h" 映射到 long

我们建议迁移到新的 cacheRetention 参数。

OpenClaw 为 Anthropic API 请求包含了 extended-cache-ttl-2025-04-11 beta 标志;如果您覆盖提供商头部,请保留它(参见 /gateway/configuration)。

选项 B:Claude setup-token

最适合: 使用您的 Claude 订阅。

在哪里获取 setup-token

Setup-token 由 Claude Code CLI 创建,而不是 Anthropic 控制台。您可以在任何机器上运行:

bash
claude setup-token

将令牌粘贴到 OpenClaw(向导:Anthropic token (paste setup-token)),或在网关主机上运行:

bash
openclaw models auth setup-token --provider anthropic

如果您在不同的机器上生成了令牌,请粘贴它:

bash
openclaw models auth paste-token --provider anthropic

CLI 设置

bash
# Paste a setup-token during onboarding
openclaw onboard --auth-choice setup-token

配置片段

json5
{
  agents: { defaults: { model: { primary: "anthropic/claude-opus-4-5" } } },
}

注意事项

故障排除

401 错误 / 令牌突然失效

  • Claude 订阅身份验证可能会过期或被撤销。重新运行 claude setup-token 并将其粘贴到网关主机
  • 如果 Claude CLI 登录在不同的机器上,在网关主机上使用 openclaw models auth paste-token --provider anthropic

未找到提供商 "anthropic" 的 API 密钥

  • 身份验证是针对每个代理的。新代理不会继承主代理的密钥。
  • 为该代理重新运行入门设置,或在网关主机上粘贴 setup-token / API 密钥,然后使用 openclaw models status 进行验证。

未找到配置文件 anthropic:default 的凭据

  • 运行 openclaw models status 查看哪个身份验证配置文件处于活动状态。
  • 重新运行入门设置,或为该配置文件粘贴 setup-token / API 密钥。

没有可用的身份验证配置文件(全部处于冷却/不可用状态)

  • 检查 openclaw models status --json 中的 auth.unusableProfiles
  • 添加另一个 Anthropic 配置文件或等待冷却。

更多信息:/gateway/troubleshooting/help/faq