cli.mdx 14 KB

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