Configuration
Relevant source files
The following files were used as context for generating this wiki page:- README.md
- packages/sdk/stainless/openapi.json
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/codex.txt
- packages/opencode/src/agent/generate.txt
- packages/opencode/src/command/template/review.txt
- packages/opencode/src/command/template/initialize.txt
- AGENTS.md
- packages/opencode/src/session/prompt/beast.txt
Agents are the core of OpenCode, acting as specialized AI assistants designed to handle a variety of software engineering tasks. Each agent is defined by a unique persona, a set of comprehensive instructions, and specific permissions that govern its capabilities and behavior. This system allows for a flexible and powerful approach to coding assistance, from high-level planning and exploration to direct code implementation and review.
OpenCode comes with built-in agents and also supports custom agent configurations. This allows users to tailor the AI's behavior to specific project needs, coding styles, and workflows. Agents can be invoked directly, used in commands, or triggered by other system events, making them a fundamental component of the OpenCode architecture.
Core Concepts
OpenCode provides a set of built-in agents, each with a distinct purpose. Users can switch between these agents to leverage their specialized skills.
Sources: README.md
Built-in Agents
The system includes two primary agents and one subagent for internal tasks.
| Agent | Type | Description |
|---|---|---|
build | Primary | The default agent with full access, intended for general development work. |
plan | Primary | A read-only agent designed for analysis and code exploration. It denies file edits and asks for permission before running bash commands. |
general | Subagent | Used internally for complex searches and multi-step tasks. It can be invoked in messages via @general. |
Sources: README.md
This diagram illustrates the roles of the primary built-in agents.
Sources: README.md
Agent Configuration
Agent behavior is defined through a detailed configuration structure. These settings can be specified in the main configuration file and are exposed via the API.
Sources: packages/sdk/stainless/openapi.json
Agent Properties
The Agent object in the API contains all configuration details for a single agent.
| Property | Type | Description |
|---|---|---|
name | string | The unique identifier for the agent. |
description | string | A description of when the agent should be used. |
mode | subagent | primary | all | Defines the agent's role in the system. |
builtIn | boolean | Indicates if the agent is a built-in part of OpenCode. |
model | { modelID: string, providerID: string } | The specific AI model the agent will use. |
prompt | string | The system prompt that defines the agent's persona, instructions, and behavior. |
tools | object | A map of tool IDs to a boolean, enabling or disabling specific tools for the agent. |
permission | object | Defines the agent's permissions for actions like edit, bash, and webfetch. |
topP | number | The Top-P sampling value for the model. |
temperature | number | The temperature setting for the model. |
options | object | Additional provider-specific options. |
Sources: packages/sdk/stainless/openapi.json (schema: components.schemas.Agent)
Agent Permissions
Permissions are a critical part of agent configuration, ensuring that agents operate within safe boundaries. The permission object controls an agent's ability to perform potentially destructive or sensitive actions.
| Action | Values | Description |
|---|---|---|
edit | ask allow deny | Governs the agent's ability to edit files. |
bash | ask allow deny | Governs the agent's ability to execute shell commands. Can be a single value or a map of command patterns. |
webfetch | ask allow deny | Governs the agent's ability to access the internet. |
Sources: packages/sdk/stainless/openapi.json (schema: components.schemas.AgentConfig.properties.permission)
The following diagram shows the permission check flow for an agent action.
Sources: packages/sdk/stainless/openapi.json (schema: components.schemas.AgentConfig.properties.permission)
System Prompts
The prompt property is the most important part of an agent's configuration. It's a text file containing detailed instructions that define the agent's persona, workflow, and communication style. Different models may have differently structured prompts.
For example, the gemini.txt prompt includes:
- Core Mandates: Strict rules on adhering to project conventions, verifying library usage, and mimicking existing code style.
- Primary Workflows: Step-by-step procedures for software engineering tasks (Understand -> Plan -> Implement -> Verify).
- Tone and Style: Guidelines for concise, direct communication suitable for a CLI environment.
Sources: packages/opencode/src/session/prompt/gemini.txt:3-49
Another example, beast.txt, defines a specific workflow:
- Fetch Provided URLs
- Deeply Understand the Problem
- Codebase Investigation
- Internet Research
- Develop a Detailed Plan
- Making Code Changes
- Debugging
- Test frequently
- Iterate
- Reflect and validate
Sources: packages/opencode/src/session/prompt/beast.txt:24-52
AGENTS.md: Project-Specific Instructions
To provide project-specific context, OpenCode agents read AGENTS.md files. These Markdown files allow developers to give instructions about coding conventions, project structure, or how to run and test code.
Key behaviors of AGENTS.md:
- Scope: An
AGENTS.mdfile applies to the entire directory tree where it is located. - Precedence: More deeply-nested
AGENTS.mdfiles take precedence over those in parent directories. Direct user instructions in a prompt always overrideAGENTS.md. - Initialization: The system can be prompted to analyze a codebase and create an
AGENTS.mdfile, including build/lint/test commands and code style guidelines.
Sources: packages/opencode/src/session/prompt/codex.txt:12-25, packages/opencode/src/command/template/initialize.txt:1-6
This diagram shows the hierarchy of instructions for an agent.
Agent Interaction and API
Agents are managed and invoked through the OpenCode server API.
API Endpoints
| Endpoint | Method | Operation ID | Description |
|---|---|---|---|
/agent | GET | app.agents | List all available agents and their configurations. |
/session/{id}/message | POST | session.prompt | Sends a new message to a session, optionally specifying an agent to handle it. |
/session/{id}/command | POST | session.command | Sends a command to a session, which can be handled by a specified agent. |
Sources: packages/sdk/stainless/openapi.json (paths: /agent, /session/{id}/message, /session/{id}/command)
Invocation Flow
The following sequence diagram illustrates how a user prompt invokes an agent via the API.
Sources: packages/sdk/stainless/openapi.json (paths: /session/{id}/message)
Custom and Specialized Agents
Beyond the built-in agents, OpenCode is designed for customization.
Generating New Agents
A specialized agent can be used to generate new agent configurations. This "agent architect" takes user requirements and produces a valid JSON object defining the new agent.
The output JSON must contain:
identifier: A unique, hyphenated name (e.g.,code-reviewer).whenToUse: A description of the agent's trigger conditions and use cases.systemPrompt: The complete system prompt that governs the agent's behavior.
Sources: packages/opencode/src/agent/generate.txt:1-45
Example: Code Review Agent
The review.txt template provides the system prompt for a specialized code-reviewer agent. This demonstrates how a focused agent is defined.
Reviewer Agent Instructions:
- Input: Can review uncommitted changes, a specific commit, a branch comparison, or a pull request.
- Focus Areas:
- Bugs: Logic errors, edge cases, security issues, and broken error handling.
- Structure: Adherence to existing patterns and conventions.
- Performance: Obvious issues like O(n²) loops or N+1 queries.
- Tone: Matter-of-fact, direct, and clear. Avoids flattery and style-based critiques.
Sources: packages/opencode/src/command/template/review.txt:1-49
Summary
Agents are the intelligent core of OpenCode, providing a structured and highly configurable way to interact with AI models for coding tasks. The architecture supports both general-purpose built-in agents and specialized, custom-defined agents. Through a combination of system prompts, permission controls, and project-specific AGENTS.md files, developers can create a tailored AI-assisted development environment that aligns with their specific needs and conventions. The API provides the necessary endpoints to manage and interact with these agents, making them a central and extensible feature of the platform.