Skip to content
On this page

Claude Code 接入

Claude Code 使用 Anthropic API 风格。接入 CoreRouter 时,重点是 Base URL 不带 /v1,并通过环境变量提供 API Key。

准备工作

  • 已安装 VS Code。
  • 已安装 Claude Code 扩展或可用的 Claude Code 环境。
  • 已在 CoreRouter 控制台创建 API Key。
  • 已选择支持编程 Agent 的模型。

需要配置的环境变量

bash
ANTHROPIC_BASE_URL="https://api.corerouter.tech"
ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxxxxxx"
ANTHROPIC_MODEL="claude-sonnet-4-5"
变量说明
ANTHROPIC_BASE_URLCoreRouter Anthropic-compatible endpoint,不带 /v1
ANTHROPIC_AUTH_TOKENCoreRouter API Key
ANTHROPIC_MODEL控制台中的模型 ID,可按需要调整

VS Code 扩展配置

如果使用 Claude Code for VS Code,可以尝试在 VS Code 用户设置 JSON 中配置环境变量。不同扩展版本支持的配置键名可能不同;如果设置后没有生效,请把终端环境变量作为主配置,并从该终端启动 VS Code。

打开命令面板,执行 Preferences: Open User Settings (JSON),加入:

json
{
  "claudeCode.environmentVariables": [
    { "name": "ANTHROPIC_BASE_URL", "value": "https://api.corerouter.tech" },
    { "name": "ANTHROPIC_AUTH_TOKEN", "value": "sk-xxxxxxxxxxxxxxxx" },
    { "name": "ANTHROPIC_MODEL", "value": "claude-sonnet-4-5" }
  ]
}

macOS / Linux

临时配置:

bash
export ANTHROPIC_BASE_URL="https://api.corerouter.tech"
export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxxxxxx"
export ANTHROPIC_MODEL="claude-sonnet-4-5"

然后从同一个终端启动 VS Code:

bash
code

希望长期生效时,可以写入 ~/.zshrc 或 ~/.bashrc:

bash
echo 'export ANTHROPIC_BASE_URL="https://api.corerouter.tech"' >> ~/.zshrc
echo 'export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxxxxxx"' >> ~/.zshrc
echo 'export ANTHROPIC_MODEL="claude-sonnet-4-5"' >> ~/.zshrc
source ~/.zshrc

Windows PowerShell

临时配置:

powershell
$env:ANTHROPIC_BASE_URL="https://api.corerouter.tech"
$env:ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxxxxxxxxxx"
$env:ANTHROPIC_MODEL="claude-sonnet-4-5"

然后从同一个 PowerShell 启动 VS Code:

powershell
code

用户级永久配置:

powershell
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_BASE_URL', 'https://api.corerouter.tech', 'User')
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_AUTH_TOKEN', 'sk-xxxxxxxxxxxxxxxx', 'User')
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_MODEL', 'claude-sonnet-4-5', 'User')

重新打开 VS Code 后生效。

测试连接

text
请分析当前项目的目录结构,并介绍一下这个项目的技术栈。

如果能读取项目并返回分析结果,再测试 Agent 能力:

text
请检查当前项目是否存在 TypeScript 类型错误,并告诉我应该如何修复。

接口地址说明

Claude Code 和 OpenAI 兼容客户端使用的接口格式不同:

工具Base URLAPI 格式
Claude Codehttps://api.corerouter.techAnthropic Messages
Clinehttps://api.corerouter.tech/v1OpenAI Compatible

不要把 https://api.corerouter.tech/v1/chat/completions 当成 Claude Code 的 Endpoint。

手动测试 Anthropic-compatible endpoint

bash
curl https://api.corerouter.tech/v1/messages \
  -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-5",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello"}
    ]
  }'

如果你的网关明确要求 x-api-key 认证,请改用 ANTHROPIC_API_KEY 并把 Header 换成 x-api-key: $ANTHROPIC_API_KEY。

更多原始 API 示例见 Anthropic Messages 接入。

调用链路

text
Claude Code
   -> CoreRouter
   -> New API 兼容层
   -> 模型渠道
   -> AI Model

常见问题

环境变量不生效

关闭所有 VS Code 窗口,从设置了环境变量的终端重新执行 code。Windows 用户级环境变量设置后,可能需要重启 VS Code 或系统。

提示 API Key 无效

确认使用的是 ANTHROPIC_AUTH_TOKEN,不是 ANTHROPIC_API_KEY。同时检查 API Key 是否来自 CoreRouter 控制台。

可以连接但工具不可用

换用支持 Tool Calling、Streaming 和 Coding Agent 的模型,并检查 CoreRouter 的 Anthropic-compatible 转发是否开启。

提示 token count 或辅助端点不可用

Claude Code 的不同版本可能会调用 Anthropic 的辅助端点。CoreRouter 主要转发 Messages 请求,如果某个版本强依赖额外端点,可能会出现兼容性问题。遇到这种情况,先确认基础 /v1/messages 请求可用,再考虑更换 Claude Code 版本、换用 Cline,或联系支持确认当前接口能力。

Base URL 带不带 /v1

Claude Code 的环境变量填 https://api.corerouter.tech。如果填成 https://api.corerouter.tech/v1,工具可能会拼出错误路径。

安全建议

  • 推荐使用系统环境变量保存 API Key。
  • 不要把 API Key 写进项目仓库、README、截图或视频。
  • 为 Claude Code 创建独立 API Key,并设置额度上限。

Released under the MIT License.