cli.mdx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. ---
  2. title: CLI
  3. description: OpenCode CLI options and commands.
  4. ---
  5. import { Tabs, TabItem } from "@astrojs/starlight/components"
  6. The OpenCode CLI by default starts the [TUI](/docs/tui) when run without any arguments.
  7. ```bash
  8. opencode
  9. ```
  10. But it also accepts commands as documented on this page. This allows you to interact with OpenCode programmatically.
  11. ```bash
  12. opencode run "Explain how closures work in JavaScript"
  13. ```
  14. ---
  15. ### tui
  16. Start the OpenCode terminal user interface.
  17. ```bash
  18. opencode [project]
  19. ```
  20. #### Flags
  21. | Flag | Short | Description |
  22. | ------------ | ----- | ----------------------------------------------------------------------- |
  23. | `--continue` | `-c` | Continue the last session |
  24. | `--session` | `-s` | Session ID to continue |
  25. | `--fork` | | Fork the session when continuing (use with `--continue` or `--session`) |
  26. | `--prompt` | | Prompt to use |
  27. | `--model` | `-m` | Model to use in the form of provider/model |
  28. | `--agent` | | Agent to use |
  29. | `--port` | | Port to listen on |
  30. | `--hostname` | | Hostname to listen on |
  31. ---
  32. ## Commands
  33. The OpenCode CLI also has the following commands.
  34. ---
  35. ### agent
  36. Manage agents for OpenCode.
  37. ```bash
  38. opencode agent [command]
  39. ```
  40. ---
  41. ### attach
  42. Attach a terminal to an already running OpenCode backend server started via `serve` or `web` commands.
  43. ```bash
  44. opencode attach [url]
  45. ```
  46. This allows using the TUI with a remote OpenCode backend. For example:
  47. ```bash
  48. # Start the backend server for web/mobile access
  49. opencode web --port 4096 --hostname 0.0.0.0
  50. # In another terminal, attach the TUI to the running backend
  51. opencode attach http://10.20.30.40:4096
  52. ```
  53. #### Flags
  54. | Flag | Short | Description |
  55. | ----------- | ----- | --------------------------------- |
  56. | `--dir` | | Working directory to start TUI in |
  57. | `--session` | `-s` | Session ID to continue |
  58. ---
  59. #### create
  60. Create a new agent with custom configuration.
  61. ```bash
  62. opencode agent create
  63. ```
  64. This command will guide you through creating a new agent with a custom system prompt and tool configuration.
  65. ---
  66. #### list
  67. List all available agents.
  68. ```bash
  69. opencode agent list
  70. ```
  71. ---
  72. ### auth
  73. Command to manage credentials and login for providers.
  74. ```bash
  75. opencode auth [command]
  76. ```
  77. ---
  78. #### login
  79. OpenCode is powered by the provider list at [Models.dev](https://models.dev), so you can use `opencode auth login` to configure API keys for any provider you'd like to use. This is stored in `~/.local/share/opencode/auth.json`.
  80. ```bash
  81. opencode auth login
  82. ```
  83. When OpenCode starts up it loads the providers from the credentials file. And if there are any keys defined in your environments or a `.env` file in your project.
  84. ---
  85. #### list
  86. Lists all the authenticated providers as stored in the credentials file.
  87. ```bash
  88. opencode auth list
  89. ```
  90. Or the short version.
  91. ```bash
  92. opencode auth ls
  93. ```
  94. ---
  95. #### logout
  96. Logs you out of a provider by clearing it from the credentials file.
  97. ```bash
  98. opencode auth logout
  99. ```
  100. ---
  101. ### github
  102. Manage the GitHub agent for repository automation.
  103. ```bash
  104. opencode github [command]
  105. ```
  106. ---
  107. #### install
  108. Install the GitHub agent in your repository.
  109. ```bash
  110. opencode github install
  111. ```
  112. This sets up the necessary GitHub Actions workflow and guides you through the configuration process. [Learn more](/docs/github).
  113. ---
  114. #### run
  115. Run the GitHub agent. This is typically used in GitHub Actions.
  116. ```bash
  117. opencode github run
  118. ```
  119. ##### Flags
  120. | Flag | Description |
  121. | --------- | -------------------------------------- |
  122. | `--event` | GitHub mock event to run the agent for |
  123. | `--token` | GitHub personal access token |
  124. ---
  125. ### mcp
  126. Manage Model Context Protocol servers.
  127. ```bash
  128. opencode mcp [command]
  129. ```
  130. ---
  131. #### add
  132. Add an MCP server to your configuration.
  133. ```bash
  134. opencode mcp add
  135. ```
  136. This command will guide you through adding either a local or remote MCP server.
  137. ---
  138. #### list
  139. List all configured MCP servers and their connection status.
  140. ```bash
  141. opencode mcp list
  142. ```
  143. Or use the short version.
  144. ```bash
  145. opencode mcp ls
  146. ```
  147. ---
  148. #### auth
  149. Authenticate with an OAuth-enabled MCP server.
  150. ```bash
  151. opencode mcp auth [name]
  152. ```
  153. If you don't provide a server name, you'll be prompted to select from available OAuth-capable servers.
  154. You can also list OAuth-capable servers and their authentication status.
  155. ```bash
  156. opencode mcp auth list
  157. ```
  158. Or use the short version.
  159. ```bash
  160. opencode mcp auth ls
  161. ```
  162. ---
  163. #### logout
  164. Remove OAuth credentials for an MCP server.
  165. ```bash
  166. opencode mcp logout [name]
  167. ```
  168. ---
  169. #### debug
  170. Debug OAuth connection issues for an MCP server.
  171. ```bash
  172. opencode mcp debug <name>
  173. ```
  174. ---
  175. ### models
  176. List all available models from configured providers.
  177. ```bash
  178. opencode models [provider]
  179. ```
  180. This command displays all models available across your configured providers in the format `provider/model`.
  181. This is useful for figuring out the exact model name to use in [your config](/docs/config/).
  182. You can optionally pass a provider ID to filter models by that provider.
  183. ```bash
  184. opencode models anthropic
  185. ```
  186. #### Flags
  187. | Flag | Description |
  188. | ----------- | ------------------------------------------------------------ |
  189. | `--refresh` | Refresh the models cache from models.dev |
  190. | `--verbose` | Use more verbose model output (includes metadata like costs) |
  191. Use the `--refresh` flag to update the cached model list. This is useful when new models have been added to a provider and you want to see them in OpenCode.
  192. ```bash
  193. opencode models --refresh
  194. ```
  195. ---
  196. ### run
  197. Run opencode in non-interactive mode by passing a prompt directly.
  198. ```bash
  199. opencode run [message..]
  200. ```
  201. This is useful for scripting, automation, or when you want a quick answer without launching the full TUI. For example.
  202. ```bash "opencode run"
  203. opencode run Explain the use of context in Go
  204. ```
  205. You can also attach to a running `opencode serve` instance to avoid MCP server cold boot times on every run:
  206. ```bash
  207. # Start a headless server in one terminal
  208. opencode serve
  209. # In another terminal, run commands that attach to it
  210. opencode run --attach http://localhost:4096 "Explain async/await in JavaScript"
  211. ```
  212. #### Flags
  213. | Flag | Short | Description |
  214. | ------------ | ----- | ----------------------------------------------------------------------- |
  215. | `--command` | | The command to run, use message for args |
  216. | `--continue` | `-c` | Continue the last session |
  217. | `--session` | `-s` | Session ID to continue |
  218. | `--fork` | | Fork the session when continuing (use with `--continue` or `--session`) |
  219. | `--share` | | Share the session |
  220. | `--model` | `-m` | Model to use in the form of provider/model |
  221. | `--agent` | | Agent to use |
  222. | `--file` | `-f` | File(s) to attach to message |
  223. | `--format` | | Format: default (formatted) or json (raw JSON events) |
  224. | `--title` | | Title for the session (uses truncated prompt if no value provided) |
  225. | `--attach` | | Attach to a running opencode server (e.g., http://localhost:4096) |
  226. | `--port` | | Port for the local server (defaults to random port) |
  227. ---
  228. ### serve
  229. Start a headless OpenCode server for API access. Check out the [server docs](/docs/server) for the full HTTP interface.
  230. ```bash
  231. opencode serve
  232. ```
  233. This starts an HTTP server that provides API access to opencode functionality without the TUI interface. Set `OPENCODE_SERVER_PASSWORD` to enable HTTP basic auth (username defaults to `opencode`).
  234. #### Flags
  235. | Flag | Description |
  236. | ------------ | ------------------------------------------ |
  237. | `--port` | Port to listen on |
  238. | `--hostname` | Hostname to listen on |
  239. | `--mdns` | Enable mDNS discovery |
  240. | `--cors` | Additional browser origin(s) to allow CORS |
  241. ---
  242. ### session
  243. Manage OpenCode sessions.
  244. ```bash
  245. opencode session [command]
  246. ```
  247. ---
  248. #### list
  249. List all OpenCode sessions.
  250. ```bash
  251. opencode session list
  252. ```
  253. ##### Flags
  254. | Flag | Short | Description |
  255. | ------------- | ----- | ------------------------------------ |
  256. | `--max-count` | `-n` | Limit to N most recent sessions |
  257. | `--format` | | Output format: table or json (table) |
  258. ---
  259. ### stats
  260. Show token usage and cost statistics for your OpenCode sessions.
  261. ```bash
  262. opencode stats
  263. ```
  264. #### Flags
  265. | Flag | Description |
  266. | ----------- | --------------------------------------------------------------------------- |
  267. | `--days` | Show stats for the last N days (all time) |
  268. | `--tools` | Number of tools to show (all) |
  269. | `--models` | Show model usage breakdown (hidden by default). Pass a number to show top N |
  270. | `--project` | Filter by project (all projects, empty string: current project) |
  271. ---
  272. ### export
  273. Export session data as JSON.
  274. ```bash
  275. opencode export [sessionID]
  276. ```
  277. If you don't provide a session ID, you'll be prompted to select from available sessions.
  278. ---
  279. ### import
  280. Import session data from a JSON file or OpenCode share URL.
  281. ```bash
  282. opencode import <file>
  283. ```
  284. You can import from a local file or an OpenCode share URL.
  285. ```bash
  286. opencode import session.json
  287. opencode import https://opncd.ai/s/abc123
  288. ```
  289. ---
  290. ### web
  291. Start a headless OpenCode server with a web interface.
  292. ```bash
  293. opencode web
  294. ```
  295. This starts an HTTP server and opens a web browser to access OpenCode through a web interface. Set `OPENCODE_SERVER_PASSWORD` to enable HTTP basic auth (username defaults to `opencode`).
  296. #### Flags
  297. | Flag | Description |
  298. | ------------ | ------------------------------------------ |
  299. | `--port` | Port to listen on |
  300. | `--hostname` | Hostname to listen on |
  301. | `--mdns` | Enable mDNS discovery |
  302. | `--cors` | Additional browser origin(s) to allow CORS |
  303. ---
  304. ### acp
  305. Start an ACP (Agent Client Protocol) server.
  306. ```bash
  307. opencode acp
  308. ```
  309. This command starts an ACP server that communicates via stdin/stdout using nd-JSON.
  310. #### Flags
  311. | Flag | Description |
  312. | ------------ | --------------------- |
  313. | `--cwd` | Working directory |
  314. | `--port` | Port to listen on |
  315. | `--hostname` | Hostname to listen on |
  316. ---
  317. ### uninstall
  318. Uninstall OpenCode and remove all related files.
  319. ```bash
  320. opencode uninstall
  321. ```
  322. #### Flags
  323. | Flag | Short | Description |
  324. | --------------- | ----- | ------------------------------------------- |
  325. | `--keep-config` | `-c` | Keep configuration files |
  326. | `--keep-data` | `-d` | Keep session data and snapshots |
  327. | `--dry-run` | | Show what would be removed without removing |
  328. | `--force` | `-f` | Skip confirmation prompts |
  329. ---
  330. ### upgrade
  331. Updates opencode to the latest version or a specific version.
  332. ```bash
  333. opencode upgrade [target]
  334. ```
  335. To upgrade to the latest version.
  336. ```bash
  337. opencode upgrade
  338. ```
  339. To upgrade to a specific version.
  340. ```bash
  341. opencode upgrade v0.1.48
  342. ```
  343. #### Flags
  344. | Flag | Short | Description |
  345. | ---------- | ----- | ----------------------------------------------------------------- |
  346. | `--method` | `-m` | The installation method that was used; curl, npm, pnpm, bun, brew |
  347. ---
  348. ## Global Flags
  349. The opencode CLI takes the following global flags.
  350. | Flag | Short | Description |
  351. | -------------- | ----- | ------------------------------------ |
  352. | `--help` | `-h` | Display help |
  353. | `--version` | `-v` | Print version number |
  354. | `--print-logs` | | Print logs to stderr |
  355. | `--log-level` | | Log level (DEBUG, INFO, WARN, ERROR) |
  356. ---
  357. ## Environment variables
  358. OpenCode can be configured using environment variables.
  359. | Variable | Type | Description |
  360. | ------------------------------------- | ------- | ------------------------------------------------- |
  361. | `OPENCODE_AUTO_SHARE` | boolean | Automatically share sessions |
  362. | `OPENCODE_GIT_BASH_PATH` | string | Path to Git Bash executable on Windows |
  363. | `OPENCODE_CONFIG` | string | Path to config file |
  364. | `OPENCODE_TUI_CONFIG` | string | Path to TUI config file |
  365. | `OPENCODE_CONFIG_DIR` | string | Path to config directory |
  366. | `OPENCODE_CONFIG_CONTENT` | string | Inline json config content |
  367. | `OPENCODE_DISABLE_AUTOUPDATE` | boolean | Disable automatic update checks |
  368. | `OPENCODE_DISABLE_PRUNE` | boolean | Disable pruning of old data |
  369. | `OPENCODE_DISABLE_TERMINAL_TITLE` | boolean | Disable automatic terminal title updates |
  370. | `OPENCODE_PERMISSION` | string | Inlined json permissions config |
  371. | `OPENCODE_DISABLE_DEFAULT_PLUGINS` | boolean | Disable default plugins |
  372. | `OPENCODE_DISABLE_LSP_DOWNLOAD` | boolean | Disable automatic LSP server downloads |
  373. | `OPENCODE_ENABLE_EXPERIMENTAL_MODELS` | boolean | Enable experimental models |
  374. | `OPENCODE_DISABLE_AUTOCOMPACT` | boolean | Disable automatic context compaction |
  375. | `OPENCODE_DISABLE_CLAUDE_CODE` | boolean | Disable reading from `.claude` (prompt + skills) |
  376. | `OPENCODE_DISABLE_CLAUDE_CODE_PROMPT` | boolean | Disable reading `~/.claude/CLAUDE.md` |
  377. | `OPENCODE_DISABLE_CLAUDE_CODE_SKILLS` | boolean | Disable loading `.claude/skills` |
  378. | `OPENCODE_DISABLE_MODELS_FETCH` | boolean | Disable fetching models from remote sources |
  379. | `OPENCODE_DISABLE_MOUSE` | boolean | Disable mouse capture in the TUI |
  380. | `OPENCODE_FAKE_VCS` | string | Fake VCS provider for testing purposes |
  381. | `OPENCODE_CLIENT` | string | Client identifier (defaults to `cli`) |
  382. | `OPENCODE_ENABLE_EXA` | boolean | Enable Exa web search tools |
  383. | `OPENCODE_SERVER_PASSWORD` | string | Enable basic auth for `serve`/`web` |
  384. | `OPENCODE_SERVER_USERNAME` | string | Override basic auth username (default `opencode`) |
  385. | `OPENCODE_MODELS_URL` | string | Custom URL for fetching models configuration |
  386. ---
  387. ### Experimental
  388. These environment variables enable experimental features that may change or be removed.
  389. | Variable | Type | Description |
  390. | ----------------------------------------------- | ------- | --------------------------------------- |
  391. | `OPENCODE_EXPERIMENTAL` | boolean | Enable all experimental features |
  392. | `OPENCODE_EXPERIMENTAL_ICON_DISCOVERY` | boolean | Enable icon discovery |
  393. | `OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT` | boolean | Disable copy on select in TUI |
  394. | `OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS` | number | Default timeout for bash commands in ms |
  395. | `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX` | number | Max output tokens for LLM responses |
  396. | `OPENCODE_EXPERIMENTAL_FILEWATCHER` | boolean | Enable file watcher for entire dir |
  397. | `OPENCODE_EXPERIMENTAL_OXFMT` | boolean | Enable oxfmt formatter |
  398. | `OPENCODE_EXPERIMENTAL_LSP_TOOL` | boolean | Enable experimental LSP tool |
  399. | `OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER` | boolean | Disable file watcher |
  400. | `OPENCODE_EXPERIMENTAL_EXA` | boolean | Enable experimental Exa features |
  401. | `OPENCODE_EXPERIMENTAL_LSP_TY` | boolean | Enable TY LSP for python files |
  402. | `OPENCODE_EXPERIMENTAL_MARKDOWN` | boolean | Enable experimental markdown features |
  403. | `OPENCODE_EXPERIMENTAL_PLAN_MODE` | boolean | Enable plan mode |