References: OpenCode Config, Providers
OpenCode uses the OpenAI Chat Completions API. When connecting to a custom gateway, declare the endpoint and the model list in the provider block of opencode.jsonc.
1. Installation and Configuration File
| |
The user configuration is at ~/.config/opencode/opencode.jsonc by default (opencode.json also works).
Configuration precedence (high → low):
- macOS Managed configuration (pushed by the enterprise, cannot be overridden)
- Inline configuration
OPENCODE_CONFIG_CONTENT - Project configuration
opencode.json/opencode.jsonc - Custom path
OPENCODE_CONFIG - Global configuration
~/.config/opencode/opencode.jsonc
It is best to keep the Provider and Key in the global configuration, to avoid committing them to git.
2. Connecting to a Gateway
| Field | Purpose |
|---|---|
provider.<id>.options.baseURL | Gateway address |
provider.<id>.options.apiKey | API Key |
provider.<id>.models | List of available models |
model | Default model, in the format provider/model |
When connecting to an OpenAI-compatible gateway, npm is always @ai-sdk/openai-compatible.
2.1 Minimal Configuration
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"llmapi": {
"npm": "@ai-sdk/openai-compatible",
"name": "llmapi",
"options": {
"baseURL": "https://api.xxx.com/v1",
"apiKey": "your-token"
},
"models": {
"glm-5.2": { "name": "GLM 5.2" }
}
}
},
"model": "llmapi/glm-5.2"
}
Restart OpenCode, then use /models in the TUI to confirm whether the model is loaded.
2.2 Where to Put the Secret
Writing apiKey directly in the configuration file leaves it on disk in plaintext. The api.xxx.com and your-token in this post’s examples are placeholders; replace them with your own values, and never commit a real Key to a public repository.
Safer approaches:
"apiKey": "{env:LLMAPI_KEY}"
| |
"apiKey": "{file:~/.config/opencode/llmapi.key}" is also supported.
2.3 Gateway Caveats
- The gateway must be compatible with OpenAI Chat Completions (
/v1/chat/completions) - The keys of
modelsmust match themodelfield accepted by the gateway API - If the endpoint uses
/v1/responses, use@ai-sdk/openaias thenpmpackage instead - Model IDs are subject to the gateway’s documentation
3. Multi-Model Configuration
Declare all models under provider.<id>.models and divide the work between model and small_model:
{
"provider": {
"llmapi": {
"npm": "@ai-sdk/openai-compatible",
"name": "llmapi",
"options": {
"baseURL": "https://api.xxx.com/v1",
"apiKey": "{env:LLMAPI_KEY}"
},
"models": {
"glm-5.2": { "name": "GLM 5.2" },
"deepseek-v4-pro": { "name": "DeepSeek V4 Pro" },
"deepseek-v4-flash": { "name": "DeepSeek V4 Flash" }
}
}
},
"model": "llmapi/glm-5.2",
"small_model": "llmapi/deepseek-v4-flash"
}
| Field | Model | Use |
|---|---|---|
model | GLM 5.2 | Everyday main model |
small_model | DeepSeek V4 Flash | Lightweight tasks such as title generation |
/models switch | DeepSeek V4 Pro | Switched manually for heavy coding |
3.1 Switching Within a Session
| |
4. Usage Tips
4.1 Everyday Development
- Default to GLM 5.2 day to day, and switch to Pro in
/modelswhen coding gets hard - Flash is configured as
small_model; lightweight tasks use it automatically, so there is no need to switch manually
4.2 Troubleshooting Checklist
| Symptom | Common Cause |
|---|---|
| The model list is empty | models was not declared, or the Provider ID is wrong |
| 401 / 403 | apiKey was not set, or the {env:...} variable was not exported |
| 404 | baseURL is wrong, or the gateway is not compatible with the OpenAI API |
| Invalid model ID | The keys of models do not match the names on the gateway side |
| Config changes have no effect | OpenCode was not restarted |
5. Complete Example
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"llmapi": {
"npm": "@ai-sdk/openai-compatible",
"name": "llmapi",
"options": {
"baseURL": "https://api.xxx.com/v1",
"apiKey": "{env:LLMAPI_KEY}"
},
"models": {
"glm-5.2": { "name": "GLM 5.2" },
"deepseek-v4-pro": { "name": "DeepSeek V4 Pro" },
"deepseek-v4-flash": { "name": "DeepSeek V4 Flash" }
}
}
},
"model": "llmapi/glm-5.2",
"small_model": "llmapi/deepseek-v4-flash"
}
| |
