Opencode Agents Md
In OpenCode, **rules are the core mechanism for controlling AI behavior**.
We can use the `AGENTS.md` file to provide custom instructions to OpenCode, enabling it to develop according to your project specifications.
Tools determine what AI can do; AGENTS.md determines how AI should do it.
**AGENTS.md = establishing team development guidelines for AI.**
* * *
## 1. What is AGENTS.md
`AGENTS.md` is a rules file used to:
* Define project structure
* Constrain code style
* Standardize development workflow
* Guide AI on how to execute tasks
This content is added to the LLM context.
**Effects:**
* AI better understands your project
* Generated code better conforms to specifications
* Reduces repetitive revisions
* * *
## 2. Quick Initialization
Run in OpenCode:
/init
This command will:
* Scan your project structure
* Analyze code organization patterns
* Automatically generate AGENTS.md
**Recommendations:**
* Commit AGENTS.md to Git
* Use as unified team standards
* * *
## 3. Example Structure
# Project DescriptionThis is a TypeScript-based monorepo project.## Project Structure- packages/ Core modules- infra/ Infrastructure- web/ Frontend application## Code Standards- Use TypeScript strict mode- Common code goes in packages/core - All functions must include comments## Development Conventions- Use workspace for module references- APIs must have unified error handling
Essence: "writing team standards for AI to read"
* * *
## 4. Rule Scope
### 1. Project-level Rules
Path:
./AGENTS.md
Effect:
* Only applies to current project
* Shared across team
### 2. Global Rules
Path:
~/.config/opencode/AGENTS.md
Effect:
* Applies to all projects
* Personal use
**Recommended Uses:**
* Personal coding habits
* Output style control
* * *
## 5. Claude Code Compatibility
OpenCode is compatible with Claude Code's rule system:
* Project rules: CLAUDE.md
* Global rules: ~/.claude/CLAUDE.md
* Skills directory: ~/.claude/skills/
To disable:
export OPENCODE_DISABLE_CLAUDE_CODE=1
* * *
## 6. Rule Loading Priority (Important)
When OpenCode starts, it loads in this order:
1. Search upward from current directory for AGENTS.md
2. If not found β search for CLAUDE.md
3. Global rules ~/.config/opencode/AGENTS.md
4. Claude global rules ~/.claude/CLAUDE.md
**Key Points:**
* AGENTS.md has highest priority
* Only the first matching file at the same level is used
* * *
## 7. Extended Rules (instructions)
Additional rule files can be loaded in `opencode.json`:
{ "instructions": [ "CONTRIBUTING.md", "docs/guidelines.md", ".cursor/rules/*.md" ]}
Benefits:
* Reuse existing documentation
* Avoid duplicate maintenance
### Support for Remote Rules
{ "instructions": [ "https://raw.githubusercontent.com/xxx/rules/main/style.md" ]}
**Notes:**
* Supports URL loading
* Timeout: 5 seconds
* * *
## 8. Modular Rules (Advanced)
### Recommended Approach (using instructions)
{ "instructions": [ "docs/dev.md", "test/rules.md", "packages/*/AGENTS.md" ]}
Suitable for:
* monorepo
* Large projects
### Approach 2: Manual Reference in AGENTS.md
# Rule Loading InstructionsWhen encountering @xxx files, use the read tool to load them.- Do not load all files at once- Load on demand- Must comply after loading
Examples:
TypeScript standards: @docs/typescript.md React standards: @docs/react.md API standards: @docs/api.md
Achieves "on-demand rule loading"
* * *
## 9. Best Practices (Key Points)
### 1. Rules Should Be Specific
β Don't write:
Code should be clean
β
Should write:
All functions must include JSDoc comments
### 2. Rules Should Be Actionable
AI can only execute explicit rules, not abstract descriptions.
### 3. Don't Write Too Much
Recommendations:
* Write core rules in AGENTS.md
* Split detailed rules into instructions files
### 4. Maintain Uniformly as a Team
* Include in Git management
* Make part of development standards
YouTip