Claude Code CLI
Claude Code 是 Anthropic 官方的命令行 AI 编程助手,Miloapi 上跑 Claude 的首选客户端。
1. 安装
# Mac / Linux (curl 一键)
curl -fsSL https://claude.ai/install.sh | bash
# 或者 npm 全局安装(所有 OS)
npm install -g @anthropic-ai/claude-code
# Windows 推荐 npm,先装 Node.js (https://nodejs.org)
验证:
claude --version
2. 配置方式 ① 环境变量(快速但不持久)
macOS / Linux — 写入 ~/.zshrc 或 ~/.bashrc:
export ANTHROPIC_BASE_URL=https://www.miloapi.com
export ANTHROPIC_AUTH_TOKEN=sk-你的key
保存后 source ~/.zshrc 让它生效。
Windows PowerShell — 临时(当前会话):
$env:ANTHROPIC_BASE_URL = "https://www.miloapi.com"
$env:ANTHROPIC_AUTH_TOKEN = "sk-你的key"
永久写入(只需做一次):
[Environment]::SetEnvironmentVariable("ANTHROPIC_BASE_URL", "https://www.miloapi.com", "User")
[Environment]::SetEnvironmentVariable("ANTHROPIC_AUTH_TOKEN", "sk-你的key", "User")
3. 配置方式 ② settings.json(推荐)
环境变量偶尔会被某些 shell 配置覆盖,配置文件更稳。
文件位置:
| 系统 | 路径 |
|---|---|
| macOS / Linux | ~/.claude/settings.json |
| Windows | %USERPROFILE%\.claude\settings.json |
文件不存在就手动创建(包括目录)。
完整内容:
{
"env": {
"ANTHROPIC_BASE_URL": "https://www.miloapi.com",
"ANTHROPIC_AUTH_TOKEN": "sk-你的key",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1",
"CLAUDE_CODE_DISABLE_TERMINAL_TITLE": "1"
}
}
参数说明:
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1— 关闭非必要的统计 / 升级检查请求,避免它们去打官方 endpoint 触发 403CLAUDE_CODE_DISABLE_TERMINAL_TITLE=1— 别让 Claude 改你的 terminal 标题
⚠️ Key 选 Claude-MAX 池(平台=anthropic),GPT 池的 Key 调 Claude 会 404。
4. 验证
任意目录执行:
claude
出现交互界面、问个问题能正常回答,就算成功。
5. 故障排查
| 现象 | 原因 / 处理 |
|---|---|
Login required 反复弹 | 配置文件没生效,先检查文件路径和 JSON 格式(cat ~/.claude/settings.json 看一下) |
401 Unauthorized | Key 写错了 / Key 被删了 / 分组不对(用了 GPT 池 Key) |
404 Not Found | base URL 写错了,Claude Code 是 https://www.miloapi.com 不带 /v1 |
| 连不上,卡住 | 检查代理是否劫持了 anthropic.com 之类的域名,可能是公司网络拦截 |
Insufficient balance | 余额不够,去钱包充值 |
| 改了 settings.json 还是不生效 | 重启 terminal,然后再试 claude |
| JSON 解析报错 | 检查文件里有没有多余的逗号 / 没引号的字段名,贴去 jsonlint.com 验证 |
6. 进阶
切换多个 endpoint
如果你同时挂多个中转 + 官方,推荐用 CC-Switch,一键切换 endpoint。
限制 Claude Code 不读 git 历史
某些团队不希望 AI 看到所有 git log,在 settings.json 加:
{
"env": { ... },
"ignore": {
"git_history": true
}
}
详见 Anthropic 官方文档。
