cli.mdx 15 KB

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