在 Claude Code 中使用 oairoute
- Claude Code 支持自定义 API 端点
- 通过环境变量指向 OAIRoute,即可用同一个 Key 跑通
- 适合统一账单与额度管理
第一步:获取 API Key
在 OAIRoute 控制台 创建。
Create one in the OAIRoute console.
第二步:设置环境变量
Claude Code 读取两个环境变量:
填 https://api.oairoute.com。注意这里不带 /v1,Claude Code 会自己拼 /v1/messages。
填你的 OAIRoute API Key。
macOS / Linux(写入 shell 配置):
echo 'export ANTHROPIC_BASE_URL="https://api.oairoute.com"' >> ~/.zshrc
echo 'export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxx"' >> ~/.zshrc
source ~/.zshrcWindows PowerShell(写入用户环境变量):
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://api.oairoute.com", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "sk-xxxxxxxx", "User")改完后要重开终端,环境变量才会生效。
Claude Code reads two environment variables:
Enter https://api.oairoute.com. Don't include /v1 here; Claude Code appends /v1/messages itself.
Enter your OAIRoute API key.
macOS / Linux (write to your shell config):
echo 'export ANTHROPIC_BASE_URL="https://api.oairoute.com"' >> ~/.zshrc
echo 'export ANTHROPIC_AUTH_TOKEN="sk-xxxxxxxx"' >> ~/.zshrc
source ~/.zshrcWindows PowerShell (write user environment variables):
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://api.oairoute.com", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "sk-xxxxxxxx", "User")Reopen the terminal afterward for the variables to take effect.
第三步:验证
claude --version
claude "你好,用一句话介绍你自己"能正常回复就说明接通了。
claude --version
claude "Hello, introduce yourself in one sentence"A normal reply means it's connected.
模型选择
Claude Code 默认使用 Claude 系列模型。想指定具体模型,用 --model:
claude --model claude-sonnet-5-5 "帮我看看这段代码"如果同时装了官方版和 OAIRoute 版,建议开两个终端窗口分别设置环境变量,或者用 .env 文件按项目隔离,避免串号。
Claude Code uses Claude models by default. To pick a specific model, use --model:
claude --model claude-sonnet-5-5 "Take a look at this code"If you have both the official build and the OAIRoute build installed, set the environment variables in two separate terminal windows, or use a per-project .env file to isolate them and avoid crossing wires.
常见问题
提示 authentication_error
ANTHROPIC_AUTH_TOKEN 没设上,或者设在了当前 shell 之外。用 echo $ANTHROPIC_AUTH_TOKEN 确认(Windows 用 echo $env:ANTHROPIC_AUTH_TOKEN)。
提示 404
ANTHROPIC_BASE_URL 多写了 /v1。正确值是 https://api.oairoute.com。
可以正常对话但工具调用失败
部分模型对 tool_use 的支持程度不同。换用 claude-sonnet-5-5 通常能解决。
authentication_error
ANTHROPIC_AUTH_TOKEN isn't set, or it's set outside the current shell. Confirm with echo $ANTHROPIC_AUTH_TOKEN (on Windows, echo $env:ANTHROPIC_AUTH_TOKEN).
404 error
ANTHROPIC_BASE_URL has an extra /v1. The correct value is https://api.oairoute.com.
Chat works but tool calls fail
Models differ in their tool_use support. Switching to claude-sonnet-5-5 usually fixes it.