OpenCode with AnyRouter — free models, one API key
Point OpenCode at AnyRouter's base URL. Use free models with anyrouter/free, or any Chat Completions model with one sk-ar- key.
Run OpenCode on any AnyRouter model with one sk-ar- key. It is a config-only change. Flags, profiles and login details are in the OpenCode CLI setup; to add AnyRouter's MCP tools see MCP with OpenCode.
Set it up
One command handles login, the key, and a session-only provider (opens a browser, or shows a device code over SSH):
curl -fsSL https://anyrouter.dev/cli.sh | bash
anyr opencodePin a model or forward flags: anyr opencode --model "z-ai/glm-5.2", anyr opencode --model auto -- --help.
-
Create a key in the dashboard and export it:
export ANYROUTER_API_KEY="sk-ar-your-anyrouter-key" -
Add the provider to
~/.config/opencode/opencode.json(all projects) or./opencode.json:{ "$schema": "https://opencode.ai/config.json", "provider": { "anyrouter": { "npm": "@ai-sdk/openai-compatible", "name": "AnyRouter", "options": { "baseURL": "https://anyrouter.dev/api/v1", "apiKey": "{env:ANYROUTER_API_KEY}" }, "models": { "anthropic/claude-sonnet-4-6": { "name": "Claude Sonnet 4.6", "limit": { "context": 200000, "output": 65536 } } } } }, "model": "anyrouter/anthropic/claude-sonnet-4-6" }apiKeyis the env var name in{env:...}form, never the secret. If you setlimit, set bothcontextandoutput. -
Run
opencode, open/models, and choose ananyrouter/...id.
Confirm the call appears in your request logs.
Free models and auto routing
| Model id | Behavior |
|---|---|
anyrouter/free | Best free model available now, with fallback. No credits. Unlock with a $5 top-up, Go or higher, or one donated key; no daily cap. See Free Tier. |
anyrouter/auto | Top free and paid models with health-aware failover. Paid members use credits. |
anyr opencode --model anyrouter/freeIn a persistent config, add "anyrouter/free": { "name": "AnyRouter Free" } to models and select anyrouter/anyrouter/free. A 400 no_eligible_models means your privacy or routing settings (for example Zero Data Retention or blocked providers) excluded every candidate: relax them or pick a specific model.
To use your own provider keys, see BYOK.
MCP auto-connect
anyr opencode connects the AnyRouter MCP gateway with the session key when Auto-connect in coding agents is on in the dashboard. Turn it off with --no-mcp, anyr mcp auto off, or in the dashboard.
Troubleshooting
| Symptom | Fix |
|---|---|
| 401 or 403 | Check the key is active, and that ANYROUTER_API_KEY is set in OpenCode's environment or a CLI profile exists (anyr config get). |
| Model not found | The models key must match an id in the catalog, and you select it as anyrouter/<id>. |
Invalid config, missing limit.output | Set both limit.context and limit.output, or omit limit. |
| Another provider answers | A /connect login (OpenCode Zen) or leftover openai provider can win. Pick the anyrouter/... model in /models, or launch with anyr opencode. |
AnyRouter routes requests to third-party models. Models and outputs are provided as is, with no warranty; you use them at your own risk. Disclaimer