cli.mdx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. ---
  239. ### session
  240. Manage OpenCode sessions.
  241. ```bash
  242. opencode session [command]
  243. ```
  244. ---
  245. #### list
  246. List all OpenCode sessions.
  247. ```bash
  248. opencode session list
  249. ```
  250. ##### Flags
  251. | Flag | Short | Description |
  252. | ------------- | ----- | ------------------------------------ |
  253. | `--max-count` | `-n` | Limit to N most recent sessions |
  254. | `--format` | | Output format: table or json (table) |
  255. ---
  256. ### stats
  257. Show token usage and cost statistics for your OpenCode sessions.
  258. ```bash
  259. opencode stats
  260. ```
  261. #### Flags
  262. | Flag | Description |
  263. | ----------- | --------------------------------------------------------------- |
  264. | `--days` | Show stats for the last N days (all time) |
  265. | `--tools` | Number of tools to show (all) |
  266. | `--project` | Filter by project (all projects, empty string: current project) |
  267. ---
  268. ### export
  269. Export session data as JSON.
  270. ```bash
  271. opencode export [sessionID]
  272. ```
  273. If you don't provide a session ID, you'll be prompted to select from available sessions.
  274. ---
  275. ### import
  276. Import session data from a JSON file or OpenCode share URL.
  277. ```bash
  278. opencode import <file>
  279. ```
  280. You can import from a local file or an OpenCode share URL.
  281. ```bash
  282. opencode import session.json
  283. opencode import https://opncd.ai/s/abc123
  284. ```
  285. ---
  286. ### web
  287. Start a headless OpenCode server with a web interface.
  288. ```bash
  289. opencode web
  290. ```
  291. This starts an HTTP server and opens a web browser to access OpenCode through a web interface.
  292. #### Flags
  293. | Flag | Description |
  294. | ------------ | --------------------- |
  295. | `--port` | Port to listen on |
  296. | `--hostname` | Hostname to listen on |
  297. | `--mdns` | Enable mDNS discovery |
  298. ---
  299. ### acp
  300. Start an ACP (Agent Client Protocol) server.
  301. ```bash
  302. opencode acp
  303. ```
  304. This command starts an ACP server that communicates via stdin/stdout using nd-JSON.
  305. #### Flags
  306. | Flag | Description |
  307. | ------------ | --------------------- |
  308. | `--cwd` | Working directory |
  309. | `--port` | Port to listen on |
  310. | `--hostname` | Hostname to listen on |
  311. ---
  312. ### uninstall
  313. Uninstall OpenCode and remove all related files.
  314. ```bash
  315. opencode uninstall
  316. ```
  317. #### Flags
  318. | Flag | Short | Description |
  319. | --------------- | ----- | ------------------------------------------- |
  320. | `--keep-config` | `-c` | Keep configuration files |
  321. | `--keep-data` | `-d` | Keep session data and snapshots |
  322. | `--dry-run` | | Show what would be removed without removing |
  323. | `--force` | `-f` | Skip confirmation prompts |
  324. ---
  325. ### upgrade
  326. Updates opencode to the latest version or a specific version.
  327. ```bash
  328. opencode upgrade [target]
  329. ```
  330. To upgrade to the latest version.
  331. ```bash
  332. opencode upgrade
  333. ```
  334. To upgrade to a specific version.
  335. ```bash
  336. opencode upgrade v0.1.48
  337. ```
  338. #### Flags
  339. | Flag | Short | Description |
  340. | ---------- | ----- | ----------------------------------------------------------------- |
  341. | `--method` | `-m` | The installation method that was used; curl, npm, pnpm, bun, brew |
  342. ---
  343. ## Global Flags
  344. The opencode CLI takes the following global flags.
  345. | Flag | Short | Description |
  346. | -------------- | ----- | ------------------------------------ |
  347. | `--help` | `-h` | Display help |
  348. | `--version` | `-v` | Print version number |
  349. | `--print-logs` | | Print logs to stderr |
  350. | `--log-level` | | Log level (DEBUG, INFO, WARN, ERROR) |
  351. ---
  352. ## Environment variables
  353. OpenCode can be configured using environment variables.
  354. | Variable | Type | Description |
  355. | ------------------------------------- | ------- | ---------------------------------------- |
  356. | `OPENCODE_AUTO_SHARE` | boolean | Automatically share sessions |
  357. | `OPENCODE_GIT_BASH_PATH` | string | Path to Git Bash executable on Windows |
  358. | `OPENCODE_CONFIG` | string | Path to config file |
  359. | `OPENCODE_CONFIG_DIR` | string | Path to config directory |
  360. | `OPENCODE_CONFIG_CONTENT` | string | Inline json config content |
  361. | `OPENCODE_DISABLE_AUTOUPDATE` | boolean | Disable automatic update checks |
  362. | `OPENCODE_DISABLE_PRUNE` | boolean | Disable pruning of old data |
  363. | `OPENCODE_DISABLE_TERMINAL_TITLE` | boolean | Disable automatic terminal title updates |
  364. | `OPENCODE_PERMISSION` | string | Inlined json permissions config |
  365. | `OPENCODE_DISABLE_DEFAULT_PLUGINS` | boolean | Disable default plugins |
  366. | `OPENCODE_DISABLE_LSP_DOWNLOAD` | boolean | Disable automatic LSP server downloads |
  367. | `OPENCODE_ENABLE_EXPERIMENTAL_MODELS` | boolean | Enable experimental models |
  368. | `OPENCODE_DISABLE_AUTOCOMPACT` | boolean | Disable automatic context compaction |
  369. | `OPENCODE_CLIENT` | string | Client identifier (defaults to `cli`) |
  370. | `OPENCODE_ENABLE_EXA` | boolean | Enable Exa web search tools |
  371. ---
  372. ### Experimental
  373. These environment variables enable experimental features that may change or be removed.
  374. | Variable | Type | Description |
  375. | ----------------------------------------------- | ------- | --------------------------------------- |
  376. | `OPENCODE_EXPERIMENTAL` | boolean | Enable all experimental features |
  377. | `OPENCODE_EXPERIMENTAL_ICON_DISCOVERY` | boolean | Enable icon discovery |
  378. | `OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT` | boolean | Disable copy on select in TUI |
  379. | `OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH` | number | Max output length for bash commands |
  380. | `OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS` | number | Default timeout for bash commands in ms |
  381. | `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX` | number | Max output tokens for LLM responses |
  382. | `OPENCODE_EXPERIMENTAL_FILEWATCHER` | boolean | Enable file watcher for entire dir |
  383. | `OPENCODE_EXPERIMENTAL_OXFMT` | boolean | Enable oxfmt formatter |
  384. | `OPENCODE_EXPERIMENTAL_LSP_TOOL` | boolean | Enable experimental LSP tool |