agents.mdx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. ---
  2. title: "Agents"
  3. description: ""
  4. ---
  5. Agents combine a system prompt, model preference, tool permissions, and display
  6. metadata into a reusable assistant profile. OpenCode includes agents for common
  7. workflows, and you can override them or add your own in configuration or
  8. Markdown files.
  9. ## Built-in agents
  10. | Agent | Mode | Purpose |
  11. | --- | --- | --- |
  12. | **Build** (`build`) | `primary` | Default coding agent. Tools are allowed by default, sensitive environment-file reads ask for approval, and access outside the workspace asks for approval. |
  13. | **Plan** (`plan`) | `primary` | Planning agent. File edits are denied except for OpenCode plan files. Shell commands are not generally denied. |
  14. | **General** (`general`) | `subagent` | General-purpose research and multi-step work. It has broad tool access but cannot launch more subagents. |
  15. | **Explore** (`explore`) | `subagent` | Read-only code and web exploration using `read`, `glob`, `grep`, `webfetch`, and `websearch`. |
  16. OpenCode also has hidden `compaction`, `title`, and `summary` system agents.
  17. They run internal maintenance tasks and are not selectable. There is no built-in
  18. `scout` agent in V2.
  19. You can override a built-in agent with an entry of the same ID. Set
  20. `disabled: true` to remove one.
  21. ## Default agent
  22. Set the primary agent used when a session has not selected one:
  23. ```jsonc title="opencode.jsonc"
  24. {
  25. "$schema": "https://opencode.ai/config.json",
  26. "default_agent": "reviewer"
  27. }
  28. ```
  29. The configured agent must exist, must not have `mode: "subagent"`, and must not
  30. be hidden. If it is unavailable, OpenCode falls back to `build`, then to the
  31. first visible agent that can run as a primary agent. This selection does not
  32. rewrite the agent already stored on an existing session.
  33. ## Modes
  34. An agent's `mode` controls where it can run:
  35. | Mode | Behavior |
  36. | --- | --- |
  37. | `primary` | Can be selected as the main agent for a session. It cannot be launched as a subagent. |
  38. | `subagent` | Can run in a child session through the `subagent` tool, but cannot be selected as the main agent. |
  39. | `all` | Can be used either way. This is the default for a custom agent when `mode` is omitted. |
  40. In the TUI, press <kbd>Tab</kbd> and <kbd>Shift</kbd>+<kbd>Tab</kbd> to cycle
  41. through visible primary and `all` agents, or use `/agents` to choose one.
  42. Subagents run in child sessions with fresh context. A primary agent can invoke
  43. one with the `subagent` tool, either in the foreground or in the background.
  44. You can also `@` mention a visible subagent to ask the current agent to delegate
  45. work to it:
  46. ```text
  47. @explore find where authentication errors are handled
  48. ```
  49. The parent agent's `subagent` permission controls which agents it may launch.
  50. The child currently uses its own configured permissions, not a restricted copy
  51. of the parent's permissions.
  52. ## Configure agents
  53. ### Markdown files
  54. The recommended file locations are:
  55. ```text
  56. ~/.config/opencode/agents/<name>.md
  57. .opencode/agents/<name>.md
  58. ```
  59. OpenCode discovers project `.opencode` directories from the current directory
  60. up to the project root. The path below `agents/` becomes the agent ID, so
  61. `.opencode/agents/team/reviewer.md` defines `team/reviewer`.
  62. Frontmatter uses the same fields as an entry under `agents`. The Markdown body
  63. becomes `system`:
  64. ```md title=".opencode/agents/reviewer.md"
  65. ---
  66. description: Reviews changes without modifying files
  67. mode: subagent
  68. model: anthropic/claude-sonnet-4-5#high
  69. color: warning
  70. steps: 8
  71. permissions:
  72. - action: edit
  73. resource: "*"
  74. effect: deny
  75. - action: shell
  76. resource: "*"
  77. effect: deny
  78. ---
  79. Review for correctness, security, regressions, and missing tests.
  80. List findings in severity order with file and line references.
  81. ```
  82. ### JSON or JSONC
  83. Use the `agents` field in any [OpenCode configuration file](/config):
  84. ```jsonc title="opencode.jsonc"
  85. {
  86. "$schema": "https://opencode.ai/config.json",
  87. "default_agent": "reviewer",
  88. "agents": {
  89. "reviewer": {
  90. "description": "Reviews changes for correctness, security, and missing tests",
  91. "mode": "all",
  92. "model": "anthropic/claude-sonnet-4-5#high",
  93. "system": "Review the current changes. Report findings before any summary.",
  94. "color": "warning",
  95. "steps": 8,
  96. "permissions": [
  97. { "action": "edit", "resource": "*", "effect": "deny" },
  98. { "action": "shell", "resource": "*", "effect": "deny" }
  99. ]
  100. },
  101. "build": {
  102. "permissions": [
  103. { "action": "shell", "resource": "git push *", "effect": "ask" }
  104. ]
  105. }
  106. }
  107. }
  108. ```
  109. Agent definitions merge in configuration order. Later scalar fields replace
  110. earlier values, request maps merge by key, and permission rules are appended.
  111. Global `permissions` are applied to every agent before its agent-specific rules,
  112. so a later agent rule can refine a global rule.
  113. ## Options
  114. ### `description`
  115. Explains the agent's purpose. It is optional, but strongly recommended for
  116. subagents because OpenCode includes it in the subagent catalog shown to the
  117. model.
  118. ### `mode`
  119. Accepts `primary`, `subagent`, or `all`. The default is `all`.
  120. ### `model`
  121. Selects a model using `provider/model` with an optional `#variant`:
  122. ```jsonc
  123. {
  124. "agents": {
  125. "reviewer": {
  126. "model": "anthropic/claude-sonnet-4-5#high"
  127. }
  128. }
  129. }
  130. ```
  131. The equivalent expanded form is:
  132. ```jsonc
  133. {
  134. "agents": {
  135. "reviewer": {
  136. "model": {
  137. "providerID": "anthropic",
  138. "model": "claude-sonnet-4-5",
  139. "variant": "high"
  140. }
  141. }
  142. }
  143. }
  144. ```
  145. The TUI uses this as the preferred model when the agent is selected. A child
  146. session uses its subagent's configured model, or inherits the parent session's
  147. model when none is configured. In the API, the session's selected model is
  148. stored separately; creating or switching a primary session with only an agent
  149. ID does not itself change that session model.
  150. ### `system`
  151. Sets the agent's system prompt. A non-empty value replaces OpenCode's
  152. provider-specific base prompt for that agent. Project instructions, skills,
  153. references, and other instruction sources are still added separately.
  154. For a Markdown agent, use the document body instead of a `system` frontmatter
  155. field.
  156. ### `permissions`
  157. Permissions are an ordered array of rules:
  158. ```jsonc
  159. {
  160. "agents": {
  161. "orchestrator": {
  162. "permissions": [
  163. { "action": "subagent", "resource": "*", "effect": "deny" },
  164. { "action": "subagent", "resource": "explore", "effect": "allow" },
  165. { "action": "shell", "resource": "git *", "effect": "ask" }
  166. ]
  167. }
  168. }
  169. }
  170. ```
  171. Each rule has:
  172. | Field | Meaning |
  173. | --- | --- |
  174. | `action` | Tool or permission action, with `*` wildcards supported. |
  175. | `resource` | The path, command, agent ID, or other resource matched by the action. Wildcards are supported. |
  176. | `effect` | `allow`, `ask`, or `deny`. |
  177. The last matching rule wins. Important V2 action names include `shell` for
  178. shell commands, `edit` for all edit/write/patch tools, and `subagent` for child
  179. agents. Other tools generally use their tool name, such as `read`, `glob`,
  180. `grep`, `webfetch`, `websearch`, and `skill`.
  181. <Tip>
  182. Put broad wildcard rules first and exceptions afterward. For example, deny
  183. all subagents first, then allow `explore`.
  184. </Tip>
  185. `~` and `$HOME` are expanded in filesystem resources for `read`, `edit`, and
  186. `external_directory`. Shell resources are raw command text and are not
  187. expanded.
  188. ### `steps`
  189. Sets a positive maximum number of model steps. On the final allowed step,
  190. OpenCode removes tools and asks the model to summarize its work in text. New
  191. user input resets the allowance.
  192. ### `hidden`
  193. When `true`, removes the agent from normal selectors, `@` autocomplete, and the
  194. subagent catalog advertised to models. It is a visibility setting, not a
  195. security boundary.
  196. ### `color`
  197. Sets the agent's UI color. Use a six-digit hex color such as `#ff6b6b`, or one
  198. of `primary`, `secondary`, `accent`, `success`, `warning`, `error`, or `info`.
  199. ### `disabled`
  200. When `true`, removes the agent definition at that point in configuration
  201. loading. This works for built-in and custom agents.
  202. ### `request`
  203. The V2 schema accepts per-agent request `headers` and JSON `body` overlays:
  204. ```jsonc
  205. {
  206. "agents": {
  207. "reviewer": {
  208. "request": {
  209. "headers": { "x-agent": "reviewer" },
  210. "body": { "temperature": 0.1 }
  211. }
  212. }
  213. }
  214. }
  215. ```
  216. <Warning>
  217. The current V2 session runner preserves these overlays on the agent
  218. definition but does not yet apply them to model requests. Configure effective
  219. request settings on the provider, model, or model variant instead. Do not use
  220. legacy top-level agent fields such as `temperature`, `top_p`, `prompt`,
  221. `permission`, `tools`, `disable`, or `maxSteps` in new V2 configuration.
  222. </Warning>