What this unit solves
.claude/rules/ splits CLAUDE.md’s always-on constraints into modules that activate only when matching files are touched. When CLAUDE.md grows bloated, cross-domain rules start interfering with each other, or certain rules only make sense for specific file extensions, rules are the right answer. This unit covers the division of labor between rules and CLAUDE.md, the path-scoped loading mechanism, naming and layered organization, and comparisons with equivalent mechanisms in other tools.Learning objectives
- State the difference between rules and
CLAUDE.md, and know when to split something into rules. - Use the
pathsfrontmatter to load a rule only when files matching a glob are touched. - Split constraints by domain or language into separate rule files, avoiding a single monolithic file.
- Decide whether a constraint belongs in
CLAUDE.md, a standalone rule file, a Skill, or a Hook, and state the deciding criterion. - Compare the equivalent mechanisms in OpenAI Codex, Google Antigravity, GitHub Copilot, and Cursor.
1. What rules are
Rules are Markdown files placed in.claude/rules/, loaded at the same level as CLAUDE.md (as of 2026-06, per the official memory documentation [1]). Key characteristics:
- Each
.mdfile is one module, named after its topic (testing.md,api-design.md,python-style.md). - Without a
pathsfrontmatter field, a rule is equivalent toCLAUDE.md: loaded in full at every session start. - With a
pathsfrontmatter field, the rule is only injected into context when Claude touches a file matching the glob. - Like
CLAUDE.md, rules belong to the “context layer,” not “enforced execution.” For constraints that must be guaranteed to run, use a Hook (see 04-6 Hooks).
.md files inside .claude/rules/ are read recursively; you can organize them into subdirectories (frontend/, backend/, ml/) for a layered hierarchy [1].
2. Division of labor with CLAUDE.md
CLAUDE.md (user-level, project-level, or local layer) holds constraints that apply across tasks, languages, and directories. Rules hold contextual constraints: ones that are only meaningful for a specific path or file extension.
There is only one criterion: read this rule aloud to an agent working on an unrelated task — would it cause interference or contradictions? Yes: split it into rules. No: keep it in CLAUDE.md.
How to splitStay in
CLAUDE.md:- “Responses always in Traditional Chinese, second person”
- “Never commit
.env” - “Build command:
make build; tests:make test” - “Run
make lintbefore every commit”
.claude/rules/python-style.md (path-scoped to **/*.py):- “Python requires type hints; public functions must not accept
Any” - “pytest fixtures go in
conftest.py; do not define cross-file fixtures inside individual test files” - “Public API functions must have a docstring (Google style)”
.claude/rules/api-design.md (path-scoped to src/api/**):- “Every endpoint must be registered in the OpenAPI spec”
- “Input validation is done at the handler boundary, not inside internal functions”
- “Error responses always use the
{code, message, details}structure”
CLAUDE.md.3. Path-scoped rules: the loading mechanism
.md files in .claude/rules/ can use the paths field in YAML frontmatter to restrict when they load. Claude Code only injects the rule into context when a file matching the glob is read during the session [1].
Basic syntax:
.gitignore style):
Multiple patterns can be listed together, or brace expansion used to match multiple extensions at once [1].
4. Naming and layered organization
Naming principle: use specific semantic names, not specific behavioral descriptions (notdo-not-touch.md, but api-migration-safety.md).
Common layering:
.claude
CLAUDE.md
rules
common
code-style.md · general language style
testing.md · testing principles
security.md · security prohibitions (path-scoped to src/)
frontend
react-style.md · React conventions
a11y.md · accessibility
backend
api-design.md · API design
db-migrations.md · database migrations
ml
experiment-tracking.md
SKILL.md is under 500 lines (a guideline for skills, but the same spirit applies to rules). Beyond that length, consider splitting further or upgrading to a Skill.
5. Sharing across projects: symlinks
.claude/rules/ supports symlinks, so you can maintain a shared rule set in ~/shared-claude-rules/ and link it into multiple projects [1]:
~/shared-*.md via @path inside .claude/rules/ is an alternative.
6. User-level rules
Rules placed in~/.claude/rules/ apply to all projects, with a load order after the user-level CLAUDE.md and before project-level rules [1]:
~/.claude/rules
preferences.md · personal coding preferences
workflows.md · personal common workflows
7. The boundary with Skills and Hooks
The three mechanisms have non-overlapping responsibilities:
Quick decision tree:
- Model needs to judge when to insert it: Skill
- User needs to trigger it explicitly: Command
- Must run after every occurrence of an action (regardless of model judgment): Hook
- Always resident, loaded every session, applies across tasks:
CLAUDE.md - Always resident, loaded only for specific paths: rules (path-scoped)
- Recording facts rather than instructions: auto memory
8. Tool comparison
Rule file mechanisms differ considerably across tools in path-scoped support and frontmatter details (as of 2026-06):Naming clarifications
- OpenAI Codex “rules” are executable policy files written in Starlark (allowlists/blocklists controlling agent behavior), not Markdown rule files. Semantically they are closer to Hooks than to the
CLAUDE.mdmodularization discussed here. - Cursor uses the
.mdcextension (.mddoes not work). The frontmatter fields aredescription,globs, andalwaysApply[3].alwaysApply: truemeans unconditional loading;globsset means auto-load only on matching files;descriptiononly means the model decides by semantic relevance; nothing set means manual@-mention only. - GitHub Copilot
.instructions.mdusesapplyToto restrict to a glob, and can addexcludeAgent: "code-review"or"cloud-agent"to exclude specific agents from loading it [4].
9. Hands-on exercises
1
Split one rule
Move the Python coding style section from
CLAUDE.md wholesale into .claude/rules/python-style.md, adding paths: ["**/*.py"].2
Verify loading
In the same session, run
/memory and confirm python-style.md appears with its path annotation.3
Test the no-load scenario
Ask Claude to edit a
*.md file (outside the **/*.py glob) and then ask about Python style rules. It should not reference python-style.md.4
Cross-project sharing
Create
~/.claude/rules/preferences.md; start sessions in two different projects and confirm it is loaded in both.10. Common pitfalls
Self-check
The bar for passing this unit
- Can you state the difference between rules and
CLAUDE.md, and explain when to split something into rules? - Can you write a correct
pathsfrontmatter and use glob patterns to scope the loading range? - Can you decide, given a requirement, whether a constraint belongs in
CLAUDE.md, a standalone rule file, a Skill, or a Hook? - Can you fill in the rule file mechanism for your primary tool on the five-tool comparison table?
Sources and further reading
Factual claims are grounded in official documentation; fast-changing items are annotated as of 2026-05.-
[1] Anthropic, “How Claude remembers your project,” code.claude.com, 2026. Available: https://code.claude.com/docs/en/memory (as of 2026-06; includes
.claude/rules/,pathsfrontmatter, symlinks, and auto memory chapters in full) -
[2] Anthropic, “Extend Claude with skills,” code.claude.com, 2026. Available: https://code.claude.com/docs/en/skills (as of 2026-06; includes the division of labor between Skills and
CLAUDE.md/ rules) -
[3] Cursor, “Rules,” cursor.com, 2026. Available: https://cursor.com/docs/context/rules (as of 2026-06;
.mdcand thedescription/globs/alwaysApplyfrontmatter fields) -
[4] GitHub Docs, “Adding custom instructions for GitHub Copilot,” docs.github.com, 2026. Available: https://docs.github.com/en/copilot/customizing-copilot/adding-custom-instructions-for-github-copilot (as of 2026-06;
.instructions.mdand theapplyToglob) - [5] Agentic AI Foundation (Linux Foundation), “AGENTS.md,” 2026. Available: https://agents.md/ (as of 2026-06; cross-tool standard for project rule files)
- Settings layer model: 02-1 Config Layer Model.
CLAUDE.mdand auto memory: 04-1 CLAUDE.md and Memory Files.- Skills (on-demand procedure invocation): 04-4 Skills.
- Hooks (deterministic guarantees): 04-6 Hooks.
- Context engineering and rule loading costs: 01-4 Context Engineering.