agents.mdx 9.5 KB

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