System Architecture Overview
Relevant source files
The following files were used as context for generating this wiki page:- packages/opencode/src/session/prompt/gemini.txt
- packages/opencode/src/session/prompt/codex.txt
- CONTRIBUTING.md
- packages/console/app/src/routes/index.tsx
- packages/desktop/AGENTS.md
- packages/opencode/src/agent/generate.txt
- packages/opencode/src/command/template/review.txt
- packages/opencode/src/command/template/initialize.txt
OpenCode is an open-source, interactive AI coding agent designed to assist with software engineering tasks directly from the command line. Its architecture is modular, centered around a core business logic package, a set of user interfaces (TUI, Web, Desktop), and a flexible agent system. The agent's behavior is dynamically defined by a series of prompt templates and configuration files, allowing for specialized "personas" tailored to different tasks like code generation, review, and project initialization.
The project is structured as a monorepo, utilizing Bun for dependency management and development scripts. This architecture supports multiple independent packages for the core logic, user interfaces, and plugins, enabling parallel development and clear separation of concerns. The system is designed to be extensible, allowing users to connect to over 75 LLM providers, including local models, and to define custom agent behaviors.
Core Components
The OpenCode project is divided into several key packages, each with a distinct responsibility.
| Component/Package | Description | Source |
|---|---|---|
packages/opencode | The core business logic and server for the OpenCode agent. | CONTRIBUTING.md:21 |
packages/opencode/src/cli/cmd/tui/ | The Terminal User Interface (TUI), built with SolidJS and opentui. | CONTRIBUTING.md:22 |
packages/console | The web-based console and public-facing website, also built with SolidJS. | packages/console/app/src/routes/index.tsx |
packages/desktop | The desktop application, which has its own specific agent guidelines. | packages/desktop/AGENTS.md |
packages/plugin | The source for the @opencode-ai/plugin, indicating a plugin architecture. | CONTRIBUTING.md:23 |
This diagram illustrates the high-level relationship between the main components.
Agent Architecture
The heart of OpenCode is its flexible agent architecture. Agents are not hardcoded but are defined by a combination of system prompts, behavioral guidelines, and tool access. This allows for the creation of specialized agents for various tasks.
Agent Personas and Prompts
Each agent's personality, workflow, and capabilities are defined in plain text files, which serve as system prompts. The system includes multiple personas, such as:
gemini.txt: A general-purpose software engineering agent.codex.txt: An agent focused on precise, safe, and helpful coding with an emphasis on planning.beast.txt: A highly autonomous agent designed to solve problems completely before yielding to the user, with a strong emphasis on internet research.review.txt: A specialized code reviewer agent that analyzes diffs and provides actionable feedback.
These prompts establish core mandates, operational guidelines, and interaction styles. For example, the gemini.txt prompt instructs the agent to rigorously adhere to project conventions, mimic existing code style, and verify library usage before implementation.
Sources: packages/opencode/src/session/prompt/gemini.txt:3, packages/opencode/src/session/prompt/codex.txt:1, packages/opencode/src/session/prompt/beast.txt:1, packages/opencode/src/command/template/review.txt:1
Agent Workflow
Agents typically follow a structured workflow for software engineering tasks. This process ensures that changes are well-understood, planned, and verified.
The diagram below shows the primary workflow for a software engineering task as defined in gemini.txt.
A key aspect of the agent's operation is the ability to use a variety of tools to interact with the file system and execute commands.
The following sequence diagram illustrates a typical interaction:
Agent Guidelines (AGENTS.md)
The system supports repository-specific instructions through AGENTS.md files. These files provide context-aware rules for agents, such as build commands, code style, and key dependencies. An agent must obey the instructions in any AGENTS.md file whose scope includes a file it is modifying. This allows projects to enforce their own conventions.
For example, the packages/desktop/AGENTS.md file specifies:
- Framework: SolidJS with TypeScript
- Validation: Use
bun run typecheckonly. - File Naming:
snake_casefor file names.
Sources: packages/desktop/AGENTS.md:3-16, packages/opencode/src/session/prompt/codex.txt:20-30
Furthermore, the system can be instructed to generate these AGENTS.md files by analyzing a codebase, ensuring that agent behavior aligns with project standards from the outset.
Sources: packages/opencode/src/command/template/initialize.txt:1-5
User Interfaces and Installation
OpenCode is accessible through multiple interfaces, primarily a native Terminal UI (TUI) and a web console.
Installation
The application can be installed via several package managers. The web console provides an interactive tabbed interface for installation commands.
| Method | Command |
|---|---|
curl | `curl -fsSL https://opencode.ai/install |
npm | npm i -g opencode-ai |
bun | bun add -g opencode-ai |
brew | brew install opencode |
paru | paru -S opencode |
Sources: packages/console/app/src/routes/index.tsx:84-142
Features Highlighted in UI
The web interface highlights several key architectural features of OpenCode:
- Native TUI: A responsive, themeable terminal UI.
- LSP Enabled: Automatically loads Language Server Protocols for the LLM.
- Multi-session: Supports running multiple agents in parallel.
- Shareable Links: Sessions can be shared via a URL.
- Multi-provider Support: Connects to Claude, and over 75+ other LLM providers.
- Privacy-First: Does not store user code or context data.
Sources: packages/console/app/src/routes/index.tsx:162-189, packages/console/app/src/routes/index.tsx:313-317
Development and Build Process
The project uses a modern JavaScript/TypeScript toolchain within a monorepo.
- Package Manager:
Bunis used for dependency installation and script execution. The required version is 1.3+. - Monorepo Management: The repository structure and task pipeline are likely managed by a tool like Turborepo, as evidenced by the presence of a
turbo.jsonfile, although its contents were not provided. - Core Development Script: The primary command to start the development server is
bun dev. - Debugging: The recommended way to debug is by running
bun run --inspect=<url> dev ...and attaching a debugger.
Sources: CONTRIBUTING.md:15-18, CONTRIBUTING.md:29-32