cli.mdx 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. ### auth
  48. Command to manage credentials and login for providers.
  49. ```bash
  50. opencode auth [command]
  51. ```
  52. ---
  53. #### login
  54. 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`.
  55. ```bash
  56. opencode auth login
  57. ```
  58. 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.
  59. ---
  60. #### list
  61. Lists all the authenticated providers as stored in the credentials file.
  62. ```bash
  63. opencode auth list
  64. ```
  65. Or the short version.
  66. ```bash
  67. opencode auth ls
  68. ```
  69. ---
  70. #### logout
  71. Logs you out of a provider by clearing it from the credentials file.
  72. ```bash
  73. opencode auth logout
  74. ```
  75. ---
  76. ### github
  77. Manage the GitHub agent for repository automation.
  78. ```bash
  79. opencode github [command]
  80. ```
  81. ---
  82. #### install
  83. Install the GitHub agent in your repository.
  84. ```bash
  85. opencode github install
  86. ```
  87. This sets up the necessary GitHub Actions workflow and guides you through the configuration process. [Learn more](/docs/github).
  88. ---
  89. #### run
  90. Run the GitHub agent. This is typically used in GitHub Actions.
  91. ```bash
  92. opencode github run
  93. ```
  94. ##### Flags
  95. | Flag | Description |
  96. | --------- | -------------------------------------- |
  97. | `--event` | GitHub mock event to run the agent for |
  98. | `--token` | GitHub personal access token |
  99. ---
  100. ### models
  101. List all available models from configured providers.
  102. ```bash
  103. opencode models [provider]
  104. ```
  105. This command displays all models available across your configured providers in the format `provider/model`.
  106. This is useful for figuring out the exact model name to use in [your config](/docs/config/).
  107. You can optionally pass a provider ID to filter models by that provider.
  108. ```bash
  109. opencode models anthropic
  110. ```
  111. #### Flags
  112. | Flag | Description |
  113. | ----------- | ------------------------------------------------------------ |
  114. | `--refresh` | Refresh the models cache from models.dev |
  115. | `--verbose` | Use more verbose model output (includes metadata like costs) |
  116. 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.
  117. ```bash
  118. opencode models --refresh
  119. ```
  120. ---
  121. ### run
  122. Run opencode in non-interactive mode by passing a prompt directly.
  123. ```bash
  124. opencode run [message..]
  125. ```
  126. This is useful for scripting, automation, or when you want a quick answer without launching the full TUI. For example.
  127. ```bash "opencode run"
  128. opencode run Explain the use of context in Go
  129. ```
  130. You can also attach to a running `opencode serve` instance to avoid MCP server cold boot times on every run:
  131. ```bash
  132. # Start a headless server in one terminal
  133. opencode serve
  134. # In another terminal, run commands that attach to it
  135. opencode run --attach http://localhost:4096 "Explain async/await in JavaScript"
  136. ```
  137. #### Flags
  138. | Flag | Short | Description |
  139. | ------------ | ----- | ------------------------------------------------------------------ |
  140. | `--command` | | The command to run, use message for args |
  141. | `--continue` | `-c` | Continue the last session |
  142. | `--session` | `-s` | Session ID to continue |
  143. | `--share` | | Share the session |
  144. | `--model` | `-m` | Model to use in the form of provider/model |
  145. | `--agent` | | Agent to use |
  146. | `--file` | `-f` | File(s) to attach to message |
  147. | `--format` | | Format: default (formatted) or json (raw JSON events) |
  148. | `--title` | | Title for the session (uses truncated prompt if no value provided) |
  149. | `--attach` | | Attach to a running opencode server (e.g., http://localhost:4096) |
  150. | `--port` | | Port for the local server (defaults to random port) |
  151. ---
  152. ### serve
  153. Start a headless opencode server for API access. Check out the [server docs](/docs/server) for the full HTTP interface.
  154. ```bash
  155. opencode serve
  156. ```
  157. This starts an HTTP server that provides API access to opencode functionality without the TUI interface.
  158. #### Flags
  159. | Flag | Short | Description |
  160. | ------------ | ----- | --------------------- |
  161. | `--port` | `-p` | Port to listen on |
  162. | `--hostname` | | Hostname to listen on |
  163. ---
  164. ### upgrade
  165. Updates opencode to the latest version or a specific version.
  166. ```bash
  167. opencode upgrade [target]
  168. ```
  169. To upgrade to the latest version.
  170. ```bash
  171. opencode upgrade
  172. ```
  173. To upgrade to a specific version.
  174. ```bash
  175. opencode upgrade v0.1.48
  176. ```
  177. #### Flags
  178. | Flag | Short | Description |
  179. | ---------- | ----- | ----------------------------------------------------------------- |
  180. | `--method` | `-m` | The installation method that was used; curl, npm, pnpm, bun, brew |
  181. ---
  182. ## Global Flags
  183. The opencode CLI takes the following global flags.
  184. | Flag | Short | Description |
  185. | -------------- | ----- | ------------------------------------ |
  186. | `--help` | `-h` | Display help |
  187. | `--version` | `-v` | Print version number |
  188. | `--print-logs` | | Print logs to stderr |
  189. | `--log-level` | | Log level (DEBUG, INFO, WARN, ERROR) |
  190. ---
  191. ## Environment variables
  192. OpenCode can be configured using environment variables.
  193. | Variable | Type | Description |
  194. | ------------------------------------- | ------- | -------------------------------------- |
  195. | `OPENCODE_AUTO_SHARE` | boolean | Automatically share sessions |
  196. | `OPENCODE_GIT_BASH_PATH` | string | Path to Git Bash executable on Windows |
  197. | `OPENCODE_CONFIG` | string | Path to config file |
  198. | `OPENCODE_CONFIG_DIR` | string | Path to config directory |
  199. | `OPENCODE_CONFIG_CONTENT` | string | Inline json config content |
  200. | `OPENCODE_DISABLE_AUTOUPDATE` | boolean | Disable automatic update checks |
  201. | `OPENCODE_DISABLE_PRUNE` | boolean | Disable pruning of old data |
  202. | `OPENCODE_PERMISSION` | string | Inlined json permissions config |
  203. | `OPENCODE_DISABLE_DEFAULT_PLUGINS` | boolean | Disable default plugins |
  204. | `OPENCODE_DISABLE_LSP_DOWNLOAD` | boolean | Disable automatic LSP server downloads |
  205. | `OPENCODE_ENABLE_EXPERIMENTAL_MODELS` | boolean | Enable experimental models |
  206. | `OPENCODE_DISABLE_AUTOCOMPACT` | boolean | Disable automatic context compaction |
  207. | `OPENCODE_CLIENT` | string | Client identifier (defaults to `cli`) |
  208. | `OPENCODE_ENABLE_EXA` | boolean | Enable Exa web search tools |
  209. ---
  210. ### Experimental
  211. These environment variables enable experimental features that may change or be removed.
  212. | Variable | Type | Description |
  213. | ----------------------------------------------- | ------- | --------------------------------------- |
  214. | `OPENCODE_EXPERIMENTAL` | boolean | Enable all experimental features |
  215. | `OPENCODE_EXPERIMENTAL_ICON_DISCOVERY` | boolean | Enable icon discovery |
  216. | `OPENCODE_EXPERIMENTAL_DISABLE_COPY_ON_SELECT` | boolean | Disable copy on select in TUI |
  217. | `OPENCODE_EXPERIMENTAL_BASH_MAX_OUTPUT_LENGTH` | number | Max output length for bash commands |
  218. | `OPENCODE_EXPERIMENTAL_BASH_DEFAULT_TIMEOUT_MS` | number | Default timeout for bash commands in ms |
  219. | `OPENCODE_EXPERIMENTAL_OUTPUT_TOKEN_MAX` | number | Max output tokens for LLM responses |
  220. | `OPENCODE_EXPERIMENTAL_FILEWATCHER` | boolean | Enable file watcher for entire dir |
  221. | `OPENCODE_EXPERIMENTAL_OXFMT` | boolean | Enable oxfmt formatter |