⚠️ 待更新·2026-08-29核验 · 更新时间待核验 · 本文信息可能已过期,请以官方文档为准
更新时间:2026-08-29 · 核验状态:待更新 · 官方溯源待补
简介
OpenCode 自身不收费,成本完全来自模型 API。好消息是,OpenRouter、Groq、Together AI 等平台都提供免费层模型,通过 OpenAI 兼容协议即可接入。本文给出三套即用配置,并对比各家免费层在编程任务上的实际表现。
架构图
flowchart LR
A[OpenCode CLI] --> B[OpenRouter]
A --> C[Groq]
A --> D[Together AI]
B --> B1[Llama:free]
C --> C1[Llama 3.3 70B]
D --> D1[Qwen 2.5 72B]
B1 --> E[Zero-cost terminal coding]
C1 --> E
D1 --> E
前置准备
确认已安装 OpenCode(参考 us-01),并准备好以下 Key:
全部用环境变量管理,避免硬编码到配置文件。
配置文件
把以下内容写入项目根目录 opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openrouter": {
"options": {
"baseURL": "https://openrouter.ai/api/v1"
},
"models": {
"meta-llama/llama-3.3-70b-instruct:free": {
"name": "Llama 3.3 70B (free)"
}
}
},
"groq": {
"options": {
"baseURL": "https://api.groq.com/openai/v1"
},
"models": {
"llama-3.3-70b-versatile": { "name": "Llama 3.3 70B Groq" },
"llama-3.1-8b-instant": { "name": "Llama 3.1 8B Instant" }
}
},
"together": {
"options": {
"baseURL": "https://api.together.xyz/v1"
},
"models": {
"meta-llama/Llama-3.3-70B-Instruct-Turbo": { "name": "Llama 3.3 70B Together" }
}
}
},
"model": "groq/llama-3.3-70b-versatile"
}
注入密钥
export OPENROUTER_API_KEY="sk-or-v1-..."
export GROQ_API_KEY="gsk_..."
export TOGETHER_API_KEY="..."
OpenCode 会按 provider 名称自动匹配同名环境变量,无需在配置中硬编码 Key。
启动与切换
opencode
进入 TUI 后,按 Ctrl+M 可在已配置的模型间快速切换。日常写代码选 Groq 的 llama-3.3-70b-versatile(速度极快,首字延迟常低于 300ms),复杂推理切到 OpenRouter 的 llama-3.3-70b-instruct:free,代码补全用 llama-3.1-8b-instant 最划算。
免费层对比
| Provider |
强项 |
弱项 |
适用场景 |
| Groq |
速度极快 |
上下文 128K,TPM 严 |
实时对话、补全 |
| OpenRouter :free |
模型多 |
排队严重 |
复杂推理 |
| Together |
稳定 |
完全免费模型少 |
中等任务 |
模型选型经验
经过实测,各家免费模型在不同任务上表现差异明显:代码补全选 Groq 的 llama-3.1-8b-instant,延迟低且免费配额大;复杂重构与算法题选 OpenRouter 上的 llama-3.3-70b-instruct:free,70B 参数量带来更强推理;中文文档撰写选 Together 上的 Qwen 系列。建议把多家都配上,任务来时按需切换。
常见问题
429 Too Many Requests:免费层有 RPM 限制,等 30 秒或切换另一家 provider。推荐把三家都配上,出问题就 Ctrl+M 切换。
- 中文输出乱码:终端设置
export LANG=en_US.UTF-8,或在 prompt 里加 Always respond in Simplified Chinese。
- 想用 OpenRouter 的 :free 模型但报 403:确认账户已绑定信用卡(OpenRouter 免费模型也要求验证,不会扣费)。
- context 超 8K 报错:8B 模型上下文有限,改用 70B 或拆分文件。
至此,你已经拥有一个完全免费的终端 AI 编程环境。
常见坑与最佳实践
- 多 provider 切换需要 /model 命令:OpenCode 不会自动 fallback,单个 provider 429 时需要手动切到备用模型。
- Token 上下文要小:免费模型大多 8K-32K 上下文,长文件先 chunk 再投。
- API Key 不入 Git:
apiKey: "${ENV_VAR}" 引用语法必须用,避免 commit hook 拦不住的泄露。
- Groq 比 OpenRouter 快:日常编程优先接 Groq,OpenRouter
:free 留作降级。
⚠️ Pending Update · 2026-08-29 Verification · Content may be outdated, please refer to official docs
Updated: 2026-08-29 · Status: Pending Verification
Introduction
OpenCode itself is free — your only cost is the model API. The good news is that OpenRouter, Groq, and Together AI all expose free-tier models over an OpenAI-compatible protocol. This article gives you three ready-to-use configurations and compares their real-world behavior on coding tasks.
架构图
flowchart LR
A[OpenCode CLI] --> B[OpenRouter]
A --> C[Groq]
A --> D[Together AI]
B --> B1[Llama:free]
C --> C1[Llama 3.3 70B]
D --> D1[Qwen 2.5 72B]
B1 --> E[Zero-cost terminal coding]
C1 --> E
D1 --> E
Prerequisites
Make sure OpenCode is installed (see us-01) and grab these keys:
Manage all of them via environment variables — never hardcode them into config files.
Config File
Write the following to opencode.json in your project root:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"openrouter": {
"options": { "baseURL": "https://openrouter.ai/api/v1" },
"models": {
"meta-llama/llama-3.3-70b-instruct:free": { "name": "Llama 3.3 70B (free)" }
}
},
"groq": {
"options": { "baseURL": "https://api.groq.com/openai/v1" },
"models": {
"llama-3.3-70b-versatile": { "name": "Llama 3.3 70B Groq" },
"llama-3.1-8b-instant": { "name": "Llama 3.1 8B Instant" }
}
},
"together": {
"options": { "baseURL": "https://api.together.xyz/v1" },
"models": {
"meta-llama/Llama-3.3-70B-Instruct-Turbo": { "name": "Llama 3.3 70B Together" }
}
}
},
"model": "groq/llama-3.3-70b-versatile"
}
Inject Keys
export OPENROUTER_API_KEY="sk-or-v1-..."
export GROQ_API_KEY="gsk_..."
export TOGETHER_API_KEY="..."
OpenCode matches each provider to an environment variable of the same name, so you never hardcode keys in the config.
Launch and Switch
opencode
Inside the TUI, press Ctrl+M to switch between configured models. Use Groq's llama-3.3-70b-versatile for fast everyday coding (first-token latency often under 300ms), fall back to OpenRouter's :free Llama for heavier reasoning, and pick llama-3.1-8b-instant for the cheapest code completion.
Free-tier Comparison
| Provider |
Strength |
Weakness |
Best for |
| Groq |
Blazing fast |
128K context, strict TPM |
Real-time chat, completion |
| OpenRouter :free |
Many models |
Heavy queuing |
Complex reasoning |
| Together |
Stable |
Few fully free models |
Mid-complexity tasks |
Model Selection Tips
In practice, free models differ noticeably by task: for code completion use Groq's llama-3.1-8b-instant (low latency, large free quota); for complex refactors or algorithms pick OpenRouter's llama-3.3-70b-instruct:free (70B params, stronger reasoning); for Chinese prose use Qwen on Together. Configure all of them and switch on demand.
Troubleshooting
429 Too Many Requests: Free tiers have RPM caps. Wait 30 seconds or switch providers — configuring all three lets you Ctrl+M to the next on failure.
- Garbled non-English output: Set
export LANG=en_US.UTF-8, or append Always respond in Simplified Chinese to the prompt.
- OpenRouter
:free returns 403: Verify your account has a credit card on file — OpenRouter requires verification even for free models and will not charge you.
context length exceeded on 8B models: Switch to a 70B model or split the file into chunks.
You now have a fully free terminal AI coding environment.
Pitfalls and Best Practices
- Multi-provider switching needs /model command: OpenCode does not auto-fallback; when one provider hits 429 you must manually switch to the backup.
- Keep token context small: most free models have 8K-32K context; chunk long files before sending.
- Never commit API keys: use the
apiKey: "${ENV_VAR}" reference syntax; pre-commit hooks alone are not enough.
- Prefer Groq over OpenRouter for speed: Groq is consistently faster for daily coding; reserve OpenRouter
:free as the degraded fallback.