OpenCode + OpenRouter Setup Guide
Developer Guide

OpenCode + OpenRouter
Setup Guide

How to use affordable AI models for coding, including DeepSeek, Qwen, and more, inside your terminal and IDE.

OpenCode OpenRouter VS Code Zed DeepSeek · Qwen

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.

Key Feature

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.

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.

Why Use OpenRouter with OpenCode?

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.

Before installing OpenCode, you’ll need an OpenRouter account and API key. This only takes a few minutes.

Tip

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.

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:

Terminal
npm install -g opencode-ai

Verify the installation worked by checking the version:

Terminal
opencode --version

If you see a version number printed out, OpenCode is installed correctly. To launch it, navigate to any project folder and run:

Terminal
cd /path/to/your/project
opencode
What you’ll see

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.

With OpenCode running, connecting your OpenRouter account is done entirely through the built-in /connect command. No manual config editing required.

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:

JSON · ~/.local/share/opencode/auth.json
{
  "openrouter": {
    "key": "sk-or-v1-YOUR_KEY_HERE"
  }
}
Privacy Note

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.

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
Tip: Start Cheap

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.

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:

OpenCode TUI Command
/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.

/models open model picker
/connect re-authenticate a provider
/help list all commands

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:

JSON · opencode.json
{
  "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:

Terminal
opencode --model openrouter/qwen/qwen3-coder
Example Workflow

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.

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

Inside Zed

Pro Tip: Split Your View

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.

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:

JSON · opencode.json
{
  // 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:

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.

A few recommendations to get the most out of OpenCode and keep your costs low: