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