Skip to content

CLI Command Reference

All commands and options for the opencode CLI tool

Course Notes

Key takeaways from this lesson:

CLI Command Reference Notes


Command Overview

CommandFunction
opencodeStart TUI interactive interface
opencode runExecute tasks in non-interactive mode
opencode serveStart headless server
opencode webStart Web interface
opencode attachConnect to remote server
opencode authAuthentication management
opencode modelsList available models
opencode agentAgent management
opencode mcpMCP server management
opencode sessionSession management
opencode statsUsage statistics
opencode exportExport session
opencode importImport session
opencode githubGitHub integration
opencode prPull and process PR
opencode acpACP server
opencode upgradeUpgrade version
opencode uninstallUninstall OpenCode

Main Commands

opencode

Start the TUI interactive interface.

bash
opencode [project]

Options:

OptionShortDescription
--continue-cContinue last session
--session-sSpecify session ID
--promptInitial prompt
--model-mSpecify model (format: provider/model)
--agentSpecify Agent
--portListen port
--hostnameListen address

Examples:

bash
# Start TUI
opencode

# Start with initial prompt
opencode --prompt "Help me analyze the code structure of this project"

# Use specific model
opencode -m anthropic/claude-sonnet-4-20250514

# Continue last session
opencode -c

opencode run

Execute tasks in non-interactive mode, suitable for scripts and CI/CD.

bash
opencode run [message..]

Options:

OptionShortDescription
--commandSlash command name to execute, message as command arguments
--continue-cContinue last session
--session-sSpecify session ID
--shareShare session
--model-mSpecify model (format: provider/model)
--agentSpecify Agent
--file-fAttach files (can be multiple)
--formatOutput format: default (formatted) or json (raw JSON)
--titleSession title
--attachConnect to running server (e.g. http://localhost:4096)
--portLocal server port (random by default)
--variantModel variant (reasoning effort: high, max, minimal)

Examples:

bash
# Basic usage
opencode run "Fix type errors in src/main.ts"

# Specify model
opencode run -m anthropic/claude-sonnet-4-5 "Review this code"

# Attach files (supports multiple files)
opencode run -f src/main.ts -f package.json "Analyze this project"

# Continue previous session
opencode run -c "What else needs to be done?"

# Use JSON output format (suitable for scripts)
opencode run --format json "List all TypeScript files"

# Connect to remote server (avoid MCP cold start)
opencode serve  # Start in another terminal
opencode run --attach http://localhost:4096 "Explain async/await"

# Use custom command
opencode run --command explain --file code.ts "How does this work?"

# Specify model variant (reasoning effort)
opencode run -m anthropic/claude-opus-4-5 --variant max "Analyze entire codebase"

# Auto-share session
opencode run --share "Generate project documentation"

# Specify session title
opencode run --title "Bug Fix" "Fix the login issue"

# Read input from stdin
echo "Count lines of code" | opencode run "Analyze"

opencode serve

Start headless server mode, providing API access.

bash
opencode serve

Options:

OptionDescription
--portListen port
--hostnameListen address
--mdnsEnable mDNS discovery
--corsAllowed CORS origins

Examples:

bash
# Start with default configuration
opencode serve

# Specify port and allow remote access
opencode serve --port 4096 --hostname 0.0.0.0

opencode web

Start the Web interface.

bash
opencode web

Options:

OptionDescription
--portListen port
--hostnameListen address
--mdnsEnable mDNS discovery
--corsAllowed CORS origins

Examples:

bash
# Start Web interface
opencode web

# Specify port
opencode web --port 4096

opencode attach

Connect to a remote OpenCode server.

bash
opencode attach [url]

Options:

OptionShortDescription
--dirTUI working directory
--session-sSpecify session ID

Examples:

bash
# Start server in one terminal
opencode web --port 4096 --hostname 0.0.0.0

# Connect from another terminal
opencode attach http://10.20.30.40:4096

Management Commands

opencode auth

Manage authentication and API keys. Credentials are stored in ~/.local/share/opencode/auth.json.

bash
opencode auth <subcommand>
SubcommandFunction
loginLogin (interactive provider selection)
list / lsList authenticated providers
logoutLogout from provider

Examples:

bash
# Interactive login
opencode auth login

# List authenticated providers
opencode auth list

# Logout
opencode auth logout

opencode models

List available models.

bash
opencode models [provider]

Options:

OptionDescription
--refreshRefresh model cache
--verboseShow detailed info (including cost and other metadata)

Examples:

bash
# List all available models
opencode models

# List only Anthropic models
opencode models anthropic

# Refresh model list
opencode models --refresh

opencode agent

Manage Agent configurations.

bash
opencode agent <subcommand>
SubcommandFunction
listList all Agents
createCreate new Agent (interactive)

Examples:

bash
# List Agents
opencode agent list

# Create new Agent
opencode agent create

opencode mcp

Manage MCP servers.

bash
opencode mcp <subcommand>
SubcommandFunction
list / lsList MCP servers and connection status
addAdd MCP server (interactive)
auth [name]OAuth authentication
auth list / auth lsList OAuth-supported servers and auth status
logout [name]Remove OAuth credentials
debug <name>Debug OAuth connection issues

Examples:

bash
# List MCP servers
opencode mcp list

# Add new server
opencode mcp add

# OAuth authentication
opencode mcp auth context7

# List OAuth status
opencode mcp auth ls

# Debug connection
opencode mcp debug context7

opencode session

Manage sessions.

bash
opencode session <subcommand>
SubcommandFunction
listList sessions

Options (list):

OptionShortDescription
--max-count-nLimit to last N sessions
--formatOutput format: table or json

Examples:

bash
# List sessions
opencode session list

# List last 10 sessions
opencode session list -n 10

# Output as JSON
opencode session list --format json

opencode stats

View usage statistics.

bash
opencode stats

Options:

OptionDescription
--daysStatistics for last N days
--toolsNumber of tools to display (shows all by default)
--modelsShow model usage details (pass number for Top N)
--projectFilter by project (empty string for current project)

Examples:

bash
# View statistics
opencode stats

# View last 7 days
opencode stats --days 7

# Show top 5 models
opencode stats --models 5

opencode export

Export session data as JSON.

bash
opencode export [sessionID]

If no session ID is specified, you will be prompted to select one.

Examples:

bash
opencode export abc123

opencode import

Import session data.

bash
opencode import <file>

Supports importing from local files or OpenCode share URLs.

Examples:

bash
# Import from file
opencode import session.json

# Import from share URL
opencode import https://opncd.ai/share/abc123

opencode github

GitHub integration management.

bash
opencode github <subcommand>
SubcommandFunction
installInstall GitHub Actions workflow
runRun GitHub Agent (for Actions)

run options:

OptionDescription
--eventGitHub mock event
--tokenGitHub personal access token

Examples:

bash
# Install Actions
opencode github install

opencode pr

Pull and checkout a GitHub PR branch, then start OpenCode.

bash
opencode pr <number>

This command will:

  1. Use gh pr checkout to pull the PR to local branch pr/<PR-number>
  2. Automatically add remote repository if it's a Fork PR
  3. Automatically import if PR description contains OpenCode session link
  4. Start OpenCode TUI

Prerequisites:

  • gh CLI installed and authenticated
  • Current directory is a Git repository

Examples:

bash
# Pull PR #123 and start OpenCode
opencode pr 123

# You will see:
# Fetching and checking out PR #123...
# Successfully checked out PR #123 as branch 'pr/123'
# Starting opencode...

opencode acp

Start ACP (Agent Client Protocol) server.

bash
opencode acp

Communicates via stdin/stdout using nd-JSON.

Options:

OptionDescription
--cwdWorking directory
--portListen port
--hostnameListen address

opencode upgrade

Upgrade to the latest version or a specific version.

bash
opencode upgrade [target]

Options:

OptionShortDescription
--method-mInstallation method: curl, npm, pnpm, bun, brew

Examples:

bash
# Upgrade to latest
opencode upgrade

# Upgrade to specific version
opencode upgrade v1.0.5

# Downgrade to 0.x
opencode upgrade 0.15.31

opencode uninstall

Uninstall OpenCode and delete related files.

bash
opencode uninstall

Options:

OptionShortDescription
--keep-config-cKeep configuration files
--keep-data-dKeep session data and snapshots
--dry-runOnly show what will be deleted
--force-fSkip confirmation prompt

Examples:

bash
# Complete uninstall
opencode uninstall

# Keep configuration
opencode uninstall --keep-config

# Preview deletion content
opencode uninstall --dry-run

Global Options

All commands support the following global options:

OptionShortDescription
--help-hShow help
--version-vShow version number
--print-logsPrint logs to stderr
--log-levelLog level: DEBUG, INFO, WARN, ERROR

Environment Variables

VariableTypeDescription
OPENCODE_CONFIGstringConfiguration file path
OPENCODE_CONFIG_DIRstringConfiguration directory path
OPENCODE_CONFIG_CONTENTstringInline JSON configuration
OPENCODE_PERMISSIONstringInline JSON permission configuration
OPENCODE_AUTO_SHAREbooleanAuto-share sessions
OPENCODE_DISABLE_AUTOUPDATEbooleanDisable auto-update check
OPENCODE_DISABLE_PRUNEbooleanDisable old data cleanup
OPENCODE_DISABLE_TERMINAL_TITLEbooleanDisable terminal title updates
OPENCODE_DISABLE_DEFAULT_PLUGINSbooleanDisable default plugins
OPENCODE_DISABLE_LSP_DOWNLOADbooleanDisable LSP server auto-download
OPENCODE_DISABLE_AUTOCOMPACTbooleanDisable auto context compression
OPENCODE_ENABLE_EXPERIMENTAL_MODELSbooleanEnable experimental models
OPENCODE_ENABLE_EXAbooleanEnable Exa web search
OPENCODE_CLIENTstringClient identifier (default cli)
OPENCODE_GIT_BASH_PATHstringWindows Git Bash path

Server Security

Authentication configuration for opencode serve and opencode web:

VariableTypeDescription
OPENCODE_SERVER_PASSWORDstringServer password (strongly recommended)
OPENCODE_SERVER_USERNAMEstringUsername (default opencode)

Security Notice

If OPENCODE_SERVER_PASSWORD is not set, the server will have no authentication protection, anyone can access it.

Examples:

bash
# Set server authentication
export OPENCODE_SERVER_PASSWORD=your-secure-password
export OPENCODE_SERVER_USERNAME=admin

opencode serve --hostname 0.0.0.0

Provider API Keys

API keys for each provider are set via corresponding environment variables:

VariableDescription
ANTHROPIC_API_KEYAnthropic API Key
OPENAI_API_KEYOpenAI API Key
DEEPSEEK_API_KEYDeepSeek API Key
GROQ_API_KEYGroq API Key

Experimental Variables

Source: cli.mdx

VariableTypeDescription
OPENCODE_EXPERIMENTALbooleanEnable all experimental features
OPENCODE_EXPERIMENTAL_ICON_DISCOVERYbooleanEnable icon discovery
OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECTbooleanDisable copy-on-select in TUI
OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MSnumberBash default timeout (milliseconds)
OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAXnumberLLM max output tokens
OPENCODE_EXPERIMENTAL_FILEWATCHERbooleanEnable directory file watching
OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHERbooleanDisable directory file watching
OPENCODE_EXPERIMENTAL_OXFMTbooleanEnable oxfmt formatter
OPENCODE_EXPERIMENTAL_LSP_TOOLbooleanEnable experimental LSP tool
OPENCODE_EXPERIMENTAL_LSP_TYbooleanEnable LSP type inference
OPENCODE_ENABLE_EXAbooleanEnable Exa code search