Agents
Relevant source files
The following files were used as context for generating this wiki page:- README.md
- packages/opencode/src/acp/README.md
- packages/opencode/src/agent/generate.txt
- packages/opencode/src/cli/cmd/agent.ts
- packages/opencode/src/command/template/initialize.txt
- packages/opencode/src/session/prompt/anthropic-20250930.txt
- packages/opencode/src/session/prompt/anthropic.txt
- packages/opencode/src/session/prompt/codex.txt
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/qwen.txt
- packages/opencode/src/tool/task.txt
Agents are specialized, autonomous AI entities within the OpenCode ecosystem, designed to perform a wide range of software engineering tasks. They are defined by a specific persona, a set of instructions (system prompt), and access to a curated list of tools. This modular design allows for the creation of highly-focused experts for tasks like code generation, review, analysis, and more.
OpenCode provides built-in agents and allows users to create custom agents tailored to their specific needs, either globally or within a project. These agents can operate as primary assistants or be invoked as subagents by other agents to handle complex, multi-step workflows.
Sources: README.md, packages/opencode/src/agent/generate.txt, packages/opencode/src/cli/cmd/agent.ts
Core Concepts
Agent Definition
A custom agent is defined by a markdown file (.md) containing a system prompt and frontmatter metadata. The generation process, initiated by a user's description, produces a JSON object that is then converted into this file format.
Sources: packages/opencode/src/cli/cmd/agent.ts:114-121
The core components of an agent's definition are:
| Field | Description |
|---|---|
identifier | A unique, descriptive name using lowercase letters, numbers, and hyphens (e.g., code-reviewer). This becomes the agent's filename. |
whenToUse | A precise description of when the agent should be used, including trigger conditions and examples. This is stored in the description frontmatter field. |
systemPrompt | The complete set of instructions governing the agent's persona, behavior, methodologies, and operational parameters. This forms the main content of the markdown file. |
Sources: packages/opencode/src/agent/generate.txt:46-52
Agent Modes
Agents can be configured to operate in different modes, which define their role and how they can be invoked.
| Mode | Description |
|---|---|
primary | Acts as a primary/main agent. |
subagent | Can be used as a subagent by other agents via the Task tool. |
all | Can function in both primary and subagent roles. |
Sources: packages/opencode/src/cli/cmd/agent.ts:91-108
Built-in Agents
OpenCode includes two built-in agents available by default. Users can switch between them using the Tab key in the terminal.
| Agent | Description |
|---|---|
build | The default, full-access agent intended for general development work. |
plan | A read-only agent for analysis and code exploration. It denies file edits and asks for permission before running bash commands. |
general | An internal subagent used for complex searches and multi-step tasks. It can be invoked using @general in messages. |
Sources: README.md:52-62
Subagents and the Task Tool
A key architectural feature is the ability for agents to delegate complex tasks to other, more specialized agents. This is achieved using the Task tool. When an agent uses the Task tool, it launches a new, stateless subagent to handle a specific sub-problem. This allows for the composition of complex behaviors from simpler, single-purpose agents.
The diagram below illustrates the flow of an agent using the Task tool to invoke a subagent.
Agent Management via CLI
The opencode agent command provides a suite of tools for managing agents.
Sources: packages/opencode/src/cli/cmd/agent.ts:145-149
Creating an Agent
The opencode agent create command walks the user through a series of prompts to generate a new agent configuration.
This flowchart shows the agent creation process.
Steps:
- Scope: If in a Git repository, choose whether the agent is for the current project (
.opencode/agent/) or global (~/.opencode/agent/). - Description: Provide a natural language description of what the agent should do.
- Generation: An LLM call generates the agent's
identifier,whenToUsedescription, andsystemPrompt. - Tools: Select which tools the agent can access from a predefined list.
- Mode: Choose the agent's operational mode (
all,primary, orsubagent). - File Creation: A markdown file (e.g.,
code-reviewer.md) is created in the selected scope with the system prompt as content and configuration (description, mode, tools) as frontmatter.
Sources: packages/opencode/src/cli/cmd/agent.ts:13-123
Listing Agents
The opencode agent list command displays all available agents, including their name and mode. The list is sorted to show built-in agents first, followed by custom agents in alphabetical order.
Sources: packages/opencode/src/cli/cmd/agent.ts:128-143
Agent Behavior and Execution
An agent's behavior is primarily governed by its system prompt. The various prompt templates in the repository establish a baseline for all agents.
Core Instructions
Across different model-specific prompts, several key instructions are consistent:
- Conciseness: Agents should be concise and direct, especially in a CLI environment.
- Convention Adherence: Agents must mimic the coding style, libraries, and patterns of the existing codebase.
- Verification: After making changes, agents are expected to run verification steps like linting and type-checking commands if they are known.
- Task Planning: For complex tasks, agents should use the
TodoWritetool to create and track a plan. - Security: Agents must refuse to work on malicious code and must never expose secrets.
Sources: packages/opencode/src/session/prompt/qwen.txt:1-120, packages/opencode/src/session/prompt/gemini.txt:1-50, packages/opencode/src/session/prompt/anthropic.txt:28-68
AGENTS.md File
Agents are instructed to look for and obey instructions within AGENTS.md files. These files provide repository-specific context and rules, such as build commands, code style guidelines, and naming conventions. The scope of an AGENTS.md file applies to the directory it's in and all subdirectories, with more deeply nested files taking precedence.
Sources: packages/opencode/src/session/prompt/codex.txt:16-30, packages/opencode/src/command/template/initialize.txt:1-6
Agent Client Protocol (ACP) Integration
OpenCode agents can be exposed to external clients, such as the Zed editor, through the Agent Client Protocol (ACP). The implementation maps ACP sessions to internal OpenCode sessions, allowing an ACP client to initialize an agent, manage its lifecycle, and send prompts. This integration demonstrates the modularity of the agent system, enabling it to be driven by interfaces other than the OpenCode TUI.