OpenCode — Getting Started

Install and configure OpenCode, the multi-provider AI coding agent. Covers installation, LLM provider setup, Zen/Go pricing, and first session walkthrough.

June 16, 2026
opencodeterminal-agentmulti-providersetup

OpenCode — Getting Started

OpenCode is a terminal-based 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.

Installation

# Via npm
npm install -g opencode-ai

# Via Homebrew
brew install anomalyco/tap/opencode

# Verify
opencode --version

Provider Setup

OpenCode Provider Map

OpenCode supports any OpenAI-compatible endpoint. Choose your provider:

Quick Setup — Common Providers

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

Configure in ~/.config/opencode/opencode.json or opencode.json (project root):

{
  "$schema": "https://opencode.ai/config.json",
  "model": "anthropic/claude-sonnet-4-5",
  "provider": {
    "anthropic": {
      "options": {
        "apiKey": "{env:ANTHROPIC_API_KEY}"
      }
    },
    "openai": {
      "options": {
        "apiKey": "{env:OPENAI_API_KEY}"
      }
    },
    "deepseek": {
      "options": {
        "apiKey": "{env:DEEPSEEK_API_KEY}"
      }
    }
  }
}

Most providers are auto-detected from your exported API key — the provider block is only needed for custom endpoints or non-default options.

Pricing Plans

TierPriceWhat You Get
Free$0Bring your own API key, 75+ providers, community skills
ZenPay-as-you-goCurated, tested models. Free tier included — add credits ($10 minimum) for premium models
Go$10/moGenerous usage limits on top open-source coding models

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, OpenCode prompts you to connect a provider. Type /connect, pick your provider, and follow the auth flow (browser OAuth or paste an API key). Then run /models to 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 (see above), OpenCode detects it automatically and you can skip /connect.

Essential Slash Commands

CommandWhat it does
/helpShow all slash commands
/connectAdd a provider and authenticate
/modelsList and select available models — switch provider/model mid-session
/sessionsList and switch between sessions (aliases: /resume, /continue)
/initGenerate or update AGENTS.md for the project
/newStart a fresh session (alias: /clear)
/undoRevert the last message and its file changes
/redoRe-apply an undone message
/compactSummarize the current session to free context (alias: /summarize)
/shareCreate a shareable link for the session
/exportExport the conversation to a markdown file

Plan mode isn't a slash command — press Tab in the TUI to switch between Build and Plan mode.

Session Management

Every session's full conversation history, file snapshots, and token usage are stored in a SQLite database at ~/.local/share/opencode/opencode.db. Find the path with opencode db path.

TUI Commands

CommandWhat it does
/sessionsList all sessions, switch between them, or resume a past one
/newStart a fresh session
/undo / /redoStep backward and forward through message + file changes
/compactCompress a long session to reduce token usage — useful when you start hitting context limits
/shareGenerate a shareable link for the current session
/exportSave the conversation as a markdown file

CLI Commands

CommandPurpose
opencode session listList all sessions (-n <N> to limit, --format json for machine output)
opencode session delete <id>Delete a session
opencode statsShow token usage and cost (--days, --models, --project)
opencode export [id]Export a session as JSON (--sanitize to redact sensitive data)
opencode import <file or url>Import a session from a JSON file or share URL

Continuing Past Work

# Continue the most recent session
opencode -c

# Continue a specific session by ID
opencode -s ses_312c8fd8bffe5DMWmNRPKKLvt3

# Fork a session (branch without modifying the original)
opencode --fork --continue

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

Project Configuration: AGENTS.md

Like Claude Code's CLAUDE.md, OpenCode reads AGENTS.md every session. Create it at your project root:

# 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)

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 (local)

OpenCode's key advantage: switch providers mid-session with /provider. Debug with Sonnet, then switch to DeepSeek for less demanding work.