agents.mdx 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. ---
  2. title: Agents
  3. description: Configure and use specialized agents.
  4. ---
  5. Agents are specialized AI assistants that can be configured for specific tasks and workflows. They allow you to create focused tools with custom prompts, models, and tool access.
  6. :::tip
  7. Use the plan agent to analyze code and review suggestions without making any code changes.
  8. :::
  9. You can switch between agents during a session or invoke them with the `@` mention.
  10. ---
  11. ## Types
  12. There are two types of agents in OpenCode; primary agents and subagents.
  13. ---
  14. ### Primary agents
  15. Primary agents are the main assistants you interact with directly. You can cycle through them using the **Tab** key, or your configured `switch_agent` keybind. These agents handle your main conversation. Tool access is configured via permissions — for example, Build has all tools enabled while Plan is restricted.
  16. :::tip
  17. You can use the **Tab** key to switch between primary agents during a session.
  18. :::
  19. OpenCode comes with two built-in primary agents, **Build** and **Plan**. We'll
  20. look at these below.
  21. ---
  22. ### Subagents
  23. Subagents are specialized assistants that primary agents can invoke for specific tasks. You can also manually invoke them by **@ mentioning** them in your messages.
  24. OpenCode comes with three built-in subagents, **General**, **Explore**, and **Scout**. We'll look at this below.
  25. ---
  26. ## Built-in
  27. OpenCode comes with two built-in primary agents and three built-in subagents.
  28. ---
  29. ### Use build
  30. _Mode_: `primary`
  31. Build is the **default** primary agent with all tools enabled. This is the standard agent for development work where you need full access to file operations and system commands.
  32. ---
  33. ### Use plan
  34. _Mode_: `primary`
  35. A restricted agent designed for planning and analysis. We use a permission system to give you more control and prevent unintended changes.
  36. By default, all of the following are set to `ask`:
  37. - `file edits`: All writes, patches, and edits
  38. - `bash`: All bash commands
  39. This agent is useful when you want the LLM to analyze code, suggest changes, or create plans without making any actual modifications to your codebase.
  40. ---
  41. ### Use general
  42. _Mode_: `subagent`
  43. A general-purpose agent for researching complex questions and executing multi-step tasks. Has full tool access (except todo), so it can make file changes when needed. Use this to run multiple units of work in parallel.
  44. ---
  45. ### Use explore
  46. _Mode_: `subagent`
  47. A fast, read-only agent for exploring codebases. Cannot modify files. Use this when you need to quickly find files by patterns, search code for keywords, or answer questions about the codebase.
  48. ---
  49. ### Use scout
  50. _Mode_: `subagent`
  51. A read-only agent for external docs and dependency research. Use this when you need to clone a dependency repository into OpenCode's managed cache, inspect library source, or cross-reference local code against upstream implementations without modifying your workspace.
  52. ---
  53. ### Use compaction
  54. _Mode_: `primary`
  55. Hidden system agent that compacts long context into a smaller summary. It runs automatically when needed and is not selectable in the UI.
  56. ---
  57. ### Use title
  58. _Mode_: `primary`
  59. Hidden system agent that generates short session titles. It runs automatically and is not selectable in the UI.
  60. ---
  61. ### Use summary
  62. _Mode_: `primary`
  63. Hidden system agent that creates session summaries. It runs automatically and is not selectable in the UI.
  64. ---
  65. ## Usage
  66. 1. For primary agents, use the **Tab** key to cycle through them during a session. You can also use your configured `switch_agent` keybind.
  67. 2. Subagents can be invoked:
  68. - **Automatically** by primary agents for specialized tasks based on their descriptions.
  69. - Manually by **@ mentioning** a subagent in your message. For example.
  70. ```txt frame="none"
  71. @general help me search for this function
  72. ```
  73. 3. **Navigation between sessions**: When subagents create child sessions, use `session_child_first` (default: **\<Leader>+Down**) to enter the first child session from the parent.
  74. 4. Once you are in a child session, use:
  75. - `session_child_cycle` (default: **Right**) to cycle to the next child session
  76. - `session_child_cycle_reverse` (default: **Left**) to cycle to the previous child session
  77. - `session_parent` (default: **Up**) to return to the parent session
  78. This lets you switch between the main conversation and specialized subagent work.
  79. ---
  80. ## Configure
  81. You can customize the built-in agents or create your own through configuration. Agents can be configured in two ways:
  82. ---
  83. ### JSON
  84. Configure agents in your `opencode.json` config file:
  85. ```json title="opencode.json"
  86. {
  87. "$schema": "https://opencode.ai/config.json",
  88. "agent": {
  89. "build": {
  90. "mode": "primary",
  91. "model": "anthropic/claude-sonnet-4-20250514",
  92. "prompt": "{file:./prompts/build.txt}",
  93. "permission": {
  94. "edit": "allow",
  95. "bash": "allow"
  96. }
  97. },
  98. "plan": {
  99. "mode": "primary",
  100. "model": "anthropic/claude-haiku-4-20250514",
  101. "permission": {
  102. "edit": "deny",
  103. "bash": "deny"
  104. }
  105. },
  106. "code-reviewer": {
  107. "description": "Reviews code for best practices and potential issues",
  108. "mode": "subagent",
  109. "model": "anthropic/claude-sonnet-4-20250514",
  110. "prompt": "You are a code reviewer. Focus on security, performance, and maintainability.",
  111. "permission": {
  112. "edit": "deny"
  113. }
  114. }
  115. }
  116. }
  117. ```
  118. ---
  119. ### Markdown
  120. You can also define agents using markdown files. Place them in:
  121. - Global: `~/.config/opencode/agents/`
  122. - Per-project: `.opencode/agents/`
  123. ```markdown title="~/.config/opencode/agents/review.md"
  124. ---
  125. description: Reviews code for quality and best practices
  126. mode: subagent
  127. model: anthropic/claude-sonnet-4-20250514
  128. temperature: 0.1
  129. permission:
  130. edit: deny
  131. bash: deny
  132. ---
  133. You are in code review mode. Focus on:
  134. - Code quality and best practices
  135. - Potential bugs and edge cases
  136. - Performance implications
  137. - Security considerations
  138. Provide constructive feedback without making direct changes.
  139. ```
  140. The markdown file name becomes the agent name. For example, `review.md` creates a `review` agent.
  141. ---
  142. ## Options
  143. Let's look at these configuration options in detail.
  144. ---
  145. ### Description
  146. Use the `description` option to provide a brief description of what the agent does and when to use it.
  147. ```json title="opencode.json"
  148. {
  149. "agent": {
  150. "review": {
  151. "description": "Reviews code for best practices and potential issues"
  152. }
  153. }
  154. }
  155. ```
  156. This is a **required** config option.
  157. ---
  158. ### Temperature
  159. Control the randomness and creativity of the LLM's responses with the `temperature` config.
  160. Lower values make responses more focused and deterministic, while higher values increase creativity and variability.
  161. ```json title="opencode.json"
  162. {
  163. "agent": {
  164. "plan": {
  165. "temperature": 0.1
  166. },
  167. "creative": {
  168. "temperature": 0.8
  169. }
  170. }
  171. }
  172. ```
  173. Temperature values typically range from 0.0 to 1.0:
  174. - **0.0-0.2**: Very focused and deterministic responses, ideal for code analysis and planning
  175. - **0.3-0.5**: Balanced responses with some creativity, good for general development tasks
  176. - **0.6-1.0**: More creative and varied responses, useful for brainstorming and exploration
  177. ```json title="opencode.json"
  178. {
  179. "agent": {
  180. "analyze": {
  181. "temperature": 0.1,
  182. "prompt": "{file:./prompts/analysis.txt}"
  183. },
  184. "build": {
  185. "temperature": 0.3
  186. },
  187. "brainstorm": {
  188. "temperature": 0.7,
  189. "prompt": "{file:./prompts/creative.txt}"
  190. }
  191. }
  192. }
  193. ```
  194. If no temperature is specified, OpenCode uses model-specific defaults; typically 0 for most models, 0.55 for Qwen models.
  195. ---
  196. ### Max steps
  197. Control the maximum number of agentic iterations an agent can perform before being forced to respond with text only. This allows users who wish to control costs to set a limit on agentic actions.
  198. If this is not set, the agent will continue to iterate until the model chooses to stop or the user interrupts the session.
  199. ```json title="opencode.json"
  200. {
  201. "agent": {
  202. "quick-thinker": {
  203. "description": "Fast reasoning with limited iterations",
  204. "prompt": "You are a quick thinker. Solve problems with minimal steps.",
  205. "steps": 5
  206. }
  207. }
  208. }
  209. ```
  210. When the limit is reached, the agent receives a special system prompt instructing it to respond with a summarization of its work and recommended remaining tasks.
  211. :::caution
  212. The legacy `maxSteps` field is deprecated. Use `steps` instead.
  213. :::
  214. ---
  215. ### Disable
  216. Set to `true` to disable the agent.
  217. ```json title="opencode.json"
  218. {
  219. "agent": {
  220. "review": {
  221. "disable": true
  222. }
  223. }
  224. }
  225. ```
  226. ---
  227. ### Prompt
  228. Specify a custom system prompt file for this agent with the `prompt` config. The prompt file should contain instructions specific to the agent's purpose.
  229. ```json title="opencode.json"
  230. {
  231. "agent": {
  232. "review": {
  233. "prompt": "{file:./prompts/code-review.txt}"
  234. }
  235. }
  236. }
  237. ```
  238. This path is relative to where the config file is located. So this works for both the global OpenCode config and the project specific config.
  239. ---
  240. ### Model
  241. Use the `model` config to override the model for this agent. Useful for using different models optimized for different tasks. For example, a faster model for planning, a more capable model for implementation.
  242. :::tip
  243. If you don’t specify a model, primary agents use the [model globally configured](/docs/config#models) while subagents will use the model of the primary agent that invoked the subagent.
  244. :::
  245. ```json title="opencode.json"
  246. {
  247. "agent": {
  248. "plan": {
  249. "model": "anthropic/claude-haiku-4-20250514"
  250. }
  251. }
  252. }
  253. ```
  254. The model ID in your OpenCode config uses the format `provider/model-id`. For example, if you're using [OpenCode Zen](/docs/zen), you would use `opencode/gpt-5.1-codex` for GPT 5.1 Codex.
  255. ---
  256. ### Tools (deprecated)
  257. `tools` is **deprecated**. Prefer the agent's [`permission`](#permissions) field for new configs, updates and more fine-grained control.
  258. Allows you to control which tools are available in this agent. You can enable or disable specific tools by setting them to `true` or `false`. In an agent's `tools` config, `true` is equivalent to `{"*": "allow"}` permission and `false` is equivalent to `{"*": "deny"}` permission.
  259. ```json title="opencode.json" {3-6,9-12}
  260. {
  261. "$schema": "https://opencode.ai/config.json",
  262. "tools": {
  263. "write": true,
  264. "bash": true
  265. },
  266. "agent": {
  267. "plan": {
  268. "tools": {
  269. "write": false,
  270. "bash": false
  271. }
  272. }
  273. }
  274. }
  275. ```
  276. :::note
  277. The agent-specific config overrides the global config.
  278. :::
  279. You can also use wildcards in legacy `tools` entries to control multiple tools at once. For example, to disable all tools from an MCP server:
  280. ```json title="opencode.json"
  281. {
  282. "$schema": "https://opencode.ai/config.json",
  283. "agent": {
  284. "readonly": {
  285. "tools": {
  286. "mymcp_*": false,
  287. "write": false,
  288. "edit": false
  289. }
  290. }
  291. }
  292. }
  293. ```
  294. [Learn more about tools](/docs/tools).
  295. ---
  296. ### Permissions
  297. You can configure permissions to manage what actions an agent can take. Each permission key can be set to:
  298. - `"ask"` — Prompt for approval before running the tool
  299. - `"allow"` — Allow all operations without approval
  300. - `"deny"` — Disable the tool
  301. The available permission keys are:
  302. | Key | Tools it gates |
  303. | -------------------- | ---------------------------------------------------------------- |
  304. | `read` | `read` |
  305. | `edit` | `write`, `edit`, `apply_patch` |
  306. | `glob` | `glob` |
  307. | `grep` | `grep` |
  308. | `list` | `list` |
  309. | `bash` | `bash` |
  310. | `task` | `task` |
  311. | `external_directory` | Any tool that reads or writes files outside the project worktree |
  312. | `todowrite` | `todowrite`, `todoread` |
  313. | `webfetch` | `webfetch` |
  314. | `websearch` | `websearch` |
  315. | `lsp` | `lsp` |
  316. | `skill` | `skill` |
  317. | `question` | `question` |
  318. | `doom_loop` | Recovery prompts when an agent appears stuck |
  319. `read`, `edit`, `glob`, `grep`, `list`, `bash`, `task`, `external_directory`, `lsp`, and `skill` accept either a shorthand action (`"allow" | "ask" | "deny"`) or an object of glob/pattern → action for fine-grained control. The remaining keys accept the shorthand action only.
  320. :::note
  321. Permission keys are matched as wildcard patterns against the underlying tool name, so the same syntax works for built-ins, custom tools, and MCP tools — for example `"mymcp_*": "deny"` denies every tool from an MCP server, and `"mymcp_search": "ask"` targets a single one.
  322. :::
  323. ```json title="opencode.json"
  324. {
  325. "$schema": "https://opencode.ai/config.json",
  326. "permission": {
  327. "edit": "deny"
  328. }
  329. }
  330. ```
  331. You can override these permissions per agent.
  332. ```json title="opencode.json" {3-5,8-10}
  333. {
  334. "$schema": "https://opencode.ai/config.json",
  335. "permission": {
  336. "edit": "deny"
  337. },
  338. "agent": {
  339. "build": {
  340. "permission": {
  341. "edit": "ask"
  342. }
  343. }
  344. }
  345. }
  346. ```
  347. You can also set permissions in Markdown agents.
  348. ```markdown title="~/.config/opencode/agents/review.md"
  349. ---
  350. description: Code review without edits
  351. mode: subagent
  352. permission:
  353. edit: deny
  354. bash:
  355. "*": ask
  356. "git diff": allow
  357. "git log*": allow
  358. "grep *": allow
  359. webfetch: deny
  360. ---
  361. Only analyze code and suggest changes.
  362. ```
  363. You can set permissions for specific bash commands.
  364. ```json title="opencode.json" {7}
  365. {
  366. "$schema": "https://opencode.ai/config.json",
  367. "agent": {
  368. "build": {
  369. "permission": {
  370. "bash": {
  371. "git push": "ask",
  372. "grep *": "allow"
  373. }
  374. }
  375. }
  376. }
  377. }
  378. ```
  379. This can take a glob pattern.
  380. ```json title="opencode.json" {7}
  381. {
  382. "$schema": "https://opencode.ai/config.json",
  383. "agent": {
  384. "build": {
  385. "permission": {
  386. "bash": {
  387. "git *": "ask"
  388. }
  389. }
  390. }
  391. }
  392. }
  393. ```
  394. And you can also use the `*` wildcard to manage permissions for all commands.
  395. Since the last matching rule takes precedence, put the `*` wildcard first and specific rules after.
  396. ```json title="opencode.json" {8}
  397. {
  398. "$schema": "https://opencode.ai/config.json",
  399. "agent": {
  400. "build": {
  401. "permission": {
  402. "bash": {
  403. "*": "ask",
  404. "git status *": "allow"
  405. }
  406. }
  407. }
  408. }
  409. }
  410. ```
  411. [Learn more about permissions](/docs/permissions).
  412. ---
  413. ### Mode
  414. Control the agent's mode with the `mode` config. The `mode` option is used to determine how the agent can be used.
  415. ```json title="opencode.json"
  416. {
  417. "agent": {
  418. "review": {
  419. "mode": "subagent"
  420. }
  421. }
  422. }
  423. ```
  424. The `mode` option can be set to `primary`, `subagent`, or `all`. If no `mode` is specified, it defaults to `all`.
  425. ---
  426. ### Hidden
  427. Hide a subagent from the `@` autocomplete menu with `hidden: true`. Useful for internal subagents that should only be invoked programmatically by other agents via the Task tool.
  428. ```json title="opencode.json"
  429. {
  430. "agent": {
  431. "internal-helper": {
  432. "mode": "subagent",
  433. "hidden": true
  434. }
  435. }
  436. }
  437. ```
  438. This only affects user visibility in the autocomplete menu. Hidden agents can still be invoked by the model via the Task tool if permissions allow.
  439. :::note
  440. Only applies to `mode: subagent` agents.
  441. :::
  442. ---
  443. ### Task permissions
  444. Control which subagents an agent can invoke via the Task tool with `permission.task`. Uses glob patterns for flexible matching.
  445. ```json title="opencode.json"
  446. {
  447. "agent": {
  448. "orchestrator": {
  449. "mode": "primary",
  450. "permission": {
  451. "task": {
  452. "*": "deny",
  453. "orchestrator-*": "allow",
  454. "code-reviewer": "ask"
  455. }
  456. }
  457. }
  458. }
  459. }
  460. ```
  461. When set to `deny`, the subagent is removed from the Task tool description entirely, so the model won't attempt to invoke it.
  462. :::tip
  463. Rules are evaluated in order, and the **last matching rule wins**. In the example above, `orchestrator-planner` matches both `*` (deny) and `orchestrator-*` (allow), but since `orchestrator-*` comes after `*`, the result is `allow`.
  464. :::
  465. :::tip
  466. Users can always invoke any subagent directly via the `@` autocomplete menu, even if the agent's task permissions would deny it.
  467. :::
  468. ---
  469. ### Color
  470. Customize the agent's visual appearance in the UI with the `color` option. This affects how the agent appears in the interface.
  471. Use a valid hex color (e.g., `#FF5733`) or theme color: `primary`, `secondary`, `accent`, `success`, `warning`, `error`, `info`.
  472. ```json title="opencode.json"
  473. {
  474. "agent": {
  475. "creative": {
  476. "color": "#ff6b6b"
  477. },
  478. "code-reviewer": {
  479. "color": "accent"
  480. }
  481. }
  482. }
  483. ```
  484. ---
  485. ### Top P
  486. Control response diversity with the `top_p` option. Alternative to temperature for controlling randomness.
  487. ```json title="opencode.json"
  488. {
  489. "agent": {
  490. "brainstorm": {
  491. "top_p": 0.9
  492. }
  493. }
  494. }
  495. ```
  496. Values range from 0.0 to 1.0. Lower values are more focused, higher values more diverse.
  497. ---
  498. ### Additional
  499. Any other options you specify in your agent configuration will be **passed through directly** to the provider as model options. This allows you to use provider-specific features and parameters.
  500. For example, with OpenAI's reasoning models, you can control the reasoning effort:
  501. ```json title="opencode.json" {6,7}
  502. {
  503. "agent": {
  504. "deep-thinker": {
  505. "description": "Agent that uses high reasoning effort for complex problems",
  506. "model": "openai/gpt-5",
  507. "reasoningEffort": "high",
  508. "textVerbosity": "low"
  509. }
  510. }
  511. }
  512. ```
  513. These additional options are model and provider-specific. Check your provider's documentation for available parameters.
  514. :::tip
  515. Run `opencode models` to see a list of the available models.
  516. :::
  517. ---
  518. ## Create agents
  519. You can create new agents using the following command:
  520. ```bash
  521. opencode agent create
  522. ```
  523. This interactive command will:
  524. 1. Ask where to save the agent; global or project-specific.
  525. 2. Description of what the agent should do.
  526. 3. Generate an appropriate system prompt and identifier.
  527. 4. Let you select which permissions the agent should be allowed (anything you don't select is denied).
  528. 5. Finally, create a markdown file with the agent configuration.
  529. ---
  530. ## Use cases
  531. Here are some common use cases for different agents.
  532. - **Build agent**: Full development work with all tools enabled
  533. - **Plan agent**: Analysis and planning without making changes
  534. - **Review agent**: Code review with read-only access plus documentation tools
  535. - **Debug agent**: Focused on investigation with bash and read tools enabled
  536. - **Docs agent**: Documentation writing with file operations but no system commands
  537. ---
  538. ## Examples
  539. Here are some example agents you might find useful.
  540. :::tip
  541. Do you have an agent you'd like to share? [Submit a PR](https://github.com/anomalyco/opencode).
  542. :::
  543. ---
  544. ### Documentation agent
  545. ```markdown title="~/.config/opencode/agents/docs-writer.md"
  546. ---
  547. description: Writes and maintains project documentation
  548. mode: subagent
  549. permission:
  550. bash: deny
  551. ---
  552. You are a technical writer. Create clear, comprehensive documentation.
  553. Focus on:
  554. - Clear explanations
  555. - Proper structure
  556. - Code examples
  557. - User-friendly language
  558. ```
  559. ---
  560. ### Security auditor
  561. ```markdown title="~/.config/opencode/agents/security-auditor.md"
  562. ---
  563. description: Performs security audits and identifies vulnerabilities
  564. mode: subagent
  565. permission:
  566. edit: deny
  567. ---
  568. You are a security expert. Focus on identifying potential security issues.
  569. Look for:
  570. - Input validation vulnerabilities
  571. - Authentication and authorization flaws
  572. - Data exposure risks
  573. - Dependency vulnerabilities
  574. - Configuration security issues
  575. ```