agents.mdx 20 KB

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