OpenCode
An open-source terminal AI coding agent compatible with a wide range of model service providers.
Overview
OpenCode is an open-source terminal AI coding agent that runs in both TUI and CLI modes and is compatible with 75+ AI service providers. Through its built-in @ai-sdk/anthropic and @ai-sdk/openai-compatible adapters, it can seamlessly connect to the Anthropic-protocol models and OpenAI-compatible models provided by Models Hub.
- Website: https://opencode.ai
- GitHub: https://github.com/opencode-ai/opencode
📦 Before You Start
What you need
- OpenCode installed
- A working Models Hub Base URL (must end with
/v1) - A working Models Hub API Key (generate it in the console)
- The model name you want to use (such as
claude-sonnet-4-6,claude-haiku-4-5,deepseek-v4-pro,gpt-5.5, etc., which must match the model ID in the Models Hub console exactly)
🚀 Option 1: Interactive Configuration (Recommended for Beginners)
Add Models Hub directly through the menu in the OpenCode TUI:
- Launch the OpenCode TUI
- Enter the
/connectcommand - Select "Other" (custom endpoint) from the provider list
- Fill in as prompted:
- Base URL:
https://modelsok.com/v1 - API Key: your Models Hub API Key
- Base URL:
- After saving, enter
/modelsto switch to the newly added model
🔧 Option 2: Configuration File (Recommended for Long-Term Use)
Suitable for scenarios where you want to lock in multiple models and reuse them across projects.
1. Edit the OpenCode Configuration
Configuration file locations:
- Global:
~/.config/opencode/opencode.json - Project-level (higher priority):
opencode.jsonin the project root
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"modelsok-anthropic": {
"npm": "@ai-sdk/anthropic",
"name": "Models Hub (Anthropic)",
"options": {
"baseURL": "https://modelsok.com/v1",
"apiKey": "sk-123"
},
"models": {
"claude-sonnet-4-6": {
"name": "Claude Sonnet 4.6",
"limit": { "context": 200000, "output": 8192 }
},
"haiku": {
"id": "claude-haiku-4-5",
"name": "Claude Haiku 4.5 (fast)",
"limit": { "context": 200000, "output": 8192 }
}
}
},
"modelsok": {
"npm": "@ai-sdk/openai-compatible",
"name": "Models Hub",
"options": {
"baseURL": "https://modelsok.com/v1",
"apiKey": "sk-123"
},
"models": {
"deepseek-v4-flash": {
"name": "DeepSeek V4 Flash",
"limit": { "context": 131072, "output": 16384 }
},
"deepseek-v4-pro": {
"name": "DeepSeek V4 Pro",
"limit": { "context": 131072, "output": 16384 }
},
"gpt-5.5": {
"name": "GPT-5.5",
"limit": { "context": 128000, "output": 32768 }
},
"gpt-5.4": {
"name": "GPT-5.4",
"limit": { "context": 128000, "output": 32768 }
},
"gpt-5.4-mini": {
"name": "GPT-5.4 Mini",
"limit": { "context": 128000, "output": 16384 }
}
}
}
},
"model": "modelsok-anthropic/claude-sonnet-4-6",
"autoshare": false
}Key field notes
- The example declares two providers:
modelsok-anthropic:npmmust be@ai-sdk/anthropic, used to connect to Anthropic-protocol models such as Claudemodelsok:npmmust be@ai-sdk/openai-compatible, used to connect to OpenAI-compatible models such as DeepSeek and GPT
- The
baseURLof both providers ishttps://modelsok.com/v1 - The
apiKeyfield is written directly in the configuration file; replace it with your own Models Hub API Key (thesk-123in the example is only a placeholder) - The model IDs in
modelsmust match the model names actually exposed in the Models Hub console exactly; if you want a custom shorthand, you can use theidfield to point to the real model (such ashaiku→claude-haiku-4-5in the example) - The top-level
modelfield specifies the default model, in the format<provider>/<model> - The project-level
opencode.jsontakes priority over the global configuration
2. Configure the API Key
The example above already embeds the API Key directly in opencode.json via options.apiKey. Just replace sk-123 with your own key—no extra steps required.
Optional: Manage Keys with auth.json
If you prefer not to write the API Key into opencode.json, you can remove the apiKey field from the config and instead edit ~/.local/share/opencode/auth.json:
{
"modelsok-anthropic": {
"type": "api",
"key": "sk-your-modelsok-key"
},
"modelsok": {
"type": "api",
"key": "sk-your-modelsok-key"
}
}Or log in via the command line:
opencode auth loginFollow the prompts to enter the API Key for modelsok-anthropic and modelsok respectively.
✅ Step 3: Verify the Connection
After starting OpenCode, enter /models and check whether models such as modelsok-anthropic/claude-sonnet-4-6 and modelsok/deepseek-v4-pro appear. Select the target model, send a test message, and if you receive a normal response, the configuration was successful.
❓ FAQ
| Issue | Solution |
|---|---|
/models does not show models under Models Hub | Check whether provider.modelsok-anthropic.models or provider.modelsok.models in the config file has content |
| Model does not exist error | Confirm that the model ID matches the Models Hub console exactly |
| Invalid API Key | Check that apiKey in opencode.json or auth.json is correct; you can also rerun opencode auth login |
| Protocol error when calling Claude models | Confirm you are using the modelsok-anthropic provider (@ai-sdk/anthropic), not modelsok (@ai-sdk/openai-compatible) |
| Configuration changes have no effect | Restart OpenCode; project-level config takes priority over global |