Skip to main content

OpenCode CLI

OpenCode is an open-source terminal coding agent. It connects to many AI providers (Anthropic, OpenAI, Google, and local models via Ollama), reads your files, edits code, and runs terminal commands.

Installation

npm install -g opencode-ai

Or with a shell script:

curl -fsSL https://opencode.ai/install | bash

Start a Session

opencode

This opens an interactive session in your current directory.

Run a One-Off Task

opencode "Add error handling to the fetch calls in src/api.ts"

Pass a prompt directly to run a task without entering the interactive mode.

Configure a Provider

OpenCode uses a config file at ~/.config/opencode/config.json. Set the provider and model you want to use:

{
"provider": "anthropic",
"model": "claude-opus-4-8"
}

For Anthropic, set your API key:

export ANTHROPIC_API_KEY=your_api_key_here

Use a Local Model (Ollama)

Start Ollama with your model running, then launch OpenCode pointing to it:

opencode --provider ollama --model qwen3:14b

Or launch it directly from Ollama:

ollama launch opencode

Useful Flags

FlagWhat it does
--provider <name>Choose the AI provider (anthropic, openai, ollama, etc.)
--model <id>Choose a specific model
--config <path>Use a custom config file
--verboseShow detailed logs of what OpenCode is doing
--no-auto-approveAsk for confirmation before applying each change

Useful In-Session Commands

CommandWhat it does
/helpShow all available commands
/clearClear the conversation history
/modelSwitch to a different model
/providerSwitch to a different provider
/undoUndo the last change OpenCode made
/diffShow the last diff OpenCode produced
/exitExit OpenCode

Example: Fix a Bug

opencode "The /orders endpoint returns an empty list when the user has no orders. It should return an empty array, not null."

OpenCode reads the relevant files, applies the fix, and shows a diff.

Example: Refactor Code

cd my-project
opencode "Refactor the database queries in src/db.ts to use async/await instead of callbacks."

OpenCode explores the codebase and edits the files.

Example: Ask Without Editing

opencode "Explain how the retry logic in src/client.ts works"

OpenCode reads the file and answers without making any changes.

Providers

OpenCode supports many providers out of the box:

ProviderExample model
anthropicclaude-opus-4-8
openaigpt-4o
googlegemini-2.0-flash
ollamaqwen3:14b
groqllama-3.3-70b-versatile