agents.mdx 16 KB

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