OpenCode CLI Cheatsheet - Flags, Agents & Serve

OpenCode positions itself as an open-source, self-hostable terminal coding agent: the TUI matches Claude Code in shape, but provider and model are freely swappable and external tools can be attached over MCP. So the trade-off is not raw capability but vendor lock-in - if you hold a single official subscription the first-party CLI is less friction; if your model sources are split across a local runtime, several APIs, or an internal host, that flexibility is the reason to adopt it. Three configuration points carry the real value: pinning the model with -m provider/model so you do not re-pick it each session, freezing recurring workflows as subagents rather than retyping long instructions, and wiring MCP servers so the agent can reach facts that live outside the repository - files, databases, deploy targets. The sheet also covers session continuation and forking, because branching from the same context to try a second approach costs fewer tokens than restarting and makes the two attempts directly comparable.

AI CLI·33 commands·Last updated 2026-08-21

Launch & Run 5

opencode
Start the default interactive TUI
opencode "Refactor the auth module"
Start a session with an initial prompt
opencode -c
Continue the most recent session (--continue)
opencode -s abc123
Resume a session by ID (--session)
opencode --fork
Fork the current session into a new branch

CLI Flags 5

opencode -m anthropic/claude-sonnet "task"
Set provider/model (--model)
opencode --agent build "Implement login page"
Run with a named subagent (--agent)
opencode --prompt "Explain this function"
Single non-interactive prompt then exit
opencode --port 4096
Set the service port
opencode --hostname 0.0.0.0
Bind the listening address

Subcommands · agent / auth 5

opencode agent create <name>
Create a custom subagent
opencode agent list
List configured subagents
opencode auth login
Interactive provider sign-in
opencode auth list
List authenticated providers
opencode auth logout
Sign out

Subcommands · github / mcp 5

opencode github install
Install the GitHub integration
opencode github run
Run GitHub-related tasks
opencode mcp add <name> <url>
Add an MCP server
opencode mcp list
List configured MCP servers
opencode mcp auth
Authenticate an MCP server

Serve & Runtime 5

opencode run "task"
Run once and exit (good for automation)
opencode serve
Start the background service
opencode web
Open the Web UI
opencode attach
Attach to a running session
opencode upgrade
Upgrade to the latest version

Interactive Slash Commands 8

/help
List all available commands
/sessions
Open the session picker
/share /unshare
Share / unshare the current session
/compact /summarize
Compact / summarize to free the window
/models /themes
Switch model / theme
/init
Generate project config
/undo /redo
Undo / redo the last step
/exit /quit /q
Quit the TUI

Tips

  • OpenCode is an open-source Claude Code-style TUI; set the backend with -m provider/model (Anthropic/OpenAI/local) to avoid vendor lock-in.
  • For automation use opencode run "..." to execute once and exit — lighter than a resident TUI for CI codegen/review.
  • Encode recurring workflows as subagents (opencode agent create) and call them with --agent — more stable than repeating instructions in prompts.
  • Share a session with /share for a read-only link to send a colleague the agent's solution without exposing your local environment.

FAQ

What is the difference between opencode and opencode run?

Plain opencode starts the interactive TUI with you confirming each turn; opencode run "task" executes once and exits, which is what belongs in scripts and Makefiles. To keep a service running across a session, use opencode serve and then opencode attach to connect to the running session.

How do I choose which provider and model to use?

Pass -m with a provider/model prefix, e.g. opencode -m anthropic/claude-sonnet "task"; the prefix routes to that provider, following the same convention as LiteLLM. Inside a session, /models switches between configured models and /themes switches the theme. Changing model this way beats restarting the session.

What do I do when there are too many sessions and context gets muddled?

Use opencode -c to continue the most recent session, -s <id> to resume a specific one, and --fork to branch off the current session for different work. Inside a session, /sessions opens the selector, /compact frees window space and /summarize produces a summary. The principle: one task per session, and do not mix unrelated changes into one thread.

How do I run OpenCode as a background service?

opencode serve starts a persistent service; set --port 4096 and --hostname 0.0.0.0 to control the binding. Then attach with opencode attach. This decouples the service from the session: the process stays alive while the interface can disconnect and reconnect, so long tasks survive closing the terminal. opencode web opens the web UI instead.

How do I configure sub-agents?

Create one with opencode agent create <name> and list them with opencode agent list. At run time, select one with --agent build. Sub-agents suit splitting a large task into clearly scoped parts — one implementing, one reviewing — so each keeps a focused prompt instead of crowding a single context.

Official References

Each command links to its official documentation below, so you can verify the latest usage and read deeper.

Maintained by LaoHand

Publicly updated on Aug 21, 2026, continuously proofread against official docs.

Contact Us

Wrong command or description? Send us corrections, business inquiries or product feedback by email.

Contact Us