OpenCode Rules (AGENTS.md)
In OpenCode,Rules are the core mechanism for controlling AI behavior。
We can use AGENTS.md files that provide custom instructions to OpenCode, letting it develop according to your project standards.
Tools determine what AI can do; AGENTS.md determines what AI should do.
AGENTS.md = defining team development standards for AI.
I. What is AGENTS.md
AGENTS.md is a rules file used to:
- Define project structure
- Constrain code style
- Standardize the development process
- Guide AI on how to execute tasks
These contents are added to the LLM's context.
Effect:
- AI understands your project better
- Generated code is more compliant with standards
- Reduce repeated revisions
II. Quick initialization
Run in OpenCode:
/init
This command will:
- Scan your project structure
- Analyze how code is organized
- Automatically generate AGENTS.md
Note:
- Commit AGENTS.md to Git
- As a unified team standard
III. Example structure
# Project Description
这是一个基于 TypeScript 的 monorepo 项目。
## Project Structure
- packages/ 核心模块
- infra/ 基础设施
- web/ 前端应用
## 代码规范
- 使用 TypeScript 严格模式
- 公共代码放在 packages/core
- 所有函数必须添加注释
## Development Conventions
- 使用 workspace 方式引用模块
- API 必须统一错误处理
Essence: writing team standards "for AI to read"
IV. Scope of rules
1. Project-level rules
Path:
./AGENTS.md
Function:
- Apply only to the current project
- Shared by the team
2. Global rules
Path:
~/.config/opencode/AGENTS.md
Function:
- Apply to all projects
- For personal use
Recommended uses:
- Personal coding habits
- Output style control
V. Claude Code compatibility
OpenCode is compatible with Claude Code's rules system:
- Project rules: CLAUDE.md
- Global rules: ~/.claude/CLAUDE.md
- Skills directory: ~/.claude/skills/
If you don't want to use it, you can disable it:
export OPENCODE_DISABLE_CLAUDE_CODE=1
VI. Rule loading priority (important)
OpenCode loads in the following order at startup:
- Search upward from the current directory for AGENTS.md
- If none → search for CLAUDE.md
- Global rules ~/.config/opencode/AGENTS.md
- Claude global rules ~/.claude/CLAUDE.md
Key points:
- AGENTS.md has the highest priority
- At the same level, only the first matching file is used
VII. Extended rules (instructions)
can be done during opencode.json to load additional rule files:
{
"instructions": [
"CONTRIBUTING.md",
"docs/guidelines.md",
".cursor/rules/*.md"
]
}
Fixed size, stores elements of the same type.
- Reuse existing documentation
- Avoid duplicate maintenance
Support remote rules
{
"instructions": [
"https://raw.githubusercontent.com/xxx/rules/main/style.md"
]
}
Explanation:
- Support loading from URLs
- Timeout: 5 seconds
VIII. Modular rules (advanced)
Recommended approach (using instructions)
{
"instructions": [
"docs/dev.md",
"test/rules.md",
"packages/*/AGENTS.md"
]
}
Suitable for:
- monorepo
- Large projects
Method 2: manually reference in AGENTS.md
# Rule loading instructions
当遇到 @xxx 文件时,请使用 read 工具加载。
- 不要一次性加载全部文件
- 按需加载
- 加载后必须遵守
Example:
TypeScript 规范:@docs/typescript.md
React 规范:@docs/react.md
API 规范:@docs/api.md
Implement "load rules on demand"
IX. Best practices (key points)
1. Rules should be specific
❌ Do not write:
代码要规范
✅ Should write:
所有函数必须添加 JSDoc 注释
2. Rules should be actionable
AI can only execute explicit rules, not abstract descriptions.
3. Don't write too long
Note:
- Write core rules in AGENTS.md
- Split detailed rules into instructions files
4. Unified team maintenance
- Include under Git management
- As part of the development standards