For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
ModelsChatRankingsDocs
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
DocsAPI ReferenceClient SDKsAgent SDKCookbookChangelog
  • Get Started
    • Quickstart: Build a Chat App
    • Enterprise Quickstart
    • Free Models Router
  • Working with Coding Agents
    • Automatic Code Review
    • Claude Code
    • Claude Desktop
    • Codex CLI
    • Cursor
    • Hermes Agent
    • Junie CLI
    • MCP Servers
    • OpenClaw 🦞
    • OpenCode
  • Building Agents
    • Add Human-in-the-Loop Controls
    • Build a Long-Horizon Agent
    • Build Your Own Agent TUI
    • Build Your Own Headless Agent
  • Video Generation
    • Choose a Video Generation Model
    • Generate and Download a Video from Text
    • Get Video Results with Webhooks
    • Guide a Video with Reference Images
    • Turn an Image into a Video
    • Use Provider-Specific Video Options
  • Evaluate & Optimize
    • Distillation
    • RAG with Embeddings & Rerank
    • Red Teaming
  • Administration
    • Activity Export
    • API Key Rotation
    • Crypto API
    • Organization Management
    • Usage Accounting
    • User Tracking
LogoLogo
ModelsChatRankingsDocs
On this page
  • What is OpenCode?
  • Quick Start
  • Step 1: Install OpenCode
  • Step 2: Get Your OpenRouter API Key
  • Step 3: Connect OpenCode to OpenRouter
  • Alternative: Config File
  • Provider Routing
  • Why Use OpenRouter with OpenCode?
  • Access to Hundreds of Models
  • Provider Failover
  • Organizational Controls
  • Model Flexibility
  • Troubleshooting
  • Resources
Working with Coding Agents

OpenCode

Use OpenCode with OpenRouter
Was this page helpful?
Previous

Add Human-in-the-Loop Controls to an Agent SDK Agent

Add HITL to an existing Agent SDK agent so it can pause high-stakes tool calls for human input

Next
Built with

What is OpenCode?

OpenCode is an open-source AI coding agent available as a terminal interface and desktop app (beta). 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.

Quick Start

Step 1: Install OpenCode

Install Script
npm
Homebrew
$curl -fsSL https://opencode.ai/install | bash

For additional installation methods (Bun, pnpm, Yarn, Arch Linux, Windows), see the OpenCode installation docs.

Step 2: Get Your OpenRouter API Key

  1. Sign up or log in at OpenRouter
  2. Navigate to your API Keys page
  3. Create a new API key
  4. Copy your key (starts with sk-or-...)

Step 3: Connect OpenCode to OpenRouter

OpenCode supports OpenRouter as a built-in provider. Use the interactive /connect command:

  1. Start OpenCode in your project directory:

    $cd /path/to/your/project
    $opencode
  2. Run the /connect command and select OpenRouter:

    /connect
  3. Paste your OpenRouter API key when prompted.

  4. Run /models to select a model:

    /models

Your requests will now be routed through OpenRouter.

Alternative: Config File

You can also configure OpenRouter directly in your opencode.json config file:

1{
2 "$schema": "https://opencode.ai/config.json",
3 "provider": {
4 "openrouter": {
5 "models": {
6 "~anthropic/claude-sonnet-latest": {},
7 "~google/gemini-flash-latest": {}
8 }
9 }
10 }
11}

Set your API key via the /connect command or by adding it to ~/.local/share/opencode/auth.json:

1{
2 "openrouter": {
3 "type": "api",
4 "key": "sk-or-your-key-here"
5 }
6}

Provider Routing

When using OpenRouter, you can control which upstream providers handle your requests by adding options.provider to individual models in your config:

1{
2 "$schema": "https://opencode.ai/config.json",
3 "provider": {
4 "openrouter": {
5 "models": {
6 "~anthropic/claude-sonnet-latest": {
7 "options": {
8 "provider": {
9 "order": ["anthropic"],
10 "allow_fallbacks": true
11 }
12 }
13 }
14 }
15 }
16 }
17}

For a full breakdown of routing options, see the Provider Routing docs.

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 by updating your config or using the /models command — no need to reconfigure API keys or endpoints.

Troubleshooting

  • Auth Errors: Ensure your API key is set correctly via /connect. Check at openrouter.ai/keys.
  • Model Not Found: Verify the model ID on openrouter.ai/models. Use the exact format (e.g., ~anthropic/claude-sonnet-latest).
  • Privacy: OpenRouter does not log your source code prompts unless you opt in to prompt logging. See our Privacy Policy for details.

Resources

  • OpenCode Website
  • OpenCode on GitHub
  • OpenCode Provider Docs
  • OpenRouter Models