Anthropic (Claude)

适用范围

在以下情况使用此页面:

  • 你想在 Moltbot 中使用 Anthropic 模型
  • 你想使用 setup-token 或 Claude Code CLI 认证而不是 API 密钥

Anthropic 构建 Claude 模型系列并通过 API 提供访问。 在 Moltbot 中,你可以使用 API 密钥进行身份验证,或重用 Claude Code CLI 凭证(setup-token 或 OAuth)。

选项 A:Anthropic API 密钥

最适合: 标准 API 访问和基于使用的计费。 在 Anthropic Console 中创建你的 API 密钥。

CLI 设置

moltbot onboard
# 选择:Anthropic API key

# 或非交互式
moltbot onboard --anthropic-api-key "$ANTHROPIC_API_KEY"

配置片段

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

提示缓存(Anthropic API)

除非你设置,否则 Moltbot 不会覆盖 Anthropic 的默认缓存 TTL。 这仅限 API;Claude Code CLI OAuth 会忽略 TTL 设置。

要为每个模型设置 TTL,请在模型 params 中使用 cacheControlTtl

{
  agents: {
    defaults: {
      models: {
        "anthropic/claude-opus-4-5": {
          params: { cacheControlTtl: "5m" } // 或 "1h"
        }
      }
    }
  }
}

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

选项 B:Claude Code CLI(setup-token 或 OAuth)

最适合: 使用你的 Claude 订阅或现有的 Claude Code CLI 登录。

从哪里获取 setup-token

Setup-token 由 Claude Code CLI 创建,而不是 Anthropic Console。你可以在任何机器上运行此命令:

claude setup-token

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

moltbot models auth setup-token --provider anthropic

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

moltbot models auth paste-token --provider anthropic

CLI 设置

# 如果已登录,重用 Claude Code CLI OAuth 凭证
moltbot onboard --auth-choice claude-cli

配置片段

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

说明

  • 使用 claude setup-token 生成 setup-token 并粘贴它,或在网关主机上运行 moltbot models auth setup-token
  • 如果你在 Claude 订阅上看到"OAuth token refresh failed …",请使用 setup-token 重新认证或在网关主机上重新同步 Claude Code CLI OAuth。参见 /gateway/troubleshooting#oauth-token-refresh-failed-anthropic-claude-subscription
  • Moltbot 将 auth.profiles["anthropic:claude-cli"].mode 写入为 "oauth",因此配置文件接受 OAuth 和 setup-token 凭证。使用 "token" 的较旧配置在加载时会自动迁移。
  • 认证详细信息 + 重用规则在 /concepts/oauth 中。

故障排除

401 错误 / 令牌突然无效

  • Claude 订阅认证可能过期或被撤销。在网关主机上重新运行 claude setup-token 并粘贴它。
  • 如果 Claude CLI 登录位于不同的机器上,请在网关主机上使用 moltbot models auth paste-token --provider anthropic

未找到提供商 “anthropic” 的 API 密钥

  • 认证是每个代理。新代理不会继承主代理的密钥。
  • 为该代理重新运行配置,或在网关主机上粘贴 setup-token / API 密钥, 然后使用 moltbot models status 验证。

未找到配置文件 anthropic:defaultanthropic:claude-cli 的凭证

  • 运行 moltbot models status 以查看哪个认证配置文件处于活动状态。
  • 重新运行配置,或为该配置文件粘贴 setup-token / API 密钥。

没有可用的认证配置文件(所有配置文件都在冷却/不可用)

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

更多:/gateway/troubleshooting/help/faq