Loading...
Loading...
Guide for Writing and Optimizing AGENTS.md, following the principle of progressive disclosure. This skill must be loaded when users create, modify, or refactor AGENTS.md, discuss AI agent instruction structures, rule placement, or mention terms like "progressive disclosure", "modularity", "AGENTS.md best practices". Even if users simply say "help me write an AGENTS.md", "optimize this configuration file", or "split the rules", this skill should be used.
npx skill4agent add cnife/skills optimize-agents-mdThis rule → Global effect? → Yes → Root AGENTS.md
→ Specific module? → Yes → Subdirectory AGENTS.md or Skill
→ Complex workflow? → Yes → Skill
→ Uncertain? → Consider if it's really necessary| Pitfall | Problem | Correct Approach |
|---|---|---|
| Stuffing all rules into one file | Context waste, Agent confusion | Split by scope |
| Tutorial-style content (how to use X) | Irrelevant content loaded in every session | Place in Skill or documentation |
| Overly specific command examples | Rule bloat, difficult to maintain | Only write core principles |
| Conflicts with other rules | Inconsistent Agent behavior | Merge or delete conflicting rules |
| Level | Content | Loading Timing |
|---|---|---|
| 1. Metadata | name + description | Always visible |
| 2. SKILL.md body | Core instructions | Loaded when the Agent judges it relevant |
| 3. References | Detailed documentation | Loaded only when needed |
docs/~/.config/opencode/AGENTS.md~/.config/opencode/AGENTS.md # User global (shared by all projects)
↓ Inherit/Override
project/AGENTS.md # Project root (project-level rules)
↓ Inherit/Override
project/src/python/AGENTS.md # Module-level (specific module rules)| Level | Should Contain | Should Not Contain |
|---|---|---|
| User global | Cross-project general rules (language preferences, Git specifications) | Project-specific rules, module rules |
| Project root | Project-specific rules (project architecture, team agreements) | Rules already defined in user global, module rules |
| Module directory | Module-specific rules (tech stack specifications, file naming) | Rules already defined in upper levels |
# ❌ Wrong: Project AGENTS.md duplicates user global rules
# Language
Always respond in Chinese. # Already defined in ~/.config/opencode/AGENTS.md, duplicate!
# Git
Short commit messages, no prefix # Already defined in ~/.config/opencode/AGENTS.md, duplicate!
# ✅ Correct: Project AGENTS.md only contains project-specific rules
# Project Structure
src/ is the source code directory, tests/ is the test directory.
# Team Agreement
PRs must be reviewed by at least one person.~/.config/opencode/
└── AGENTS.md # User global rules (shared by all projects)
project/
├── AGENTS.md # Project global rules (< 50 lines)
├── docs/
│ ├── AGENTS.md # Project-level documentation rules, architecture descriptions
│ ├── architecture.md # Architecture documentation
│ └── api-guide.md # API usage guide
├── src/
│ ├── python/
│ │ └── AGENTS.md # Python module-specific rules
│ └── frontend/
│ └── AGENTS.md # Frontend module-specific rules
└── .opencode/
└── skills/
└── deploy/SKILL.md # Deployment workflow (complex task)| Content Type | Placement | Example |
|---|---|---|
| User global constraints | | Language preferences, Git specifications, core principles |
| Project global constraints | Project root AGENTS.md | Project architecture, team agreements, entry instructions |
| Module/folder rules | AGENTS.md in that folder | Python specifications, frontend specifications, API module rules |
| Project-level documentation instructions | | Architecture descriptions, documentation writing specifications, project guides |
| Complex workflows | Skill | Deployment processes, PR creation processes |
| Rule Type | Placement | Example |
|---|---|---|
| User global rules | | Language preferences, Git specifications, core principles |
| Project global rules | Project root AGENTS.md | Project architecture, team agreements, entry instructions |
| Module rules | Subdirectory AGENTS.md | Python specifications → |
| Project documentation rules | | Documentation writing specifications, architecture descriptions |
| Task rules | Skill | Complex workflows, specific task guides |
Is this rule needed in every session?
├── Yes → Is it needed for all projects?
│ ├── Yes → Place in ~/.config/opencode/AGENTS.md (user global)
│ └── No → Place in project root AGENTS.md
└── No → Is it for a specific module/folder?
├── Yes → Place in AGENTS.md under that folder
└── No → Is it project-level documentation/architecture description?
├── Yes → Place in docs/AGENTS.md
└── No → Is it a complex workflow?
├── Yes → Create a Skill
└── No → Consider if this rule is really necessary1. Confirm which global rules are needed (language, principles, Git)
2. Determine if module-level rules need to be placed separately
3. Write a concise root AGENTS.md
4. If needed, create subdirectory AGENTS.md, docs/AGENTS.md or Skill1. Read the existing AGENTS.md
2. List all rule modules
3. Mark the scope of each module (global/module/project documentation/task)1. Create subdirectory AGENTS.md, docs/AGENTS.md or Skill files
2. Migrate relevant rules (maintain format and hierarchy)
3. Update the root AGENTS.md, removing split content
4. Add necessary reference notes (optional)1. Check if the root AGENTS.md is concise
2. Confirm that subdirectory files have complete content
3. Verify no rules are lost or duplicated
4. Confirm docs/AGENTS.md contains project-level documentation rules (if applicable)| Scenario | Recommendation | Reason |
|---|---|---|
| Cross-project personal preferences (language, Git) | | Shared by all projects |
| Project constraints (architecture, team agreements) | Project AGENTS.md | Effective at project level |
| Tech stack specifications (Python, frontend) | Subdirectory AGENTS.md or Skill | Loaded on demand |
| Project documentation/architecture descriptions | | Loaded when needed |
| Complex workflows (deployment, PR) | Skill | Progressive disclosure + reusable |
| Team agreements (naming, formatting) | Project AGENTS.md | Global constraints |
# Language
Always respond in Chinese.
# Core Principles
- Prioritize simple, maintainable solutions
- Avoid over-engineering
# Git
- Short commit messages, no prefix
# Module Rules
Python projects → Refer to src/python/AGENTS.md
Frontend projects → Refer to src/frontend/AGENTS.md
# Documentation
Project architecture → Refer to docs/AGENTS.md# Python Project Specifications
## Tools
Use uv for dependency management, ruff for formatting.
## Principles
- Fast-fail: Only use try-except at the outer layer
- Prohibit hard-coded credentials# Project Documentation Specifications
## Architecture Description
This project uses a three-tier architecture, see architecture.md for details.
## API Documentation
- REST API specifications → api-guide.md
- GraphQL Schema → schema.graphql| Mistake | Consequence | Correction |
|---|---|---|
| Overly long root AGENTS.md | Context waste | Split into subdirectories, docs/ or Skill |
| Duplicate rule definitions | Agent confusion | Each rule appears only once |
| Duplicate content across levels | Context waste, rule conflicts | Define each rule only at the most appropriate level |
| Overly fine-grained splitting | Maintenance burden | Merge related rules |
| Forgetting to delete original content | Rule conflicts | Must delete original content after splitting |
| Module rules placed in root directory | Irrelevant rules loaded | Move to corresponding subdirectory |
| Project documentation rules placed in root directory | Context bloat | Move to docs/AGENTS.md |
| Project rules placed in user global | Affects other projects | Move to project AGENTS.md |