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

# Claude Code

> 将 Claude Code 指向 FlexInference，并使用带有截止日期的代理密钥。

Claude Code 使用 Anthropic Messages API。我们在 `POST /v1/messages` 运行该 API，因此一个基础 URL 和一个密钥即可完成整个集成。

Claude Code 没有设置 `start_within` 的地方，因此截止日期会附加到密钥上。请先创建一个密钥（参见[代理密钥](/zh/agent-keys)）。

在托管密钥上，不进行流式传输的情况下，持续时间在三到十分钟的窗口内与 Claude 的更便宜层级竞争。交互式会话会进行流式传输，因此请在此处选择一个层级。密钥持续时间在这两种情况下都有效，因为它会回退到某个层级而不是失败。参见[Claude 持续时间需要更长的窗口](/zh/deadline-routing)。

在开始之前，您的组织必须能够运行 Anthropic。对于您自己的密钥，这意味着在**提供商密钥**选项卡上有一个 Anthropic 提供商密钥，因为我们不转售推理服务。对于[托管密钥](/zh/billing)，这意味着 Anthropic 已启用并获得资金。

## 使用代理进行设置

打开下面的代码块并将其复制到任何编码代理中。该提示永远不会要求您的 API 密钥：代理会配置所有其他内容，然后打印一行 `export` 命令供您自行运行。

<Accordion title="复制代理设置提示">
  ```text theme={null}
  Configure Claude Code to send its requests to FlexInference, which serves the Anthropic Messages API.

  You will never see or handle my API key. Claude Code reads it from the `ANTHROPIC_AUTH_TOKEN` environment variable, and I export that myself. Do not ask me for the key, do not write it into any file, and do not read it from my environment.

  Ask me whether to configure this globally or for one project, then edit `~/.claude/settings.json` or `.claude/settings.json` accordingly. Create the file if it does not exist, and preserve every setting already in it.

  Add this `env` block, merging into any existing `env` rather than replacing it. Note that it carries no credential:

  {
    "env": {
      "ANTHROPIC_BASE_URL": "https://api.flexinference.com",
      "ANTHROPIC_MODEL": "claude-opus-5",
      "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5",
      "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"
    }
  }

  Then print this line for me to run in the shell I start Claude Code from. Do not run it and do not ask for the value:

  export ANTHROPIC_AUTH_TOKEN=<paste your key here>

  Rules that matter, do not deviate:
  - `ANTHROPIC_BASE_URL` must NOT end in `/v1`. Claude Code appends `/v1/messages` itself.
  - The credential must arrive as `ANTHROPIC_AUTH_TOKEN`, never `ANTHROPIC_API_KEY`. The first sends `Authorization: Bearer`, which FlexInference requires. The second sends `x-api-key`, which it refuses with `401 missing_api_key`.
  - Both model variables must be `claude-*` slugs. Claude Code sends Anthropic-native fields such as `output_config` on every request, and FlexInference refuses to mistranslate them onto a non-Claude model.
  - `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` must be `"1"`. Without it Claude Code sends experimental beta fields such as `context_management`, FlexInference does not forward the `anthropic-beta` headers they need, and every request fails with `400 context_management: Extra inputs are not permitted`.
  - Do not add a retry setting. Claude Code retries on its own and my key may carry its own policy.

  Finally, tell me that Anthropic must be servable on my FlexInference organization. On BYOK that means an Anthropic provider key on the API, then Provider keys tab. On Managed Keys it means Anthropic enabled and funded. Then start a new Claude Code session and report what you changed.
  ```
</Accordion>

## 配置 Claude Code

1. 创建或打开 `~/.claude/settings.json`。项目文件 `.claude/settings.json` 的工作方式相同。
2. 添加一个 `env` 块，指定路由器、您的密钥和两个模型。

```json theme={null}
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.flexinference.com",
    "ANTHROPIC_AUTH_TOKEN": "flex_live_...",
    "ANTHROPIC_MODEL": "claude-opus-5",
    "ANTHROPIC_SMALL_FAST_MODEL": "claude-haiku-4-5",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"
  }
}
```

3. 启动 Claude Code。

```bash theme={null}
claude
```

或者，在您的 shell 中导出相同的变量，以避免将密钥写入文件。

```bash theme={null}
export ANTHROPIC_BASE_URL=https://api.flexinference.com
export ANTHROPIC_AUTH_TOKEN=flex_live_...
export CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1
```

以下四条规则可确保其正常工作。

**省略 `/v1` 后缀。** Claude Code 会自行将 `/v1/messages` 附加到基础 URL。

**使用 `ANTHROPIC_AUTH_TOKEN`，而不是 `ANTHROPIC_API_KEY`。** 前者发送 `Authorization: Bearer`，这是我们要求的。后者发送 `x-api-key`，我们拒绝接收。

**将 `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` 设置为 `1`。** 否则，Claude Code 会发送实验性 beta 字段，例如 `context_management`。我们不会转发这些字段所需的 `anthropic-beta` 标头，因此每个请求都会失败。

**设置两个模型变量。** Claude Code 使用一个密钥运行一个大型主循环模型和一个小型后台模型。两者都必须是 `claude-*` slug。

## 确认密钥已应用

每个响应都带有 `x-flexinference-defaults-applied`。Claude Code 不显示响应标头，因此请改为在仪表板的**日志**下查看请求。

## 故障排除

**`400 context_management: Extra inputs are not permitted`。** Claude Code 发送了一个实验性 beta 字段，但我们没有转发其所需的 `anthropic-beta` 标头。请设置 `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1` 并启动新会话。此标志也会清除任何其他命名 beta 字段的 `Extra inputs are not permitted` 拒绝。

**[`401 missing_api_key`](/zh/errors#missing_api_key)。** 您设置了 `ANTHROPIC_API_KEY`，因此客户端发送了 `x-api-key`，我们不读取此项。请改为设置 `ANTHROPIC_AUTH_TOKEN` 并取消设置 `ANTHROPIC_API_KEY`。

**[`400 unsupported_parameter`](/zh/errors#unsupported_parameter) 命名 `output_config`。** 模型变量指向非 Claude slug，并且 Claude Code 发送了我们拒绝错误翻译的 Anthropic 原生字段。请将两个模型变量都设置为 `claude-*` slug。

**[`404 unknown_url`](/zh/errors#unknown_url) 命名 `/v1/messages/count_tokens`。** 我们不路由该路径。`claude -p` 运行从不调用它，尽管交互式会话可能会。

**每个请求都会耗尽完整的重试预算。** Claude Code 会自行重试，而密钥的 `retry` 策略会与其叠加。请在使用此客户端的密钥上不设置 `retry`。

有关我们返回的所有拒绝，请参见[错误](/zh/errors)；有关非 Claude Code 特有的拒绝，请参见[代理密钥](/zh/agent-keys)。
