Why a rules file is needed
Every new session means repeating: “run the tests first”, “do not add new dependencies”, “run lint before committing”. Said too often, it gets missed, and AI forgets too.
The point of a rules file: read automatically before work starts, so the conventions are always there.
Layers and priority
A typical three-layer structure:
- Global: personal working habits that apply to every project
- Project root: this repository’s conventions
- Subdirectories: rules for particular modules
Reading usually goes from global to project, and from the root to the current directory; rules closer to the current directory appear later and have higher priority. Override files with the same name take precedence over ordinary files.
What goes in the global file
- Common commands: how to run tests, how to format
- Dependency preferences: which package manager, whether to confirm before adding dependencies
- Personal habits: comment language, commit message format
What goes in the project root
- Where the project structure and entry points are
- Checks that must be done before committing
- Code conventions: naming, error handling, logging
- What must not be touched: legacy interfaces, generated files
What goes in a subdirectory
- Module-specific commands (such as
make test-payments) - Special processes (such as who must be notified before changing the config)
- Exceptions when conflicting with the global rules
Example:
# services/payments/AGENTS.override.md
## Payment module rules
- Use make test-payments, not npm test
- Do not change field names in the clearing file
- Changes involving amount calculations must have human review
Writing suggestions
- Short: keep the main file to a few dozen lines; link to documents for details
- Checkable: make each rule verifiable with a command or check item where possible
- State exceptions: explain when it is acceptable to break the rule
- Do not pile on formatting rules: leave formatting to automated tools
How to confirm it takes effect
Have AI answer “which instruction sources have you read, and what are they”. Check:
- Whether global, project and subdirectory files were all read
- Whether subdirectory rules override the upper layers
- Whether anything was truncated or ignored
Common problems
- The file is empty → it is ignored outright
- An override file exists above → ordinary files are overridden
- Changes do not take effect → restart the session or the tool
- Too long and truncated → split into subdirectories, or adjust the size limit
Checklist
- The global file holds general habits
- The project root holds repository conventions and check commands
- Special modules have their own rules files
- Every rule is as verifiable as possible
- Have had AI restate which instruction sources it read
- Rule updates are shared with the team
Reference: key points adapted from OpenAI’s official documentation “Custom instructions with AGENTS.md”, rewritten for general tools and practical use.
Questions
Are AGENTS.md and CLAUDE.md the same thing?
They serve the same purpose: both are instruction files for AI coding tools. Different tools use different file names, and many tools also support custom names or reading several files; the content and layering approach are the same.
Which layer should a rule go in?
Global holds personal habits (such as test commands and dependency preferences), the project root holds repository conventions (structure, pre-commit checks), and subdirectories hold special rules (the payment module uses its own tests). Rules closer to the current directory have higher priority.
Why do the rules I wrote not take effect?
Common causes: the file is empty and ignored, an override file above it overrides it, the tool was not restarted to reread the files, or the rules are too long and get truncated. First have AI restate which instruction sources it has read.