OpenCode
**Project Introduction**
OpenCode
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 AutoRouter.
- Website: https://opencode.ai
- GitHub: https://github.com/opencode-ai/opencode
📦 Prerequisites
What you'll need
- OpenCode installed
- An AutoRouter endpoint (must end with
/v1) - An AutoRouter API key (generated in the console)
- A model ID exactly matching one exposed by your AutoRouter 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 AutoRouter directly from the OpenCode TUI:
- Launch the OpenCode TUI
- Type
/connect - In the provider list, choose "Other" (custom endpoint)
- Fill in:
- Base URL:
https://autorouter.top/v1 - API Key: your AutoRouter 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": {
"autorouter-anthropic": {
"npm": "@ai-sdk/anthropic",
"name": "AutoRouter (Anthropic)",
"options": {
"baseURL": "https://autorouter.top/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 }
}
}
},
"autorouter": {
"npm": "@ai-sdk/openai-compatible",
"name": "AutoRouter",
"options": {
"baseURL": "https://autorouter.top/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": "autorouter-anthropic/claude-sonnet-4-6",
"autoshare": false
}Key fields
- The example declares two providers:
autorouter-anthropic:npmmust be@ai-sdk/anthropic— use this for Claude and other Anthropic-protocol modelsautorouter:npmmust be@ai-sdk/openai-compatible— use this for DeepSeek, GPT, and other OpenAI-compatible models
- Both providers share the same
baseURL:https://autorouter.top/v1 apiKeyis embedded directly in the config file — replacesk-123with your real AutoRouter API key (the example value is a placeholder)- Model IDs under
modelsmust exactly match the model names exposed by your AutoRouter 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 AutoRouter 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:
{
"autorouter-anthropic": {
"type": "api",
"key": "sk-your-autorouter-key"
},
"autorouter": {
"type": "api",
"key": "sk-your-autorouter-key"
}
}Or use the CLI:
opencode auth loginPick autorouter-anthropic and/or autorouter when prompted and paste your API key.
✅ Step 3: Verify
Launch OpenCode and type /models. You should see entries like autorouter-anthropic/claude-sonnet-4-6 and autorouter/deepseek-v4-pro. Pick a model and send a test message — a normal response means you're set.
❓ Troubleshooting
| Issue | Fix |
|---|---|
AutoRouter models don't show in /models | Check that provider.autorouter-anthropic.models or provider.autorouter.models is populated |
| Model not found | Confirm the model ID matches your AutoRouter 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 autorouter-anthropic provider (@ai-sdk/anthropic), not autorouter (@ai-sdk/openai-compatible) |
| Config changes not picked up | Restart OpenCode; per-project config wins over global |