OpenCode
OpenCode tutorial โ Open-source terminal AI coding agent compatible with 75+ providers. Connect to EasyRouter via interactive menu or config file using the OpenAI-compatible API.
Project Introduction
OpenCode is an open-source terminal AI coding agent with both TUI and CLI modes. It's compatible with 75+ AI service providers and, via its built-in @ai-sdk/anthropic and @ai-sdk/openai-compatible adapters, can seamlessly connect to both Anthropic-protocol models and OpenAI-compatible models served by EasyRouter.
- Website: https://opencode.ai
- GitHub: https://github.com/opencode-ai/opencode
๐ฆ Prerequisites
What you'll need
- OpenCode installed
- An EasyRouter endpoint (must end with
/v1) - An EasyRouter API key (generated in the console)
- A model ID exactly matching one exposed by your EasyRouter console (e.g.
claude-sonnet-4-6,claude-haiku-4-5,deepseek-v4-pro,gpt-5.5)
๐ Option 1: Interactive menu (recommended for new users)
Add EasyRouter directly from the OpenCode TUI:
- Launch the OpenCode TUI
- Type
/connect - In the provider list, choose "Other" (custom endpoint)
- Fill in:
- Base URL:
https://easyrouter.io/v1 - API Key: your EasyRouter API key
- Base URL:
- After saving, type
/modelsand switch to the newly added model
๐ง Option 2: Config file (recommended for regular use)
Good for pinning multiple models and reusing across projects.
1. Edit OpenCode config
File location:
- Global:
~/.config/opencode/opencode.json - Per-project (takes precedence):
opencode.jsonin your project root
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"easyrouter-anthropic": {
"npm": "@ai-sdk/anthropic",
"name": "EasyRouter (Anthropic)",
"options": {
"baseURL": "https://easyrouter.io/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 }
}
}
},
"easyrouter": {
"npm": "@ai-sdk/openai-compatible",
"name": "EasyRouter",
"options": {
"baseURL": "https://easyrouter.io/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": "easyrouter-anthropic/claude-sonnet-4-6",
"autoshare": false
}Key fields
- The example declares two providers:
easyrouter-anthropic:npmmust be@ai-sdk/anthropicโ use this for Claude and other Anthropic-protocol modelseasyrouter:npmmust be@ai-sdk/openai-compatibleโ use this for DeepSeek, GPT, and other OpenAI-compatible models
- Both providers share the same
baseURL:https://easyrouter.io/v1 apiKeyis embedded directly in the config file โ replacesk-123with your real EasyRouter API key (the example value is a placeholder)- Model IDs under
modelsmust exactly match the model names exposed by your EasyRouter console. You can use a custom alias by adding anidfield pointing to the real model (seehaikuโclaude-haiku-4-5in the example) - The top-level
modelfield sets the default model in the form<provider>/<model> - Per-project
opencode.jsontakes precedence over the global config
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 real EasyRouter key โ no extra step required.
Optional: manage keys via auth.json
If you'd rather not store the API key in opencode.json, remove the apiKey field from the config and edit ~/.local/share/opencode/auth.json instead:
{
"easyrouter-anthropic": {
"type": "api",
"key": "sk-your-easyrouter-key"
},
"easyrouter": {
"type": "api",
"key": "sk-your-easyrouter-key"
}
}Or use the CLI:
opencode auth loginPick easyrouter-anthropic and/or easyrouter when prompted and paste your API key.
โ Step 3: Verify
Launch OpenCode and type /models. You should see entries like easyrouter-anthropic/claude-sonnet-4-6 and easyrouter/deepseek-v4-pro. Pick a model and send a test message โ a normal response means you're set.
โ Troubleshooting
| Issue | Fix |
|---|---|
EasyRouter models don't show in /models | Check that provider.easyrouter-anthropic.models or provider.easyrouter.models is populated |
| Model not found | Confirm the model ID matches your EasyRouter console exactly |
| Invalid API key | Verify apiKey in opencode.json or your auth.json; or re-run opencode auth login |
| Claude models return protocol errors | Make sure you're calling the easyrouter-anthropic provider (@ai-sdk/anthropic), not easyrouter (@ai-sdk/openai-compatible) |
| Config changes not picked up | Restart OpenCode; per-project config wins over global |