Learn Agents from OpenCode
中文 | English
A Chinese tutorial series on learning Coding Agent engineering architecture from the OpenCode source code.
OpenCode is an excellent open-source Coding Agent project with complete implementations of core concepts such as Agent, Tool, Permission, Skill, Session, and Provider. This repository documents the tutorials I compiled while studying the OpenCode source code, aiming to help readers understand the engineering design of a real-world Agent project rather than building a toy example from scratch.
These tutorials were primarily generated with assistance from OpenCode, Claude Code, and Codex, and have been manually reviewed. It is recommended to read them alongside the OpenCode source code.
Target Audience
- Users who have already used coding agents like Claude Code, Codex, or OpenCode and want to understand their internal workings
- Developers looking to implement their own controllable Coding Agent or other types of Agents
- Engineers with requirements for Agent permissions, tool calling, session management, prompt assembly, and other modules
Quick Start
This repo includes the OpenCode source code as a Git submodule so you can follow the tutorials while reading the actual implementation.
# Clone with submodule (recommended)
git clone --recursive https://github.com/yexia553/learn-agents-from-opencode.git
# Or if already cloned, initialize the submodule
git submodule update --init --recursive
# The OpenCode source will be available at ./opencode/
cd opencode && bun install
Note: Tutorials reference source files under
opencode/packages/opencode/src/....
Tutorial Index
For full navigation, see docs/README.md.
| No. | Tutorial | Description | | --- | --- | --- | | 00 | Learning Plan | Complete architecture learning roadmap | | 01 | System Prompt | Prompt construction, Provider adaptation, environment injection | | 02 | | Permission rules, request flow, BashArity | | 03 | | Built-in Agents, configuration system, permission integration | | 04 | | Tool definition, registration, execution flow | | 05 | | Tool Chaining, Explore Agent, SubAgent | | 06 | | Skill definition, discovery mechanism, tool integration | | 07 | | Session management, message flow, context compression | | 08 | | Multi-model adaptation, SDK initialization, cost calculation |