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.
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

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
| Tier | Price | What You Get |
|---|---|---|
| Free | $0 | Bring your own API key, 75+ providers, local models, community skills |
| Console | Pay-as-you-go credits | Managed inference for proprietary and open models, usage tracking, team workspaces |
| Go | $10/mo | Monthly usage limits on open coding models tested by the OpenCode team |
| Go Plus | $40/mo | Higher limits on the same model list |
| Go Teams | $20/member/mo | Per-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
| Input | What it does |
|---|---|
@src/auth.ts | Attach a file; add #20-45 for a line range |
!git status | Run a shell command in the session |
Shift+Enter | New line (Ctrl+J also works) |
Ctrl+P | Open the command palette |
Shift+Tab | Cycle agents (Build, Plan, and custom agents) |
Ctrl+B | Move running work to the background |
Essential Slash Commands
| Command | What it does |
|---|---|
/connect | Add a provider and authenticate |
/models | List and select models — switch provider/model mid-session |
/agents | Choose the active agent |
/sessions | List and switch between sessions |
/new | Start a fresh session |
/undo | Revert the last message and its file changes |
/redo | Re-apply an undone message |
/editor | Edit the current prompt in $EDITOR |
/btw | Ask a side question without adding it to the conversation |
/mcps | View, connect, or authenticate MCP servers |
/highlights | Reopen 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
| Command | Purpose |
|---|---|
opencode run "message" | One-shot prompt; --continue, --session <id>, --model, --agent, --format json, --file |
opencode mini | Minimal interactive interface instead of the full-screen TUI |
opencode session list | List 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 stats | Token and cost usage (--days, --models, --cost, --project, --json) |
opencode models | List every available model |
opencode auth list | Show connected providers and accounts |
opencode mcp list | Show MCP servers and connection state |
opencode plugin list | Show installed plugins (--builtin includes built-ins) |
opencode service status | Check the background server |
opencode api GET /api/session | Call the server API directly |
opencode pair | Print one-time links for the web or desktop client |
opencode debug paths db | Print 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 tasks | Anthropic (Claude Sonnet/Opus) |
| Budget coding (95% cheaper) | DeepSeek V4 |
| Massive context (1M+ tokens) | Google Gemini |
| Flexibility (300+ models) | OpenRouter |
| Self-hosted / air-gapped | Ollama, LM Studio, or vLLM |
Related Pages
- OpenCode Configuration Reference — providers, permissions, MCP, skills, and plugins
- Prompt Engineering in OpenCode — AGENTS.md conventions and prompt patterns
- Session History & Recovery — query the local database
- MCP Setup in OpenCode — step-by-step MCP configuration
Related Articles & Guides
OpenCode — AI Coding Agent
Complete guide to OpenCode 2, the open-source coding agent with a shared background service, Code Mode, background subagents, and 75+ providers. Covers setup, configuration, and best practices.
#ompOh My Pi (OMP): Install, Connect Providers, and Start Coding
Install Oh My Pi (OMP), connect Anthropic, OpenAI, or OpenCode Go, pick a model, and run a small inspect-change-verify-resume workflow.
Pi — Terminal AI Coding Agent
Complete guide to pi, Mario Zechner's minimal terminal coding agent. Covers installation, prompt engineering, context files, skills, and the lean-harness philosophy.