What is OpenCode?
OpenCode is an open-source AI coding agent available as a terminal interface (TUI), a desktop app for macOS, Windows, and Linux, and an IDE extension. It features LSP integration, multi-session support, sharable session links, and compatibility with 75+ LLM providers β including OpenRouter for unified multi-model access through a single API key. Both the terminal and desktop apps read the sameopencode.json config and the same stored credentials, so connecting OpenRouter once makes it available in either interface.
Get Your OpenRouter API Key
- Sign up or log in at OpenRouter
- Navigate to your API Keys page
- Click Create API Key and copy it (starts with
sk-or-v1-...)
Quick Start: Terminal
Step 1: Install OpenCode
- Install Script
- npm
- Homebrew
Step 2: Connect OpenCode to OpenRouter
OpenCode supports OpenRouter as a built-in provider. Use the interactive/connect command:
-
Start OpenCode in your project directory:
-
Run the
/connectcommand and search for OpenRouter: - Paste your OpenRouter API key when prompted.
-
Run
/modelsto select a model β many OpenRouter models are preloaded by default:
Quick Start: Desktop
The desktop app organizes projects and active sessions into tabs, and configures providers through a settings UI rather than slash commands.Step 1: Install OpenCode Desktop
- macOS
- Windows
- Linux
Step 2: Connect OpenRouter
- Launch OpenCode Desktop and open a project folder.
- Open Settings (or run Connect provider from the command palette).
- Under Providers, find OpenRouter in the Popular providers list and click Connect.
- Choose the API key method, paste your OpenRouter key, and confirm. OpenRouter appears under Connected providers.
Step 3: Pick a Model
Use the model picker in the session view, or run Choose model from the command palette, and select an OpenRouter model.If no OpenRouter models appear in the picker right after you add your API key, fully quit OpenCode Desktop and reopen it. The provider catalog is loaded at startup, so a newly connected provider may not surface until the app restarts.
Config File
Many OpenRouter models are preloaded, but you can add any others in youropencode.json config file. Use the modelβs OpenRouter slug as the key:
The leading
~ is not a typo. Slugs like ~anthropic/claude-sonnet-latest are OpenRouter latest aliases that always resolve to that labβs newest flagship model, so you keep getting the freshest version without editing your config. Pinned slugs such as anthropic/claude-sonnet-4.5 work exactly the same way β browse them all at openrouter.ai/models.opencode.json at your project root; user-wide settings go in ~/.config/opencode/opencode.json. Both formats also accept JSONC.
Setting a Default Model
To skip the model picker on startup, set a top-levelmodel key. OpenCode composes model IDs as provider_id/model_id, so an OpenRouter model is prefixed with openrouter/:
opencode run. You can also override it per invocation with the --model / -m flag.
Setting the API Key Manually
Keys added via/connect or the desktop settings UI are stored in ~/.local/share/opencode/auth.json, the same path on macOS, Linux, and Windows, and shared by the terminal and desktop apps. If you set XDG_DATA_HOME, substitute that directory. You can also write the file directly:
Provider Routing
When using OpenRouter, you can control which upstream providers handle your requests by addingoptions.provider to individual models in your config:
Why Use OpenRouter with OpenCode?
Access to Hundreds of Models
Switch between any model available on OpenRouter β Anthropic, OpenAI, Google, xAI, Meta, DeepSeek, and many more β without managing separate API keys for each provider.Provider Failover
If one provider is unavailable or rate-limited, OpenRouter automatically routes to another, keeping your coding sessions uninterrupted.Organizational Controls
For teams, OpenRouter provides centralized budget management. Set spending limits, allocate credits, and monitor usage across developers using OpenCode from your OpenRouter Activity Dashboard.Model Flexibility
Switch models with the/models command, the desktop model picker, or your config β no need to reconfigure API keys or endpoints.
Troubleshooting
- Auth Errors: Ensure your API key is set correctly via
/connect(or the desktop Providers settings). Runopencode auth listto confirm the credential was stored. Verify the key itself at openrouter.ai/settings/keys. - Model Not Found: Verify the slug on openrouter.ai/models and keep it exact, including any leading
~(e.g.~anthropic/claude-sonnet-latest). In a top-levelmodelkey, remember theopenrouter/provider prefix. - No Models After Connecting: Fully quit and restart OpenCode. The provider catalog is read at startup, so models from a newly connected provider may not appear until then.
- Model Missing From the Picker: Add it under
provider.openrouter.modelsin your config, then restart OpenCode. - Privacy: OpenRouter does not log your source code prompts unless you opt in to prompt logging. See our Privacy Policy for details.