agents.mdx 14 KB

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