Skip to main content

CLI Usage

The opencode CLI provides both interactive and non-interactive modes for every workflow -- from local development to CI/CD automation.

Interactive Mode​

Running opencode without arguments launches the Terminal User Interface (TUI):

opencode

You can optionally specify a project directory:

opencode /path/to/project

Or pass a prompt directly to start a conversation immediately:

opencode "Explain the architecture of this project"

When a prompt is passed as an argument, opencode starts the TUI and sends the prompt right away.

tip

You can pipe prompts directly: echo "Explain this project" | opencode

Non-Interactive Mode​

Use opencode run to execute prompts without the TUI. This is ideal for scripts, automation, and CI/CD pipelines.

Basic Usage​

opencode run "Review the changes in the working directory"

Mode Flag​

The --mode flag lets you specify which agent to use:

opencode run "Add user authentication" --mode plan
opencode run "Implement the login endpoint" --mode build

Available modes: build, plan, general, explore, scout, or any custom agent name.

Model Selection​

Specify which model to use:

opencode run "Refactor this module" --model claude-sonnet-4-20250514
opencode run "Write unit tests" --model gpt-4o

Output to File​

Write the response to a file:

opencode run "Generate API documentation" --output docs/api.md

Working Directory​

Run opencode in a specific project directory:

opencode run "Check for type errors" --dir /path/to/project

Provider and Config Override​

opencode run "Summarize changes" --provider openai --model gpt-4o --config ./custom-config.json
note

Set OPENCODE_LOG_LEVEL=debug in your environment for persistent debug logging.

Session ID​

Continue a specific session:

opencode run --session abc123 "Add error handling"

Headless Server​

Start opencode as a headless API server:

opencode serve

This runs opencode as an HTTP server listening on port 4096 by default. Other clients (including the TUI) can attach to it, sharing the same session and context.

Configuration options:

FlagDefaultDescription
--port4096Server listening port
--hostnamelocalhostBind address
--corsunsetCORS origin (e.g., *)

Client Attach​

Once the server is running, attach a TUI client:

opencode attach http://localhost:4096

Or send prompts non-interactively:

opencode run --attach http://localhost:4096 "Add pagination"

Web Interface​

Start opencode with a browser-based UI:

opencode web

This starts an HTTP server and opens the web interface in your default browser. See the Web Interface guide for details.

Agent Management​

Create an Agent​

Create a new agent interactively:

opencode agent create

This launches a guided setup for defining agent name, system prompt, provider, model, and permitted tools.

List Agents​

opencode agent list

Shows all configured agents with their current settings.

Delete an Agent​

opencode agent delete <name>

Authentication​

Login​

Authenticate with opencode services (OpenCode Zen, session sharing, etc.):

opencode auth login

Opens a browser window for authentication.

List Authenticated Accounts​

opencode auth list

Logout​

opencode auth logout [account-id]

With no arguments, logs out of all accounts.

Models​

List Models​

List all available models for your configured providers:

opencode models

Filter by provider:

opencode models anthropic
opencode models openai

Refresh Model Cache​

Fetch the latest model lists from providers:

opencode models --refresh

Session Management​

List Sessions​

opencode session list

Outputs a table of all saved sessions with ID, project, model, and timestamp.

Delete a Session​

opencode session delete <session-id>

Delete multiple sessions:

opencode session delete abc123 def456

Delete All Sessions​

opencode session delete --all

Export Session​

Export a session to a file:

opencode export <session-id>

Writes the full session transcript to a file. The filename defaults to the session ID with a .md extension.

Specify an output path:

opencode export <session-id> --output ./exports/session-review.md

Import Session​

Import a previously exported session:

opencode import ./exports/session-review.md

Statistics​

View token usage and cost estimates:

opencode stats

Output example:

Token Usage
┌──────────────────────────────────────────────┬───────────┬──────────┐
│ Model │ Tokens │ Cost │
├──────────────────────────────────────────────┼───────────┼──────────┤
│ claude-sonnet-4-20250514 │ 142,350 │ $2.85 │
│ gpt-4o │ 89,200 │ $0.89 │
├──────────────────────────────────────────────┼───────────┼──────────┤
│ Total │ 231,550 │ $3.74 │
└──────────────────────────────────────────────┴───────────┴──────────┘

GitHub Integration​

Install GitHub Action​

opencode github install

Sets up the opencode GitHub Action in your repository.

Run on GitHub​

opencode github run

Runs opencode in the context of the current GitHub repository, useful for local testing of GitHub Action workflows.

MCP Management​

Add an MCP Server​

opencode mcp add my-server --command "npx @myorg/mcp-server"

With environment variables:

opencode mcp add db --command "node" --args "server.js" --env "DB_URL=$DATABASE_URL"

List MCP Servers​

opencode mcp list

Register MCP Auth​

opencode mcp auth <server-name>

Plugin System​

Install a plugin:

opencode plugin <module>

Plugins extend opencode with additional functionality. Available plugins are listed in the Integrations guide.

Debugging​

Debug Mode​

Run in debug mode to collect troubleshooting information:

opencode debug

This outputs system information, config paths, provider status, and recent logs.

opencode --print-logs

Streams log output to stdout for debugging.

Log Level​

Set the log verbosity:

opencode --log-level debug run "test connection"
opencode --log-level trace run "test connection"

Levels: trace, debug, info, warn, error, fatal.

Updating​

Check for Updates​

opencode upgrade

If a new version is available, it will be downloaded and installed.

Install Specific Version​

opencode upgrade 0.6.0

Examples​

Daily Standup Summary​

opencode run "Summarize the changes in today's commits" --dir ./repo --output daily-summary.md

Code Review​

opencode run "Review the diff for security vulnerabilities, performance issues, and code style problems" --model claude-sonnet-4-20250514

Generate Tests​

opencode run "Write unit tests for all functions in src/utils/ with 80% coverage" --mode plan
opencode run "Implement the tests from the plan" --mode build

CI/CD Pipeline​

jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: anomalyco/opencode-action@v1
with:
prompt: "Review this PR for bugs, security issues, and style problems"
api-key: ${{ secrets.OPENCODE_API_KEY }}
model: claude-sonnet-4-20250514

Batch Process Multiple Files​

for file in src/**/*.ts; do
opencode run "Add JSDoc comments to $file" --output /dev/null
done