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.
Launch & Run 5
opencodeopencode "Refactor the auth module"opencode -copencode -s abc123opencode --forkCLI Flags 5
opencode -m anthropic/claude-sonnet "task"opencode --agent build "Implement login page"opencode --prompt "Explain this function"opencode --port 4096opencode --hostname 0.0.0.0Subcommands · agent / auth 5
opencode agent create <name>opencode agent listopencode auth loginopencode auth listopencode auth logoutSubcommands · github / mcp 5
opencode github installopencode github runopencode mcp add <name> <url>opencode mcp listopencode mcp authServe & Runtime 5
opencode run "task"opencode serveopencode webopencode attachopencode upgradeInteractive Slash Commands 8
/help/sessions/share /unshare/compact /summarize/models /themes/init/undo /redo/exit /quit /qTips
- 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
/sharefor 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