Architecture: OpenCode Core Engine
Relevant source files
The following files were used as context for generating this wiki page:- packages/opencode/src/index.ts
- packages/opencode/src/session/processor.ts
- packages/opencode/src/agent/agent.ts
- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/agent/generate.txt
- README.md
- packages/opencode/src/command/template/review.txt
- packages/opencode/src/command/template/initialize.txt
- packages/opencode/src/session/prompt/beast.txt
- packages/opencode/src/session/prompt/codex.txt
The OpenCode Core Engine is an AI-powered coding assistant designed to operate within a terminal environment. It is a 100% open-source, provider-agnostic system, capable of integrating with various LLM providers like Anthropic (Claude), OpenAI, Google, and local models. The architecture is centered around a client/server model, allowing the core logic to run on one machine while being driven by various clients, with the primary interface being a rich Terminal UI (TUI).
The engine's power lies in its modular and extensible agent-based system. Each agent is defined by a detailed system prompt that dictates its persona, capabilities, and operational workflow. This allows for the creation of specialized agents for tasks like development, code analysis, and review, all while adhering to strict operational guidelines and project-specific conventions.
Sources: README.md, packages/console/app/src/routes/index.tsx
High-Level Architecture
The OpenCode system is composed of several key components that work together to provide an interactive and powerful coding experience. The client/server architecture allows for flexibility, where the TUI is just one possible frontend.
Sources: README.md
This diagram illustrates the main components of the OpenCode architecture.
Agent Personas and Workflows
The behavior of each agent is not hardcoded but is defined by a detailed system prompt. These prompts, stored in .txt files, establish the agent's persona, core mandates, operational guidelines, and primary workflows. This makes the system highly configurable and adaptable.
Sources: packages/opencode/src/session/prompt/gemini.txt, packages/opencode/src/session/prompt/beast.txt, packages/opencode/src/session/prompt/codex.txt
Core Mandates
All agents operate under a set of strict rules to ensure safety, consistency, and efficiency.
| Mandate | Description |
|---|---|
| Conventions | Rigorously adhere to existing project conventions (style, structure, frameworks). |
| Libraries | Never assume a library is available; verify its usage in the project first. |
| Idiomatic Changes | Ensure changes integrate naturally with the local code context. |
| Proactiveness | Fulfill the request thoroughly, including reasonable, implied follow-up actions. |
| Confirm Ambiguity | Do not take significant actions beyond the request's scope without user confirmation. |
| Path Construction | Always construct and use absolute file paths for file system tools. |
| Security First | Never introduce code that exposes, logs, or commits secrets or API keys. |
Sources: packages/opencode/src/session/prompt/gemini.txt:5-30
Primary Workflow: Software Engineering Task
When tasked with a software engineering request (e.g., fixing a bug, adding a feature), the agent follows a structured, iterative process.
This flowchart shows the agent's standard procedure for handling development tasks.
Agent Types
OpenCode comes with built-in agents for different modes of operation and can be extended with custom, specialized agents.
Built-in Agents
| Agent | Description |
|---|---|
| build | The default, full-access agent for 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. |
Sources: README.md
Custom Agents & Commands
The system supports creating specialized agents through templates. These agents are designed for specific tasks like code reviews or project initialization.
review Agent
The review agent is a specialized code reviewer. Its behavior is defined by a template that outlines what to review, what to look for, and how to provide feedback.
The following diagram shows the logic the review agent uses to determine the scope of its review.
initialize Agent
The initialize command triggers an agent to analyze the codebase and create or improve an AGENTS.md file. This file contains instructions for other agents, such as build/lint/test commands and code style guidelines, ensuring that all agentic work in the repository is consistent.
Sources: packages/opencode/src/command/template/initialize.txt, packages/opencode/src/session/prompt/codex.txt:11-26
Agent Generation System
OpenCode includes a meta-agent capable of creating new agent configurations. This system translates a user's high-level description of a desired agent into a formal JSON specification.
Generation Process
- Extract Core Intent: The system identifies the agent's purpose and success criteria from the user's request.
- Design Expert Persona: It creates an expert identity relevant to the task.
- Architect Instructions: A detailed system prompt is generated, defining behavior, methodologies, and edge cases.
- Create Identifier: A unique, descriptive identifier is created (e.g.,
code-reviewer). - Generate JSON: The final output is a JSON object containing the agent's full configuration.
Sources: packages/opencode/src/agent/generate.txt
Agent Configuration Structure
The generated configuration is a JSON object with a specific structure.
| Field | Description |
|---|---|
identifier | A unique, descriptive identifier using lowercase letters, numbers, and hyphens. |
whenToUse | A precise description of when to use the agent, including examples. |
systemPrompt | The complete system prompt that governs the agent's behavior, written in the second person ("You are..."). |
Sources: packages/opencode/src/agent/generate.txt:50-55
Conclusion
The OpenCode Core Engine is a sophisticated, prompt-driven system designed for extensibility and adaptability. Its architecture separates the core operational logic from the agent's behavioral definitions, which are managed through plain-text system prompts. This allows for rapid development of new capabilities and specialized agents without altering the core engine code. By relying on a structured workflow of understanding, planning, implementing, and verifying, the engine ensures that agent actions are safe, consistent, and aligned with project standards. The ability to generate new agents on the fly further enhances its power, making it a versatile tool for a wide range of software engineering tasks.