Getting started

Introduction

OpenCode is Anomaly's open source coding agent, with a terminal, desktop, and web client. This reference covers v2: the TUI, the CLI, and opencode.jsonc.

Install

curl -fsSL https://opencode.ai/v2/install | bash  # macOS, Linux
brew install anomalyco/tap/opencode-v2            # Homebrew
npm install -g @opencode/cli                      # npm
bun install -g --trust @opencode/cli              # bun

Windows gets a standalone zip; the package managers cover macOS and Linux.

See: Intro

Connect a provider

/connect                        # add a provider account
/models                         # pick a model for this session
opencode auth login anthropic   # sign in from the CLI
opencode auth list              # list connected accounts

Catalogs come from models.dev, so most providers work once you connect an account or export its API key.

See: Providers

Sessions

opencode                          # start the full-screen TUI
opencode ~/code/project           # work in another project
opencode run "explain this repo"  # one-shot: prints and exits
opencode mini                     # minimal interactive interface
opencode run --continue "go on"   # continue the latest session

Sessions live in the shared server's database, so every client sees them. Use opencode session list --format json to inspect them.

See: CLI

CLI reference

Session commands

CommandDescription
opencode run <prompt>Send one prompt, then exit
opencode miniStart the minimal interface
opencode session listList saved sessions
opencode session export <id>Export a session as JSON

See: run

Setup commands

CommandDescription
opencode auth login <name>Sign in to a provider
opencode modelsList available models
opencode mcp add <name>Add an MCP server
opencode plugin listList installed plugins

See: auth

Service commands

CommandDescription
opencode service set <k> <v>Change shared server settings
opencode debug pathsPrint state and config paths

See: CLI

Run flags

FlagDescription
--model <provider/model>Choose the model for this run
--agent <name>Run as a specific agent
--session <id>Continue a session by ID
--continueContinue the latest session
--file <path>Attach a file to the prompt
--format jsonEmit newline-delimited JSON
--autoAuto-approve anything not denied
--archivedKeep the session out of lists
--standaloneRun on a private server

See: run

Shared server

opencode --standalone                # private server
opencode --server http://localhost:4096
opencode service set disabled true   # stop the shared service

One background server per user owns sessions and tools. In CI, opencode run --standalone gives a run its own server, so a secret reaches it.

See: Background service

Slash commands

Session

CommandDescription
/newStart a new session
/sessionsReturn to a saved session
/undoRevert the last message and its work
/redoRestore the reverted work
/compactSummarize to free up context
/btw <question>Ask a side question
/editorEdit the prompt in $EDITOR
/highlightsReplay the release notes

/btw answers from the session's context without adding it to the transcript. Esc interrupts a run.

See: TUI

Setup

CommandDescription
/connectAdd a provider account
/modelsChoose the session model
/agentsChoose the session agent
/mcpsInspect servers and sign in

See: MCP servers

Custom commands

---
description: Review code for correctness
agent: plan
---

Review $ARGUMENTS. Report bugs first.

Files in .opencode/commands/ become /name; nesting makes /team/review. $1 and $2 take positional arguments, and !`git diff` inserts shell output before the prompt is submitted.

See: Commands

Interactive mode

Leader keys

ShortcutAction
Ctrl+XLeader key for everything below
Ctrl+X NNew session
Ctrl+X LList sessions
Ctrl+X MChoose a model
Ctrl+X AChoose an agent
Ctrl+X EEdit the prompt in $EDITOR
Ctrl+X UUndo the last message
Ctrl+X RRedo
Ctrl+X CCompact the session

See: TUI

Other keys

ShortcutAction
Ctrl+PCommand palette
Ctrl+ORecent sessions and projects
Shift+TabCycle agents
F2Cycle recent models
Ctrl+TabNext session tab
Ctrl+BBackground blocking tools
EscInterrupt the session

See: Keybinds

Input modes

InputEffect
@Attach a file, with an optional range
!Run a shell command in the session
/Run a slash command

@src/auth.ts#20-45 attaches a line range. Enter steers a running session; Ctrl+X Enter queues the prompt. For a newline, use Ctrl+J or Shift+Enter.

See: TUI

Diff viewer

KeyAction
dChange the review scope
mMark the file reviewed
n / pNext or previous file
vToggle split or unified view
] / [Next or previous hunk
bToggle the file tree

Open it with /diff; d changes the scope and Base changes the branch. Set the default in cli.json with diffs.source (branch, committed, working, turn).

See: Keybinds

Rebinding

{
  "keybinds": {
    "leader": "ctrl+space",
    "model.list": "<leader>m",
    "app.debug": "none"
  }
}

Bindings live in ~/.config/opencode/cli.json, keyed by the IDs in the keybind table. "none" or false disables one, and leader rebinds the prefix.

See: Keybinds

Configuration

Config files

~/.config/opencode/opencode.jsonc  # global, every project
opencode.jsonc                     # project root, committed
.opencode/opencode.jsonc           # project, beats both above
/etc/opencode/opencode.json        # system dir (Linux), highest

Every .opencode config overrides every direct config; the system directory outranks both. On macOS that directory is /Library/Application Support/opencode. TUI settings live in ~/.config/opencode/cli.json.

See: Config

Providers and models

{
  "model": "anthropic/claude-sonnet-5-5",
  "providers": {
    "acme": {
      "env": ["ACME_API_KEY"],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": { "baseURL": "https://llm.acme/v1" },
      "models": { "qwen3-coder": {} }
    }
  }
}

References are provider/model, with an optional #variant for reasoning effort. Variants work in agents, commands, and --model; the root model key ignores them.

package reaches any OpenAI-compatible endpoint; catalog providers only need /connect.

See: Providers

Agents

AgentModePurpose
buildprimaryDefault coding agent
planprimaryPlans without editing files
generalsubagentResearch and multi-step work
exploresubagentSearch and read, never edit

Subagents run in background child sessions and report back when idle.

See: Agents

Custom agents

---
description: Reviews changes for correctness
mode: subagent
model: anthropic/claude-sonnet-5-5#high
---

Review the current changes.

Markdown agents live in .opencode/agents/ or ~/.config/opencode/agents/; the body becomes the system prompt. mode is primary, subagent, or all.

See: Agents

Permissions

{
  "permissions": [
    { "action": "shell", "resource": "*", "effect": "ask" },
    { "action": "shell", "resource": "git push *", "effect": "deny" }
  ]
}

Rules evaluate in order and the last match wins, so exceptions follow the broad rule. Anything unmatched asks.

Every agent starts with everything allowed except .env and .env.* reads, which ask; .env.example stays allowed.

Common actions are shell, edit, read, glob, grep, subagent, skill, webfetch, websearch, and external_directory; MCP tools get their own.

V2 retired v1's permission, bash, and task names.

See: Permissions

Instructions

~/.config/opencode/AGENTS.md  # every project
AGENTS.md                     # project, committed
packages/web/AGENTS.md        # loads when that area is read

Files combine from the global one down to the workspace; nested files load as the agent explores. V2 reads AGENTS.md only, not CLAUDE.md.

See: Instructions

MCP servers

opencode mcp add context7 --url https://mcp.context7.com/mcp
opencode mcp add files --global -- npx -y @acme/mcp-files
opencode mcp list

Remote servers use OAuth by default; run /mcps to finish a sign-in. Servers live under mcp.servers in the config.

See: MCP servers

Common workflows

Reviewing code

git diff | opencode run "review this diff for bugs"
opencode run --file src/auth.ts "review error handling"

In the TUI, /diff opens the diff viewer and m marks files reviewed.

See: TUI

JSON output

opencode run --format json "list the TODO comments" | jq

--format json emits newline-delimited JSON, so piping into jq works.

See: run

Archived runs

opencode run --archived "review this diff"

--archived keeps a run out of the session picker and inbox; opencode session list still shows it.

See: run

Undo and snapshots

/undo     # stage a rollback of the last message and its edits
/redo     # cancel the staged rollback and restore the work

/undo returns the prompt to the composer; submitting it commits the rollback. Snapshots bring files back too, and turn off with "snapshots": false.

See: Snapshots

Also see

0 Comments for this cheatsheet. Write yours!