Appearance
OpenCode 通过 AI SDK 和 Models.dev 支持 75+ 家 LLM provider,包括 Anthropic、OpenAI、Google 和本地模型(LM Studio、Ollama)。用 /models 命令切换模型,模型 ID 格式为 provider/model_id(如 anthropic/claude-sonnet-4-5)。支持为同一模型配置不同的 reasoning 强度变体,用快捷键 ctrl+t 快速切换。
OpenCode 模型选择
OpenCode 通过 AI SDK 和 Models.dev 支持 75+ LLM provider,同时支持运行本地模型。
添加 Provider
用 /connect 命令添加 LLM provider:
/connect弹出交互式界面,选择 provider 并填入 API Key。大多数主流 provider 已预加载,添加凭证后即可使用。
切换模型
添加 provider 后,用 /models 命令列出并选择可用模型:
/models推荐模型
以下模型在代码生成和工具调用两方面均表现优秀:
- GPT 5.2
- GPT 5.1 Codex
- Claude Opus 4.5
- Claude Sonnet 4.5
- Minimax M2.1
- Gemini 3 Pro
新模型不断涌现,这个列表不是穷举,具体以当前 Models.dev 数据为准。
设置默认模型
在 opencode.json 中配置 model 键:
json
{
"$schema": "https://opencode.ai/config.json",
"model": "anthropic/claude-sonnet-4-5"
}格式为 provider_id/model_id。
| 场景 | 示例 |
|---|---|
| Anthropic | anthropic/claude-sonnet-4-5 |
| OpenAI | openai/gpt-5 |
| OpenCode Zen | opencode/gpt-5.1-codex |
| LM Studio 本地模型 | lmstudio/google/gemma-3n-e4b |
模型加载优先级
OpenCode 启动时按以下顺序确定使用哪个模型:
- 命令行参数
--model(或-m) - 配置文件中的
model键 - 上次使用的模型
- 内部默认模型
配置模型选项
可以在配置中为特定模型设置全局参数(如 reasoning 强度):
jsonc
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openai": {
"models": {
"gpt-5": {
"options": {
"reasoningEffort": "high",
"textVerbosity": "low",
"reasoningSummary": "auto",
"include": ["reasoning.encrypted_content"]
}
}
}
},
"anthropic": {
"models": {
"claude-sonnet-4-5-20250929": {
"options": {
"thinking": {
"type": "enabled",
"budgetTokens": 16000
}
}
}
}
}
}
}- OpenAI 推理参数:
reasoningEffort(low/medium/high)、textVerbosity、reasoningSummary - Anthropic 推理参数:
thinking.type("enabled")、thinking.budgetTokens
模型变体(Variants)
变体让你为同一模型设置不同的 reasoning 配置,通过快捷键快速切换,不需要手动改配置。
内置变体
OpenCode 为主流 provider 内置了默认变体:
Anthropic
high:高 thinking 预算(默认)max:最大 thinking 预算
OpenAI(因模型而异)
none/minimal/low/medium/high/xhigh
low:较低 effort/token 预算high:较高 effort/token 预算
自定义变体
覆盖内置变体或新建自定义变体:
jsonc
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openai": {
"models": {
"gpt-5": {
"variants": {
"thinking": {
"reasoningEffort": "high",
"textVerbosity": "low"
},
"fast": {
"disabled": true
}
}
}
}
}
}
}切换变体
用 ctrl+t(即 variant_cycle 快捷键)循环切换当前模型的变体,不需要重启 OpenCode。
本地模型
OpenCode 支持通过 LM Studio、Ollama 等工具运行本地模型。
配置示例(LM Studio):
json
{
"$schema": "https://opencode.ai/config.json",
"model": "lmstudio/google/gemma-3n-e4b"
}本地模型不需要 API Key,适合需要离线使用或数据隐私要求较高的场景。更多配置参考 providers 文档。
常见问题
Q: 如何同时配置 Build agent 用强模型、Plan agent 用轻量模型?
A: 在 opencode.json 的 agent 配置中为每个 agent 单独指定 model。例如 Plan 用 anthropic/claude-haiku-4-5,Build 用 anthropic/claude-sonnet-4-5。
Q: 国内访问 OpenAI/Anthropic 速度慢怎么办?
A: 可以配置自定义 baseURL(proxy 地址):
json
{
"provider": {
"anthropic": {
"options": {
"baseURL": "https://your-proxy.example.com/v1",
"apiKey": "{env:ANTHROPIC_API_KEY}"
}
}
}
}Q: /models 列表里没有我想要的模型怎么办?
A: 可以在 provider.models 中手动添加自定义模型,格式参考 Models.dev 上的 provider 文档。