agents.mdx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. - **<Leader>+Right** (or your configured `session_child_cycle` keybind) to cycle forward through parent → child1 → child2 → ... → parent
  55. - **<Leader>+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" {3-6,9-12}
  218. {
  219. "$schema": "https://opencode.ai/config.json",
  220. "tools": {
  221. "write": true,
  222. "bash": true
  223. },
  224. "agent": {
  225. "plan": {
  226. "tools": {
  227. "write": false,
  228. "bash": false
  229. }
  230. }
  231. }
  232. }
  233. ```
  234. :::note
  235. The agent-specific config overrides the global config.
  236. :::
  237. You can also use wildcards to control multiple tools at once. For example, to disable all tools from an MCP server:
  238. ```json title="opencode.json"
  239. {
  240. "$schema": "https://opencode.ai/config.json",
  241. "agent": {
  242. "readonly": {
  243. "tools": {
  244. "mymcp_*": false,
  245. "write": false,
  246. "edit": false
  247. }
  248. }
  249. }
  250. }
  251. ```
  252. [Learn more about tools](/docs/tools).
  253. ---
  254. ### Permissions
  255. 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:
  256. - `"ask"` — Prompt for approval before running the tool
  257. - `"allow"` — Allow all operations without approval
  258. - `"deny"` — Disable the tool
  259. ```json title="opencode.json"
  260. {
  261. "$schema": "https://opencode.ai/config.json",
  262. "permission": {
  263. "edit": "deny"
  264. }
  265. }
  266. ```
  267. You can override these permissions per agent.
  268. ```json title="opencode.json" {3-5,8-10}
  269. {
  270. "$schema": "https://opencode.ai/config.json",
  271. "permission": {
  272. "edit": "deny"
  273. },
  274. "agent": {
  275. "build": {
  276. "permission": {
  277. "edit": "ask"
  278. }
  279. }
  280. }
  281. }
  282. ```
  283. You can also set permissions in Markdown agents.
  284. ```markdown title="~/.config/opencode/agent/review.md"
  285. ---
  286. description: Code review without edits
  287. mode: subagent
  288. permission:
  289. edit: deny
  290. bash:
  291. "git diff": allow
  292. "git log*": allow
  293. "*": ask
  294. webfetch: deny
  295. ---
  296. Only analyze code and suggest changes.
  297. ```
  298. You can set permissions for specific bash commands.
  299. ```json title="opencode.json" {7}
  300. {
  301. "$schema": "https://opencode.ai/config.json",
  302. "agent": {
  303. "build": {
  304. "permission": {
  305. "bash": {
  306. "git push": "ask"
  307. }
  308. }
  309. }
  310. }
  311. }
  312. ```
  313. This can take a glob pattern.
  314. ```json title="opencode.json" {7}
  315. {
  316. "$schema": "https://opencode.ai/config.json",
  317. "agent": {
  318. "build": {
  319. "permission": {
  320. "bash": {
  321. "git *": "ask"
  322. }
  323. }
  324. }
  325. }
  326. }
  327. ```
  328. And you can also use the `*` wildcard to manage permissions for all commands.
  329. Where the specific rule can override the `*` wildcard.
  330. ```json title="opencode.json" {8}
  331. {
  332. "$schema": "https://opencode.ai/config.json",
  333. "agent": {
  334. "build": {
  335. "permission": {
  336. "bash": {
  337. "git status": "allow",
  338. "*": "ask"
  339. }
  340. }
  341. }
  342. }
  343. }
  344. ```
  345. [Learn more about permissions](/docs/permissions).
  346. ---
  347. ### Mode
  348. Control the agent's mode with the `mode` config. The `mode` option is used to determine how the agent can be used.
  349. ```json title="opencode.json"
  350. {
  351. "agent": {
  352. "review": {
  353. "mode": "subagent"
  354. }
  355. }
  356. }
  357. ```
  358. The `mode` option can be set to `primary`, `subagent`, or `all`. If no `mode` is specified, it defaults to `all`.
  359. ---
  360. ### Additional
  361. 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.
  362. For example, with OpenAI's reasoning models, you can control the reasoning effort:
  363. ```json title="opencode.json" {6,7}
  364. {
  365. "agent": {
  366. "deep-thinker": {
  367. "description": "Agent that uses high reasoning effort for complex problems",
  368. "model": "openai/gpt-5",
  369. "reasoningEffort": "high",
  370. "textVerbosity": "low"
  371. }
  372. }
  373. }
  374. ```
  375. These additional options are model and provider-specific. Check your provider's documentation for available parameters.
  376. :::tip
  377. Run `opencode models` to see a list of the available models.
  378. :::
  379. ---
  380. ## Create agents
  381. You can create new agents using the following command:
  382. ```bash
  383. opencode agent create
  384. ```
  385. This interactive command will:
  386. 1. Ask where to save the agent; global or project-specific.
  387. 2. Description of what the agent should do.
  388. 3. Generate an appropriate system prompt and identifier.
  389. 4. Let you select which tools the agent can access.
  390. 5. Finally, create a markdown file with the agent configuration.
  391. ---
  392. ## Use cases
  393. Here are some common use cases for different agents.
  394. - **Build agent**: Full development work with all tools enabled
  395. - **Plan agent**: Analysis and planning without making changes
  396. - **Review agent**: Code review with read-only access plus documentation tools
  397. - **Debug agent**: Focused on investigation with bash and read tools enabled
  398. - **Docs agent**: Documentation writing with file operations but no system commands
  399. ---
  400. ## Examples
  401. Here are some examples agents you might find useful.
  402. :::tip
  403. Do you have an agent you'd like to share? [Submit a PR](https://github.com/sst/opencode).
  404. :::
  405. ---
  406. ### Documentation agent
  407. ```markdown title="~/.config/opencode/agent/docs-writer.md"
  408. ---
  409. description: Writes and maintains project documentation
  410. mode: subagent
  411. tools:
  412. bash: false
  413. ---
  414. You are a technical writer. Create clear, comprehensive documentation.
  415. Focus on:
  416. - Clear explanations
  417. - Proper structure
  418. - Code examples
  419. - User-friendly language
  420. ```
  421. ---
  422. ### Security auditor
  423. ```markdown title="~/.config/opencode/agent/security-auditor.md"
  424. ---
  425. description: Performs security audits and identifies vulnerabilities
  426. mode: subagent
  427. tools:
  428. write: false
  429. edit: false
  430. ---
  431. You are a security expert. Focus on identifying potential security issues.
  432. Look for:
  433. - Input validation vulnerabilities
  434. - Authentication and authorization flaws
  435. - Data exposure risks
  436. - Dependency vulnerabilities
  437. - Configuration security issues
  438. ```