文档
English Docs
CLI

CLI

The OpenCode CLI provides command-line access to OpenCode functionality. By default, running opencode starts the terminal user interface (TUI).

opencode

Pass commands for programmatic interaction:

opencode run "Explain how closures work in JavaScript"

TUI Flags

FlagShortPurpose
--continue-cResume previous session
--session-sSpecify session ID
--promptProvide initial prompt
--model-mSet model (provider/model)
--agentSelect agent

Commands

run

Execute OpenCode in non-interactive mode:

opencode run [message..]

Flags:

  • --continue, -c: Resume last session
  • --model, -m: Set model
  • --file, -f: Attach file(s)
  • --format: Output as default or json

auth

Manage credentials:

opencode auth login    # Configure API keys
opencode auth list     # Display providers
opencode auth logout   # Remove credentials

models

List available models:

opencode models [provider]
opencode models --refresh  # Update cached list
opencode models --verbose  # Show costs

session

Manage sessions:

opencode session list
opencode session list --format json

serve

Start headless HTTP server:

opencode serve --port 4096

web

Start server with web interface:

opencode web --port 4096

agent

Manage agents:

opencode agent create  # Create custom agent
opencode agent list    # List agents

mcp

Manage MCP servers:

opencode mcp add       # Add server
opencode mcp list      # Show servers
opencode mcp auth      # Authenticate
opencode mcp debug     # Troubleshoot

stats

Display usage statistics:

opencode stats --days 30
opencode stats --models

upgrade

Update OpenCode:

opencode upgrade           # Latest version
opencode upgrade v0.1.48   # Specific version

Global Flags

FlagShortPurpose
--help-hDisplay help
--version-vPrint version
--print-logsOutput logs to stderr
--log-levelSet level (DEBUG, INFO, WARN, ERROR)

Environment Variables

Core Configuration

VariablePurpose
OPENCODE_CONFIGConfig file path
OPENCODE_CONFIG_DIRConfig directory
OPENCODE_CONFIG_CONTENTInline JSON config

Features

VariablePurpose
OPENCODE_DISABLE_AUTOUPDATEDisable updates
OPENCODE_ENABLE_EXPERIMENTAL_MODELSEnable experimental models
OPENCODE_DISABLE_AUTOCOMPACTDisable compaction