OpenCode: OpenCode AGENTS.md Configuration

Last updated: 2026-08-31

--- title: OpenCode Rules (AGENTS.md) description: Learn to write AGENTS.md rule files defining AI behavior constraints in your project order: 12 lang: en

AGENTS.md is OpenCode's project-level rules file that defines AI behavior constraints for the current project.

1. What You Will Learn


2. What is AGENTS.md

AGENTS.md is a Markdown file in the project root directory. AI automatically reads and follows its rules when working.

Aspect Details
Location Project root directory
Format Markdown
Scope Current project only
Auto-load AI reads automatically when starting

3. Creating AGENTS.md

TUI command: /init (auto-generates based on project structure)

Or manually create an AGENTS.md file in the project root.


4. Rule Writing

Basic Structure

MARKDOWN
# Project Rules

## Coding Standards
- Use TypeScript strict mode
- Follow ESLint configuration
- Maximum function length: 50 lines

## Testing Requirements
- All new functions must have unit tests
- Test coverage no less than 80%

## Commit Standards
- Use Conventional Commits format
- Each commit does one thing only

Rule Categories

Category Examples
Coding standards Code style, naming rules, type requirements
Architecture constraints Directory structure, module boundaries
Testing requirements Coverage, test types, test naming
Commit standards Commit format, branch naming, PR rules
Security rules No hardcoded keys, input validation
Business logic Domain rules, data processing

5. Best Practices


6. AGENTS.md vs opencode.json

Aspect AGENTS.md opencode.json
Format Markdown JSON
Audience AI + humans OpenCode program
Content Behavior rules, coding standards Config parameters, tool permissions
Effect Advisory Mandatory

They complement each other: AGENTS.md tells AI "what to do," opencode.json controls "what it can do."


❓ FAQ

Q Will AI always follow AGENTS.md rules?
A Not guaranteed. AGENTS.md is advisory—AI tries to follow but it's not enforced. For mandatory rules, use opencode.json permission config.
Q Does AGENTS.md in subdirectories work?
A No. Only the root directory AGENTS.md is read by AI.
Q Alice has 100-line AGENTS.md, Bob has 5 lines—who gets better results?
A Not necessarily more is better. 5 precise rules may be more effective than 100 vague ones. Quality over quantity.

📖 Summary


📝 Exercises

  1. Basic: Create an AGENTS.md with at least 5 coding standards.

  2. Intermediate: Define complete development flow rules (code→test→commit) and have OpenCode follow them.

  3. Advanced: Compare OpenCode output with and without AGENTS.md, quantifying rule impact on AI behavior.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏