OpenCode — Getting Started

Install and configure OpenCode 2, the multi-provider AI coding agent. Covers V2 installation, provider setup, Console and Go pricing, and the first session.

October 20, 2026
opencodeterminal-agentmulti-providersetup

OpenCode — Getting Started

OpenCode is an AI coding agent that works with 75+ LLM providers. Unlike Claude Code (Anthropic-only) or Gemini CLI (Google-only), OpenCode lets you choose your model, pay what you want, and customize every aspect of the agent.

OpenCode 2 runs the agent as a shared background service. The terminal UI, web client, and desktop app all connect to the same server, so sessions and approvals stay in sync across them.

Installation

# Install script (recommended)
curl -fsSL https://opencode.ai/v2/install | bash

# Via npm
npm install -g @opencode/cli

# Via Homebrew
brew install anomalyco/tap/opencode-v2

# Verify
opencode --version

The npm package uses a postinstall script to select the native binary for your platform. With Bun or pnpm, allow the script explicitly:

bun install -g --trust @opencode/cli
pnpm add -g --allow-build=@opencode/cli @opencode/cli

Arch Linux users can install opencode-beta from the AUR (paru -S opencode-beta). Windows package managers are not supported; use the standalone binary from the download page.

Upgrading

opencode upgrade

If you are still on OpenCode 1, upgrade explicitly with opencode upgrade --major. V1 installs stay on the 1.x release line and are never offered 2.x as a routine update.

Provider Setup

OpenCode Provider Map

Run /connect in the TUI, choose a provider, and complete the auth flow. Then pick a model with /models. Credentials are stored in ~/.local/share/opencode/auth.json.

Most providers can also be detected from environment variables:

# Anthropic (via API key)
export ANTHROPIC_API_KEY="sk-ant-..."

# OpenAI
export OPENAI_API_KEY="sk-..."

# Google Gemini
export GOOGLE_API_KEY="..."

# DeepSeek (95% cheaper than Anthropic for coding)
export DEEPSEEK_API_KEY="sk-..."

# OpenRouter (300+ models, one API key)
export OPENROUTER_API_KEY="sk-or-..."

To configure a custom endpoint, add a providers block to ~/.config/opencode/opencode.jsonc or the project's opencode.jsonc:

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anthropic/claude-sonnet-5-5",
  "providers": {
    "anthropic": {
      "settings": {
        "apiKey": "{env:ANTHROPIC_API_KEY}"
      }
    },
    "deepseek": {
      "settings": {
        "apiKey": "{env:DEEPSEEK_API_KEY}"
      }
    },
    "local": {
      "name": "Local server",
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "http://127.0.0.1:1234/v1"
      },
      "models": {
        "coder": {
          "modelID": "model-name-on-server"
        }
      }
    }
  }
}

Ollama, LM Studio, and vLLM are discovered automatically when they run on their default ports. Prefer /connect and /models over hand-editing provider blocks unless you need a custom endpoint.

Pricing Plans

TierPriceWhat You Get
Free$0Bring your own API key, 75+ providers, local models, community skills
ConsolePay-as-you-go creditsManaged inference for proprietary and open models, usage tracking, team workspaces
Go$10/moMonthly usage limits on open coding models tested by the OpenCode team
Go Plus$40/moHigher limits on the same model list
Go Teams$20/member/moPer-member limits inside a Console workspace

Go is optional and works like any other provider: subscribe in the Console, copy the API key, then run /connect and select OpenCode Go.

Referral:

Sign up for OpenCode Go through this link and you get a $5 usage credit toward your Go usage limits — we get the same. No cost to you.

First Session

cd my-project
opencode

On first run, connect a provider and choose a model:

> /connect
> Select a provider (Anthropic, OpenAI, DeepSeek, Google, OpenRouter...)
> Enter your API key or sign in via your browser

> /models
> Select the model to use for this session

If you exported your provider's API key, OpenCode detects it automatically and you can skip /connect.

Prompt basics

InputWhat it does
@src/auth.tsAttach a file; add #20-45 for a line range
!git statusRun a shell command in the session
Shift+EnterNew line (Ctrl+J also works)
Ctrl+POpen the command palette
Shift+TabCycle agents (Build, Plan, and custom agents)
Ctrl+BMove running work to the background

Essential Slash Commands

CommandWhat it does
/connectAdd a provider and authenticate
/modelsList and select models — switch provider/model mid-session
/agentsChoose the active agent
/sessionsList and switch between sessions
/newStart a fresh session
/undoRevert the last message and its file changes
/redoRe-apply an undone message
/editorEdit the current prompt in $EDITOR
/btwAsk a side question without adding it to the conversation
/mcpsView, connect, or authenticate MCP servers
/highlightsReopen the release notes

If your build does not include an /init command, create AGENTS.md yourself, or ask the agent to inspect the repository and write one.

Plan Mode vs Build Mode

OpenCode ships two primary agents:

  • Build (build) — the default coding agent. It reads, writes, and edits files and runs commands within your permission rules.
  • Plan (plan) — explores and plans without editing normal project files. Shell commands remain permission-controlled.

Press Shift+Tab to cycle agents, or run /agents. Each agent can have its own model and permissions; define custom agents in .opencode/agents/<name>.md or under agents in opencode.jsonc.

Session Management

Every session's conversation history, file snapshots, and token usage are stored in a local SQLite database. Find the exact path with:

opencode debug paths db

The default is ~/.local/share/opencode/opencode.db.

CLI Commands

CommandPurpose
opencode run "message"One-shot prompt; --continue, --session <id>, --model, --agent, --format json, --file
opencode miniMinimal interactive interface instead of the full-screen TUI
opencode session listList sessions (--max-count, --format json)
opencode session delete <id>Delete a session and its children
opencode session export <id>Export a session as JSON (--sanitize to redact)
opencode session import <file>Import a session from a file or URL
opencode statsToken and cost usage (--days, --models, --cost, --project, --json)
opencode modelsList every available model
opencode auth listShow connected providers and accounts
opencode mcp listShow MCP servers and connection state
opencode plugin listShow installed plugins (--builtin includes built-ins)
opencode service statusCheck the background server
opencode api GET /api/sessionCall the server API directly
opencode pairPrint one-time links for the web or desktop client
opencode debug paths dbPrint the database path

Continuing Past Work

# Continue the most recent session
opencode run --continue "Now add tests"

# Continue a specific session by ID
opencode run --session ses_312c8fd8bffe5DMWmNRPKKLvt3 "Check edge cases"

# Pick up interactively, then choose the session with /sessions
opencode

For advanced querying — searching conversations by keyword, tracing tool-call sequences, auditing model costs — see Session History & Recovery.

Project Configuration: AGENTS.md

OpenCode reads AGENTS.md every session. Put it at the project root or next to the code it governs:

# AGENTS.md

## Commands
- `npm run dev` — Start development server at localhost:3000
- `npm run build` — Production build with type checking
- `npx vitest run` — Run all tests
- `npx eslint .` — Lint check

## Project Architecture
- Next.js 14 with App Router and React Server Components
- TypeScript strict mode with path alias @/
- SQLite via better-sqlite3 — no ORM, raw SQL
- Zero API dependencies for now

## Code Style
- Use async/await in server components for data fetching
- Extract business logic from components into lib/ immediately
- Tests go next to the file they test (co-location)

V2 discovers the global ~/.config/opencode/AGENTS.md and every AGENTS.md from the workspace toward home. It does not fall back to CLAUDE.md.

Provider Selection Strategy

If you need...Use...
Best reasoning for complex tasksAnthropic (Claude Sonnet/Opus)
Budget coding (95% cheaper)DeepSeek V4
Massive context (1M+ tokens)Google Gemini
Flexibility (300+ models)OpenRouter
Self-hosted / air-gappedOllama, LM Studio, or vLLM