Anthropic (Claude)

Anthropic 打造了 Claude 模型系列,並透過 API 提供存取。在 OpenClaw 中,你可以使用 API 金鑰或 setup-token 進行驗證。

方式 A:Anthropic API 金鑰

適用情境: 標準 API 存取與依用量計費。 請在 Anthropic Console 建立你的 API 金鑰。

CLI 設定

openclaw onboard
# 選擇:Anthropic API key

# 或以非互動方式執行
openclaw onboard --anthropic-api-key "$ANTHROPIC_API_KEY"

設定範例

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

思考模式預設值(Claude 4.6)

  • Anthropic Claude 4.6 模型在 OpenClaw 中預設使用 adaptive 思考模式(未明確設定時)。
  • 你可以在每則訊息中覆寫(/think:<level>),或在模型參數中設定: agents.defaults.models["anthropic/<model>"].params.thinking
  • 相關 Anthropic 文件:

快速模式(Anthropic API)

OpenClaw 的 /fast 切換功能也支援直接的 Anthropic API 金鑰流量。

  • /fast on 對應 service_tier: "auto"
  • /fast off 對應 service_tier: "standard_only"
  • 設定預設值:
{
  agents: {
    defaults: {
      models: {
        "anthropic/claude-sonnet-4-5": {
          params: { fastMode: true },
        },
      },
    },
  },
}

重要限制:

  • 此功能僅限 API 金鑰。Anthropic setup-token / OAuth 驗證不支援 OpenClaw 的快速模式層級注入。
  • OpenClaw 只會在直接對 api.anthropic.com 的請求中注入 Anthropic 服務層級。如果你將 anthropic/* 路由透過代理或閘道,/fast 不會調整 service_tier
  • Anthropic 會在回應的 usage.service_tier 中回報實際使用的層級。帳號若無 Priority Tier 容量,service_tier: "auto" 仍可能解析為 standard

Prompt 快取(Anthropic API)

OpenClaw 支援 Anthropic 的 prompt 快取功能。此功能僅限 API;訂閱驗證不支援快取設定。

設定方式

在模型設定中使用 cacheRetention 參數:

快取時間說明
none不快取停用 prompt 快取
short5 分鐘API 金鑰驗證的預設值
long1 小時延長快取(需要 beta 旗標)
{
  agents: {
    defaults: {
      models: {
        "anthropic/claude-opus-4-6": {
          params: { cacheRetention: "long" },
        },
      },
    },
  },
}

預設值

使用 Anthropic API 金鑰驗證時,OpenClaw 會自動為所有 Anthropic 模型套用 cacheRetention: "short"(5 分鐘快取)。你可以在設定中明確指定 cacheRetention 來覆寫此預設值。

針對個別 Agent 覆寫 cacheRetention

以模型層級的參數作為基準,再透過 agents.list[].params 為特定 agent 覆寫。

{
  agents: {
    defaults: {
      model: { primary: "anthropic/claude-opus-4-6" },
      models: {
        "anthropic/claude-opus-4-6": {
          params: { cacheRetention: "long" }, // 大多數 agent 的基準值
        },
      },
    },
    list: [
      { id: "research", default: true },
      { id: "alerts", params: { cacheRetention: "none" } }, // 僅此 agent 覆寫
    ],
  },
}

快取相關參數的合併順序:

  1. agents.defaults.models["provider/model"].params
  2. agents.list[].params(依 id 比對,按鍵覆寫)

這讓你能讓某個 agent 保持長效快取,同時讓另一個使用相同模型的 agent 停用快取,以避免在高頻/低重用流量上產生寫入成本。

Bedrock Claude 注意事項

  • Bedrock 上的 Anthropic Claude 模型(amazon-bedrock/*anthropic.claude*)在設定後支援 cacheRetention 直通。
  • 非 Anthropic 的 Bedrock 模型在執行時會被強制設為 cacheRetention: "none"
  • 當未明確設定值時,Anthropic API 金鑰的智慧預設值也會為 Claude-on-Bedrock 模型參考設定 cacheRetention: "short"

舊版參數

較舊的 cacheControlTtl 參數仍支援向後相容:

  • "5m" 對應 short
  • "1h" 對應 long

建議遷移至新的 cacheRetention 參數。

OpenClaw 會在 Anthropic API 請求中包含 extended-cache-ttl-2025-04-11 beta 旗標;如果你覆寫了供應商標頭,請保留此旗標(請參閱 /gateway/configuration)。

1M 上下文視窗(Anthropic beta)

Anthropic 的 1M 上下文視窗受 beta 限制。在 OpenClaw 中,可為支援的 Opus/Sonnet 模型設定 params.context1m: true 來啟用。

{
  agents: {
    defaults: {
      models: {
        "anthropic/claude-opus-4-6": {
          params: { context1m: true },
        },
      },
    },
  },
}

OpenClaw 會將此對應為 Anthropic 請求中的 anthropic-beta: context-1m-2025-08-07

只有在明確將 params.context1m 設為 true 時才會啟用。

前提:Anthropic 必須在該憑證上允許長上下文使用(通常需要 API 金鑰計費,或啟用了額外用量的訂閱帳號)。否則 Anthropic 會回傳: HTTP 429: rate_limit_error: Extra usage is required for long context requests

注意:Anthropic 目前會拒絕使用 OAuth/訂閱 token(sk-ant-oat-*)的 context-1m-* beta 請求。OpenClaw 會自動為 OAuth 驗證跳過 context1m beta 標頭,並保留必要的 OAuth beta。

方式 B:Claude setup-token

適用情境: 使用你的 Claude 訂閱。

取得 setup-token

Setup-token 由 Claude Code CLI 產生,而非 Anthropic Console。你可以在任何機器上執行:

claude setup-token

將 token 貼入 OpenClaw(精靈中選擇:Anthropic token (paste setup-token)),或在閘道主機上執行:

openclaw models auth setup-token --provider anthropic

如果你在另一台機器上產生了 token,可以用貼上的方式:

openclaw models auth paste-token --provider anthropic

CLI 設定(setup-token)

# 在新手引導過程中貼上 setup-token
openclaw onboard --auth-choice setup-token

設定範例(setup-token)

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

注意事項

疑難排解

401 錯誤 / token 突然失效

  • Claude 訂閱驗證可能過期或被撤銷。重新執行 claude setup-token 並貼到閘道主機
  • 如果 Claude CLI 登入在另一台機器上,請在閘道主機上使用 openclaw models auth paste-token --provider anthropic

No API key found for provider “anthropic”

  • 驗證是以 agent 為單位的。新 agent 不會繼承主 agent 的金鑰。
  • 針對該 agent 重新執行新手引導,或在閘道主機上貼上 setup-token / API 金鑰, 然後用 openclaw models status 驗證。

No credentials found for profile anthropic:default

  • 執行 openclaw models status 查看目前使用的驗證設定檔。
  • 重新執行新手引導,或為該設定檔貼上 setup-token / API 金鑰。

No available auth profile (all in cooldown/unavailable)

  • 檢查 openclaw models status --json 中的 auth.unusableProfiles
  • 新增另一個 Anthropic 設定檔或等待冷卻期結束。

更多資訊:/gateway/troubleshooting/help/faq