config.mdx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. ---
  2. title: Config
  3. description: Using the opencode JSON config.
  4. ---
  5. You can configure opencode using a JSON config file.
  6. ---
  7. ## Format
  8. opencode supports both **JSON** and **JSONC** (JSON with Comments) formats.
  9. ```jsonc title="opencode.jsonc"
  10. {
  11. "$schema": "https://opencode.ai/config.json",
  12. // Theme configuration
  13. "theme": "opencode",
  14. "model": "anthropic/claude-sonnet-4-20250514",
  15. "autoupdate": true,
  16. }
  17. ```
  18. With JSONC, you can use comments in your configuration files:
  19. ---
  20. ## Locations
  21. You can place your config in a couple of different locations and they have a
  22. different order of precedence.
  23. ---
  24. ### Global
  25. Place your global opencode config in `~/.config/opencode/opencode.json`. You'll want to use the global config for things like themes, providers, or keybinds.
  26. ---
  27. ### Per project
  28. You can also add a `opencode.json` in your project. It takes precedence over the global config. This is useful for configuring providers or modes specific to your project.
  29. :::tip
  30. Place project specific config in the root of your project.
  31. :::
  32. When opencode starts up, it looks for a config file in the current directory or traverse up to the nearest Git directory.
  33. This is also safe to be checked into Git and uses the same schema as the global one.
  34. ---
  35. ### Custom path
  36. You can also specify a custom config file path using the `OPENCODE_CONFIG` environment variable. This takes precedence over the global and project configs.
  37. ```bash
  38. export OPENCODE_CONFIG=/path/to/my/custom-config.json
  39. opencode run "Hello world"
  40. ```
  41. ---
  42. ## Schema
  43. The config file has a schema that's defined in [**`opencode.ai/config.json`**](https://opencode.ai/config.json).
  44. Your editor should be able to validate and autocomplete based on the schema.
  45. ---
  46. ### Models
  47. You can configure the providers and models you want to use in your opencode config through the `provider`, `model` and `small_model` options.
  48. ```json title="opencode.json"
  49. {
  50. "$schema": "https://opencode.ai/config.json",
  51. "provider": {},
  52. "model": "anthropic/claude-sonnet-4-20250514",
  53. "small_model": "anthropic/claude-3-5-haiku-20241022"
  54. }
  55. ```
  56. The `small_model` option configures a separate model for lightweight tasks like title generation. By default, opencode tries to use a cheaper model if one is available from your provider, otherwise it falls back to your main model.
  57. You can also configure [local models](/docs/models#local). [Learn more](/docs/models).
  58. ---
  59. ### Themes
  60. You can configure the theme you want to use in your opencode config through the `theme` option.
  61. ```json title="opencode.json"
  62. {
  63. "$schema": "https://opencode.ai/config.json",
  64. "theme": ""
  65. }
  66. ```
  67. [Learn more here](/docs/themes).
  68. ---
  69. ### Agents
  70. You can configure specialized agents for specific tasks through the `agent` option.
  71. ```jsonc title="opencode.jsonc"
  72. {
  73. "$schema": "https://opencode.ai/config.json",
  74. "agent": {
  75. "code-reviewer": {
  76. "description": "Reviews code for best practices and potential issues",
  77. "model": "anthropic/claude-sonnet-4-20250514",
  78. "prompt": "You are a code reviewer. Focus on security, performance, and maintainability.",
  79. "tools": {
  80. // Disable file modification tools for review-only agent
  81. "write": false,
  82. "edit": false,
  83. },
  84. },
  85. },
  86. }
  87. ```
  88. You can also define agents using markdown files in `~/.config/opencode/agent/` or `.opencode/agent/`. [Learn more here](/docs/agents).
  89. ---
  90. ### Sharing
  91. You can configure the [share](/docs/share) feature through the `share` option.
  92. ```json title="opencode.json"
  93. {
  94. "$schema": "https://opencode.ai/config.json",
  95. "share": "manual"
  96. }
  97. ```
  98. This takes:
  99. - `"manual"` - Allow manual sharing via commands (default)
  100. - `"auto"` - Automatically share new conversations
  101. - `"disabled"` - Disable sharing entirely
  102. By default, sharing is set to manual mode where you need to explicitly share conversations using the `/share` command.
  103. ---
  104. ### Commands
  105. You can configure custom commands for repetitive tasks through the `command` option.
  106. ```jsonc title="opencode.jsonc"
  107. {
  108. "$schema": "https://opencode.ai/config.json",
  109. "command": {
  110. "test": {
  111. "template": "Run the full test suite with coverage report and show any failures.\nFocus on the failing tests and suggest fixes.",
  112. "description": "Run tests with coverage",
  113. "agent": "build",
  114. "model": "anthropic/claude-3-5-sonnet-20241022"
  115. },
  116. "component": {
  117. "template": "Create a new React component named $ARGUMENTS with TypeScript support.\nInclude proper typing and basic structure.",
  118. "description": "Create a new component"
  119. }
  120. }
  121. }
  122. ```
  123. You can also define commands using markdown files in `~/.config/opencode/command/` or `.opencode/command/`. [Learn more here](/docs/commands).
  124. ---
  125. ### Keybinds
  126. You can customize your keybinds through the `keybinds` option.
  127. ```json title="opencode.json"
  128. {
  129. "$schema": "https://opencode.ai/config.json",
  130. "keybinds": {}
  131. }
  132. ```
  133. [Learn more here](/docs/keybinds).
  134. ---
  135. ### Autoupdate
  136. opencode will automatically download any new updates when it starts up. You can disable this with the `autoupdate` option.
  137. ```json title="opencode.json"
  138. {
  139. "$schema": "https://opencode.ai/config.json",
  140. "autoupdate": false
  141. }
  142. ```
  143. ---
  144. ### Formatters
  145. You can configure code formatters through the `formatter` option.
  146. ```json title="opencode.json"
  147. {
  148. "$schema": "https://opencode.ai/config.json",
  149. "formatter": {
  150. "prettier": {
  151. "disabled": true
  152. },
  153. "custom-prettier": {
  154. "command": ["npx", "prettier", "--write", "$FILE"],
  155. "environment": {
  156. "NODE_ENV": "development"
  157. },
  158. "extensions": [".js", ".ts", ".jsx", ".tsx"]
  159. }
  160. }
  161. }
  162. ```
  163. [Learn more about formatters here](/docs/formatters).
  164. ---
  165. ### Permissions
  166. You can configure permissions to control what AI agents can do in your codebase through the `permission` option.
  167. ```json title="opencode.json"
  168. {
  169. "$schema": "https://opencode.ai/config.json",
  170. "permission": {
  171. "edit": "ask",
  172. "bash": "ask"
  173. }
  174. }
  175. ```
  176. This allows you to configure explicit approval requirements for sensitive operations:
  177. - `edit` - Controls whether file editing operations require user approval (`"ask"` or `"allow"`)
  178. - `bash` - Controls whether bash commands require user approval (can be `"ask"`/`"allow"` or a pattern map)
  179. [Learn more about permissions here](/docs/permissions).
  180. ---
  181. ### MCP servers
  182. You can configure MCP servers you want to use through the `mcp` option.
  183. ```json title="opencode.json"
  184. {
  185. "$schema": "https://opencode.ai/config.json",
  186. "mcp": {}
  187. }
  188. ```
  189. [Learn more here](/docs/mcp-servers).
  190. ---
  191. ### Instructions
  192. You can configure the instructions for the model you're using through the `instructions` option.
  193. ```json title="opencode.json"
  194. {
  195. "$schema": "https://opencode.ai/config.json",
  196. "instructions": ["CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md"]
  197. }
  198. ```
  199. This takes an array of paths and glob patterns to instruction files. [Learn more
  200. about rules here](/docs/rules).
  201. ---
  202. ### Disabled providers
  203. You can disable providers that are loaded automatically through the `disabled_providers` option. This is useful when you want to prevent certain providers from being loaded even if their credentials are available.
  204. ```json title="opencode.json"
  205. {
  206. "$schema": "https://opencode.ai/config.json",
  207. "disabled_providers": ["openai", "gemini"]
  208. }
  209. ```
  210. The `disabled_providers` option accepts an array of provider IDs. When a provider is disabled:
  211. - It won't be loaded even if environment variables are set.
  212. - It won't be loaded even if API keys are configured through `opencode auth login`.
  213. - The provider's models won't appear in the model selection list.
  214. ---
  215. ## Variables
  216. You can use variable substitution in your config files to reference environment variables and file contents.
  217. ---
  218. ### Env vars
  219. Use `{env:VARIABLE_NAME}` to substitute environment variables:
  220. ```json title="opencode.json"
  221. {
  222. "$schema": "https://opencode.ai/config.json",
  223. "model": "{env:OPENCODE_MODEL}",
  224. "provider": {
  225. "anthropic": {
  226. "models": {},
  227. "options": {
  228. "apiKey": "{env:ANTHROPIC_API_KEY}"
  229. }
  230. }
  231. }
  232. }
  233. ```
  234. If the environment variable is not set, it will be replaced with an empty string.
  235. ---
  236. ### Files
  237. Use `{file:path/to/file}` to substitute the contents of a file:
  238. ```json title="opencode.json"
  239. {
  240. "$schema": "https://opencode.ai/config.json",
  241. "instructions": ["./custom-instructions.md"],
  242. "provider": {
  243. "openai": {
  244. "options": {
  245. "apiKey": "{file:~/.secrets/openai-key}"
  246. }
  247. }
  248. }
  249. }
  250. ```
  251. File paths can be:
  252. - Relative to the config file directory
  253. - Or absolute paths starting with `/` or `~`
  254. These are useful for:
  255. - Keeping sensitive data like API keys in separate files.
  256. - Including large instruction files without cluttering your config.
  257. - Sharing common configuration snippets across multiple config files.