主题
opencode
opencode 是 sst 团队开源的 AI 编程终端 Agent,定位类似 Claude Code,但完全开源、不绑定 provider。可以在配置文件里把任何兼容 OpenAI / Anthropic 协议的服务接进来。
关键差异:
- 多 provider 自由切换:原生支持 Claude / OpenAI / Google / Cloudflare / 本地模型,可在一份 config 里同时配
- TUI 优先:终端界面比 Claude Code 更接近 Neovim 风格
- Client/server 架构:底层服务可独立运行,方便给多个前端共享
适合谁:折腾型开发者;想绕开 Anthropic / OpenAI 的厂商绑定;偏爱终端 + 多模型同台对比。
推荐渠道
按你想用的模型选 Key:
- 用 Claude → 配
Claude 官方渠道的 Key - 用 GPT-5 → 配
OpenAI 兼容渠道的 Key - 想两家都用 → 创建两把 Key 在 config 里都配上
安装
任选一个:
bash
curl -fsSL https://opencode.ai/install | bashbash
npm install -g opencode-aibash
brew install anomalyco/tap/opencode配置
opencode 配置文件在 ~/.config/opencode/opencode.json(或 .jsonc)。我们用 @ai-sdk/openai-compatible 把 claude-api.org 加成自定义 provider:
json
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"claude-api": {
"npm": "@ai-sdk/openai-compatible",
"name": "Claude API (claude-api.org)",
"options": {
"baseURL": "https://claude-api.org/v1",
"apiKey": "{env:CLAUDE_API_KEY}"
},
"models": {
"claude-opus-4-7": {
"name": "Claude Opus 4.7",
"limit": { "context": 200000, "output": 65536 }
},
"claude-sonnet-4-6": {
"name": "Claude Sonnet 4.6",
"limit": { "context": 200000, "output": 65536 }
},
"claude-haiku-4-5-20251001": {
"name": "Claude Haiku 4.5",
"limit": { "context": 200000, "output": 65536 }
},
"gpt-5.4": {
"name": "GPT-5.4",
"limit": { "context": 128000, "output": 16384 }
},
"gpt-5.5": {
"name": "GPT-5.5",
"limit": { "context": 128000, "output": 16384 }
},
"gpt-5.3-codex": {
"name": "GPT-5.3 Codex",
"limit": { "context": 128000, "output": 16384 }
}
}
}
}
}bash
# 加到 ~/.zshrc 或 ~/.bashrc
export CLAUDE_API_KEY="sk-你的key"一份 config 多渠道
如果你想 Claude 和 GPT 用不同的 Key(精细计费),把上面的 claude-api provider 复制一份改名 claude-api-gpt,分别绑两个环境变量(CLAUDE_API_KEY_CLAUDE / CLAUDE_API_KEY_GPT),各自挂自己的模型。
验证
启动 opencode:
bash
opencode进入 TUI 后输入 /model,应该能看到你刚才配的所有模型(Claude Opus 4.7 / GPT-5.4 等)。选一个发"hello",有回复即接入成功。
也可以在启动时直接指定模型:
bash
opencode --model claude-api/claude-sonnet-4-6注意 model 写法是 <provider-name>/<model-id>。
进阶
走 Anthropic 原生协议(可选)
如果你想用 opencode 内置的 anthropic provider 直接对接(走 /v1/messages 而不是 /v1/chat/completions),试试:
json
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"baseURL": "https://claude-api.org",
"apiKey": "{env:CLAUDE_API_KEY}"
}
}
}
}实验性
opencode 内置 anthropic provider 对自定义 baseURL 的兼容性随版本变化。如果上面的 OpenAI compatible 方式已经能用,优先用它。
与 Claude Code 共存
opencode 配置文件跟 Claude Code (~/.claude/settings.json) 是独立的,可以同时装。同一把 sub2api Key 能给两边用。
常见问题
npm 提示 EACCES 权限错误
bash
sudo npm install -g opencode-ai
# 或者用 npm 设个用户级 prefix:
mkdir -p ~/.npm-global && npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
npm install -g opencode-aiProvider 没出现在 /model 列表
~/.config/opencode/opencode.json写错(缺逗号、缺引号)→cat ~/.config/opencode/opencode.json | python -m json.tool验证 JSON 合法- 环境变量没导出 →
echo $CLAUDE_API_KEY检查有值 - opencode 缓存了旧配置 → 重启 opencode
model 报 404 Not Found
- 确认你 sub2api 的 Key 绑的渠道支持该模型
- 模型名写错(漏了版本号) → 看渠道与价格的精确名
流式输出卡顿
opencode 走标准 SSE。如果国内运营商对 SSE 长连接超时,看 FAQ 的应对。
更多见 FAQ。
