Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

OpenCode CLI Usage

OpenCode CLI Usage

OpenCode provides a powerful command-line interface (CLI) that can not only launch an interactive TUI but also enable automation and scripting operations through commands.

By default, OpenCode CLI launches the TUI, but its real power lies in programmable invocation.

OpenCode CLI is not just a command tool, but a programmable AI development interface.

OpenCode CLI can be abstracted into three layers:

👉 Essentially, it is aAI Agent + local development environment + programmable CLI + Server APIunified entry point.

Below is the complete table of OpenCode CLI commands:

Basic behaviors

Command/Usage Description Notes
opencode Launch TUI by default When no arguments are given
opencode run "msg" Execute tasks non-interactively Automation/Scripting

tui launch arguments

Flag Shorthand Description
--continue -c Continue the last session
--session -s Specify session ID
--fork — Fork session
--prompt — Initial prompt
--model -m Specify model
--agent — Specify agent
--port — Listening port
--hostname — Host address

Core CLI commands

Command Description Key subcommands
agent Manage agents create / list
auth Manage authentication login / list / logout
attach Connect to remote backend —
github GitHub automation install / run
mcp MCP service management add / list / auth / debug
models View model list —
run Non-interactive execution —
serve Start API service —
web Web UI service —
session Session management list
stats Token/cost statistics —
export Export session —
import Import session —
acp Start ACP protocol service —
uninstall Uninstall —
upgrade Upgrade version —

run command arguments

Flag Shorthand Description
--continue -c Continue session
--session -s Specify session
--fork — Fork
--share — Share session
--model -m Specify model
--agent — Specify agent
--file -f Attach file
--format — Output format (json)
--title — Session title
--attach — Connect to existing service
--port — Local port

Service-type command arguments

serve / web / acp

Flag Description
--port Listening port
--hostname Host address
--mdns LAN discovery
--cors Allow cross-origin

session / stats arguments

Command Flag Description
session --max-count (-n) Latest N entries
session --format table/json
stats --days Last N days
stats --tools Tool statistics
stats --models Model usage
stats --project Project filter

Global flags

Flag Shorthand Description
--help -h Help
--version -v Version
--print-logs — Output log
--log-level — Log level

Environment Variables

Variables Type Purpose
OPENCODE_AUTO_SHARE boolean Auto-share
OPENCODE_CONFIG string Configuration paths
OPENCODE_TUI_CONFIG string TUI configuration
OPENCODE_DISABLE_AUTOUPDATE boolean Disable updates
OPENCODE_SERVER_PASSWORD string API authentication
OPENCODE_MODELS_URL string Model source

Experimental features

Variables Type Purpose
OPENCODE_EXPERIMENTAL boolean Enable all experimental features
OPENCODE_EXPERIMENTAL_LSP_TOOL boolean LSP tools
OPENCODE_EXPERIMENTAL_PLAN_MODE boolean Plan mode
OPENCODE_EXPERIMENTAL_FILEWATCHER boolean File watching

I. Basic usage

1. Launch TUI (default behavior)


opencode

Equivalent to entering the interactive terminal interface

2. Specify project directory


opencode /path/to/project

Launch TUI in a specified directory, for example, launch in the opencode-runoob-test directory:

opencode opencode-runoob-test

3. Non-interactive mode (script invocation)


opencode run 解释一下 JavaScript 中闭包的原理

Return results directly without entering the interface.


II. Core CLI capabilities

The core capabilities of OpenCode CLI can be summarized into three categories:


III. Common arguments (Flags)

Parameters Shorthand Description
--continue -c Continue the previous session
--session -s Specify session ID
--fork Fork from the current session
--model -m Specify model (provider/model)
--agent Specify agent
--port Service port
--hostname Listening address

IV. Core command details

1. run (most commonly used)

Used for non-interactive execution:


opencode run 分析这个 Go 项目的上下文使用方式

Applicable Scenarios:

Connect to background service (avoid cold start)


# Start background service
opencode serve

# Use run to connect
opencode run --attach http://localhost:4096 解释 async/await

Common parameters

Parameters Description
--model Specify model
--file Attach file
--format Output format (json / default)
--share Share session

V. auth (authentication management)

Login


opencode auth login

Configure API Key (stored locally)


View logged-in providers


opencode auth list

Log out


opencode auth logout

VI. models (model management)

View all models


opencode models

Filter by provider


opencode models anthropic

Refresh model list


opencode models --refresh

VII. agent (agent system)

Create agent


opencode agent create

Customizable prompt + tools

View agents


opencode agent list

VIII. session (session management)

View sessions


opencode session list

Limit quantity


opencode session list -n 10

IX. serve (service mode)

Start API service:


opencode serve

Provide HTTP interface (no UI)

Optional parameters:


X. web (Web UI)


opencode web

Launch the version with a web interface


XI. mcp (extension capabilities)

Add MCP service


opencode mcp add

View list


opencode mcp list

MCP is used to extend AI capabilities (such as tool calls)


XII. stats (usage statistics)


opencode stats

View Token usage and costs


XIII. export / import

Export session


opencode export

Import session


opencode import session.json

XIV. Upgrade and uninstall

Upgrade


opencode upgrade

Uninstall


opencode uninstall

XV. Global arguments

Parameters Description
--help View help
--version Check the version
--log-level Log level

XVI. Environment variables (advanced configuration)

OpenCode supports controlling behavior through environment variables, for example:


OPENCODE_DISABLE_AUTOUPDATE=true
OPENCODE_SERVER_PASSWORD=123456

Common uses:


XVII. Usage recommendations (key points)