# Claude Code

[Claude Code](https://code.claude.com/docs) 是 Anthropic 官方推出的 AI 编程 Agent，可以在终端中使用，也提供 VS Code、JetBrains 等 IDE 扩展。Claude Code 使用 **Anthropic Messages** 接口格式，BeeRoute 原生支持，只需修改接口地址和密钥。

## 1. 获取 API Key

1. 打开「[API 密钥](https://beeroute.ai/keys)」页面，点击 **创建 API 密钥**。
2. **分组** 选择 `ClaudeCode`，保存后复制密钥（以 `sk-` 开头）。

?> `ClaudeCode` 分组是 Claude Code 专用分组，价格更低，只包含 Claude 系列模型，只能在 Claude Code 客户端中使用。

## 2. 安装 Claude Code

推荐使用官方原生安装脚本，安装后会自动更新：

**macOS / Linux / WSL：**

```bash
curl -fsSL https://claude.ai/install.sh | bash
```

**Windows PowerShell：**

```powershell
irm https://claude.ai/install.ps1 | iex
```

**Windows CMD：**

```bat
curl -fsSL https://claude.ai/install.cmd -o install.cmd && install.cmd && del install.cmd
```

也可以用其他方式安装（以下方式都不会自动更新）：

| 方式 | 命令 |
| :--- | :--- |
| Homebrew（macOS） | `brew install --cask claude-code` |
| WinGet（Windows） | `winget install Anthropic.ClaudeCode` |
| npm（需要 Node.js 22 或更高版本） | `npm install -g @anthropic-ai/claude-code` |

Windows 用户建议同时安装 [Git for Windows](https://git-scm.com/downloads/win)，Claude Code 会用它执行命令。

安装完成后，打开新的终端窗口验证：

```bash
claude --version
```

## 3. 配置 BeeRoute

编辑（不存在就新建）用户级配置文件：

- macOS / Linux：`~/.claude/settings.json`
- Windows：`%USERPROFILE%\.claude\settings.json`

填入以下内容，把 `sk-xxx` 换成你的密钥：

```json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://beeroute.ai",
    "ANTHROPIC_AUTH_TOKEN": "sk-xxx",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"
  }
}
```

| 配置项 | 说明 |
| :--- | :--- |
| `ANTHROPIC_BASE_URL` | BeeRoute 地址，**不要**带 `/v1`，Claude Code 会自动拼接 `/v1/messages` |
| `ANTHROPIC_AUTH_TOKEN` | 你的 BeeRoute 密钥，以 `Authorization: Bearer` 请求头发送 |
| `CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS` | 不发送实验性 beta 功能，避免部分请求返回 400 |

- 如果文件里已有其他配置，只需把这几项合并到 `env` 中。
- **不要**把密钥写进项目目录下的 `.claude/settings.json`，这个文件通常会提交到代码仓库。
- 不要同时设置 `ANTHROPIC_API_KEY` 环境变量，否则启动时会提示两个凭据冲突。

### 一键配置脚本（可选）

也可以运行 BeeRoute 提供的脚本，按提示输入密钥即可写好配置。脚本会先备份原有的 `settings.json`，再**整体覆盖**它。

- Windows：[下载 win_claude_setup.bat](https://beeroute.ai/static/cli/win_claude_setup.bat)，双击运行。
- macOS / Linux：

```bash
bash <(curl -fsSL https://beeroute.ai/static/cli/linux_claude_setup.sh)
```

## 4. 启动与验证

进入项目目录后启动：

```bash
cd /path/to/your-project
claude
```

`~/.claude/settings.json` 里配置了 `ANTHROPIC_AUTH_TOKEN` 后，Claude Code 不会再要求登录 Anthropic 账号（首次启动可能仍会出现主题选择和安全提示，按提示继续即可）。

启动后输入 `/status`，确认：

- `Anthropic base URL` 一行显示 `https://beeroute.ai`；
- 凭据一行显示 `ANTHROPIC_AUTH_TOKEN`。

然后随便发一句话，能正常回复就说明接入成功。

## 5. 选择模型

- 在 Claude Code 中输入 `/model` 切换模型；
- 或启动时指定：`claude --model claude-opus-5-5`。

如果想让 `opus`、`sonnet`、`haiku` 这几个别名固定使用指定的模型，可以在 `env` 中加入：

```json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://beeroute.ai",
    "ANTHROPIC_AUTH_TOKEN": "sk-xxx",
    "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-5-5",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-5-5",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-5-5"
  }
}
```

`ANTHROPIC_DEFAULT_HAIKU_MODEL` 也决定了 Claude Code 后台任务（例如生成会话标题）使用的模型。`ClaudeCode` 分组可用的模型请在「[模型定价](https://beeroute.ai/modellist/?lang=zh)」页面查看。

## 6. 在 VS Code / Cursor 等编辑器中使用

1. 在扩展市场搜索并安装 **Claude Code** 扩展。
2. 按 `Ctrl+Shift+P`（macOS 为 `Cmd+Shift+P`），执行 **Preferences: Open User Settings (JSON)**，加入：

```json
{
  "claudeCode.environmentVariables": [
    { "name": "ANTHROPIC_BASE_URL", "value": "https://beeroute.ai" },
    { "name": "ANTHROPIC_AUTH_TOKEN", "value": "sk-xxx" },
    { "name": "CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS", "value": "1" }
  ],
  "claudeCode.disableLoginPrompt": true
}
```

?> 扩展启动前会先检查这里的设置，所以即使 `~/.claude/settings.json` 已经配置好，扩展里也建议再配置一次。

## 7. 常见问题

**启动后仍然要求登录？**

- 确认密钥写在用户级的 `~/.claude/settings.json` 里，而不是项目目录下的 `.claude/settings.json`。
- 之前用 Anthropic 账号登录过的，可以在 Claude Code 中执行 `/logout` 清除旧登录。
- 仍然不行，可以运行跳过登录脚本：Windows [下载 skip_claude_login.bat](https://beeroute.ai/static/cli/skip_claude_login.bat)；macOS / Linux 执行 `bash <(curl -fsSL https://beeroute.ai/static/cli/skip_claude_login.sh)`。

**返回 400，提示 `Extra inputs are not permitted` 或 `invalid beta flag`？**

确认 `env` 中有 `"CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS": "1"`。

**返回 403，或提示客户端版本过低？**

- 确认密钥属于 `ClaudeCode` 分组；
- `ClaudeCode` 分组要求较新的 Claude Code 版本，请升级：原生安装执行 `claude update`，npm 安装重新执行 `npm install -g @anthropic-ai/claude-code`。

**提示 `auth may not work as expected`，或凭据冲突？**

同时存在多个凭据（例如系统里设置了 `ANTHROPIC_API_KEY`，或之前登录过 Anthropic 账号）。删除多余的环境变量，或执行 `/logout`。

**Remote Control、语音输入不可用？**

这是 Claude Code 的官方限制：使用第三方接口地址时，这两个功能依赖 Anthropic 账号，无法使用。

更多排查方法见 Claude Code 官方文档：[Connect Claude Code to an LLM gateway](https://code.claude.com/docs/en/llm-gateway-connect)。
