What is OpenCode?
OpenCode is an open-source AI coding agent that runs directly in your terminal. Think of it like having a coding assistant that can read your files, run commands, edit code, and help you debug, all without leaving your command line.
It supports a Terminal User Interface (TUI), which gives you an interactive, keyboard-driven experience similar to a text editor, right inside your terminal window. It works inside any IDE that has a built-in terminal, including VS Code and Zed.
OpenCode connects to over 75 different AI model providers through a single tool. This means you can choose the cheapest, fastest, or most powerful model for any given task and switch between them instantly.
What is OpenRouter?
OpenRouter is a unified AI gateway. Instead of signing up for separate accounts at OpenAI, Anthropic, Google, DeepSeek, and others, you get one API key that gives you access to all of them, including dozens of affordable models like DeepSeek and Qwen.
You pay only for what you use. There is no subscription, no minimum spend, and no lock-in. Pricing is measured in tokens, and some models on OpenRouter cost as little as a fraction of a penny per thousand tokens, making them extremely affordable for development work.
OpenCode has OpenRouter built in as a first-class provider. That means connecting the two takes less than a minute, and you can switch between any of OpenRouter’s 200+ models without ever changing your API key or reconfiguring anything.
Get Your OpenRouter API Key
Before installing OpenCode, you’ll need an OpenRouter account and API key. This only takes a few minutes.
-
Create your account
Go to openrouter.ai and sign up for a free account using your email or Google login.
-
Navigate to API Keys
Once logged in, click your profile icon in the top right, then select Keys from the menu.
-
Create a new key
Click Create Key, give it a name (e.g. “OpenCode”), and copy the key that appears. Store it somewhere safe, as you won’t be able to see it again.
-
Add credits
Go to Credits in your account and add a small amount to start, as even $5 goes a long way with cheap models. OpenRouter only charges for actual usage.
You can set spending limits per key under the Key settings. This is useful if you want to cap how much a project can spend, or to prevent unexpected charges while experimenting.
Install OpenCode
OpenCode requires Node.js version 18 or higher. If you’re not sure what version you have, run node --version in your terminal. If you need to install or upgrade Node.js, visit nodejs.org.
Once Node.js is ready, install OpenCode globally with npm:
npm install -g opencode-ai
Verify the installation worked by checking the version:
opencode --version
If you see a version number printed out, OpenCode is installed correctly. To launch it, navigate to any project folder and run:
cd /path/to/your/project opencode
OpenCode will open its Terminal User Interface (TUI), a full interactive panel inside your terminal where you can type messages, see responses, and navigate with your keyboard. Press Ctrl+C at any time to exit.
Connect OpenRouter to OpenCode
With OpenCode running, connecting your OpenRouter account is done entirely through the built-in /connect command. No manual config editing required.
-
Open OpenCode in your project
In your terminal,
cdinto your project folder and runopencodeto launch the TUI. -
Type /connect
In the message input at the bottom of the TUI, type
/connectand press Enter. A list of available providers will appear. -
Search for OpenRouter
Start typing “openrouter” to filter the list, then select it with your arrow keys and press Enter.
-
Paste your API key
OpenCode will prompt you to enter your API key. Paste the key you copied from OpenRouter and press Enter.
That’s it. OpenRouter is now connected. Your key is saved to ~/.local/share/opencode/auth.json so you won’t need to re-enter it for future sessions.
Alternatively, if you prefer to set it manually, you can add your key directly to the auth file:
{
"openrouter": {
"key": "sk-or-v1-YOUR_KEY_HERE"
}
}
OpenRouter does not log your source code prompts by default. Prompt logging is opt-in only, which means your code stays private unless you explicitly choose to share it.
Recommended Affordable Models
OpenRouter gives you access to hundreds of models. The most cost-effective options for coding come from labs like DeepSeek and Qwen (by Alibaba). These models perform exceptionally well on coding tasks at a fraction of the cost of GPT-4 or Claude Opus.
Here is a comparison of the best affordable models available through OpenRouter right now:
| Model ID | Price (Input) | Context | Best For |
|---|---|---|---|
| deepseek/deepseek-chat-v4-flash | ~$0.23/M | 1M tokens | Fast everyday coding |
| deepseek/deepseek-chat-v4-pro | ~$2.87/M | 1M tokens | Complex multi-file tasks |
| qwen/qwen3-coder | Very low | 256K tokens | Code generation |
| qwen/qwen2.5-32b-instruct | ~$0.10/M | 128K tokens | Budget general coding |
| moonshot/kimi-k2 | ~$1.09/M | 262K tokens | Large codebases |
| nvidia/llama-3.1-nemotron-ultra:free | Free | 128K tokens | Zero-cost experimenting |
For most day-to-day coding tasks, DeepSeek V4 Flash or Qwen3-Coder are excellent starting points. They’re fast, affordable, and capable. Save the more expensive Pro models for complex refactoring or architecture-level tasks.
Switching Between Models
One of OpenCode’s most powerful features is how easy it is to switch models. You can do this mid-session without restarting, and your OpenRouter key stays the same regardless of which model you choose.
Method 1: Using the /models Command (Recommended)
Inside any active OpenCode session, type the following and press Enter:
/models
A searchable list of all available models will appear. Use your arrow keys to navigate, or start typing a model name to filter. Press Enter to select. The model switches immediately for your next message.
Method 2: Set a Default Model in Your Config File
If you always want to start with a specific model, set it as the default in your opencode.json config file. Create or edit this file in your project root:
{
"model": "openrouter/deepseek/deepseek-chat-v4-flash"
}
Method 3: Pass a Model via the CLI (One-Off Use)
If you want to run OpenCode with a specific model just for that session without changing your config, pass it as a flag when launching:
opencode --model openrouter/qwen/qwen3-coder
A practical approach: start a session with DeepSeek V4 Flash for speed and low cost. When you hit a complex architectural problem, run /models and switch to DeepSeek V4 Pro for that conversation, then switch back. You are only billed for the tokens used by each respective model.
Using OpenCode Inside VS Code or Zed
OpenCode runs in any terminal, which means it works seamlessly inside the built-in terminals of both VS Code and Zed. No extension or plugin is needed.
Inside VS Code
-
Open the integrated terminal
Press
Ctrl+`on Windows/Linux, orCmd+`on macOS, to open the VS Code terminal panel. -
Navigate to your project
If your project is already open in VS Code, the terminal should already be in the right folder. Confirm with
pwd. -
Launch OpenCode
Type
opencodeand press Enter. The TUI will open inside the terminal panel. You can resize the panel to give it more space.
Inside Zed
-
Open the terminal panel
Press
Ctrl+`or go to View → Terminal to open Zed’s built-in terminal. -
Launch OpenCode
Type
opencodeand press Enter. The TUI launches in the same panel, right alongside your code editor.
In both VS Code and Zed, you can split your editor and terminal side-by-side. This lets you see your code on the left and chat with OpenCode on the right simultaneously, which is a great workflow for active development sessions.
Config File Reference
OpenCode looks for a config file called opencode.json in your project root or your global config directory. This file lets you set a default model, customize behavior, and persist your preferences across sessions.
Here is a full example showing the most useful settings:
{
// Set the default model to use on launch
"model": "openrouter/deepseek/deepseek-chat-v4-flash",
// Optional: Keep conversation history between sessions
"autosave": true,
// Optional: Set the theme (dark, light, or a custom theme name)
"theme": "dark"
}
You can override the model in config on a per-project basis by placing a different opencode.json in each project folder. This is useful if some projects need a heavier model and others work fine with the cheapest option.
Model ID Format for OpenRouter
When specifying an OpenRouter model in your config or CLI flag, use this format:
openrouter/{provider}/{model-name} Examples: openrouter/deepseek/deepseek-chat-v4-flash openrouter/deepseek/deepseek-chat-v4-pro openrouter/qwen/qwen3-coder openrouter/qwen/qwen2.5-32b-instruct openrouter/moonshot/kimi-k2
You can browse all available model IDs at openrouter.ai/models. Each model’s page shows the exact ID string to use.
Tips & Best Practices
A few recommendations to get the most out of OpenCode and keep your costs low:
-
Start cheap, upgrade when needed
Default to DeepSeek V4 Flash or Qwen3-Coder for routine tasks. Only switch to a more capable model when you’re dealing with complex logic, large refactors, or architecture decisions.
-
Keep context focused
AI models charge per token, including your input. The more files and context you feed into a session, the more it costs. Open only the files relevant to the task at hand.
-
Use free models for exploration
OpenRouter has a number of completely free models (like NVIDIA Nemotron). Use these for experimenting, learning, or low-stakes tasks to preserve your credits for real work.
-
Set a spending limit on your key
In your OpenRouter dashboard, you can cap spending per API key. Set a monthly limit so you never accidentally run up a large bill while experimenting with a new model.
-
Use per-project config files
Place a custom
opencode.jsonin each project folder to set the best model for that project. A small utility script might use the cheapest model, while a large production codebase gets the more powerful one. -
Check the activity dashboard
OpenRouter provides a usage dashboard at openrouter.ai/activity where you can see exactly how many tokens each model consumed and what it cost. Review this periodically to optimize your usage.