Claude Code Rules Directory: Modular Instructions That Scale
The .claude/rules/ directory lets you organize instructions into multiple files with path-specific targeting. Learn how to structure modular rules that activate only where they matter.
Problem: Your CLAUDE.md file has grown unwieldy. React patterns mixed with API guidelines mixed with testing rules. Everything loads every session, even when you're only working on database migrations.
Quick Win: Create your first modular rule:
That rule now loads automatically alongside your CLAUDE.md, keeping concerns separated.
What Is the Rules Directory?
The .claude/rules/ directory is a modular alternative to monolithic CLAUDE.md files. Instead of cramming everything into one file, you organize instructions into multiple markdown files that Claude loads as project memory.
Critical detail from Anthropic: Rules files load with the same high priority as CLAUDE.md. This matters because Claude's context window has a priority hierarchy - not all tokens are weighted equally.
Every .md file in .claude/rules/ automatically becomes part of your project context. No configuration needed.
This structure gives you separation of concerns at the instruction level. Update your security rules without touching your styling guidelines.
Path-Specific Rules: The Power Feature
Here's where rules get interesting. You can target rules to specific file patterns using YAML frontmatter:
This rule only activates when Claude works on files matching src/api/**/*.ts. Your API guidelines stay out of the way when you're editing React components.
Multiple Path Patterns
Target multiple patterns in a single rule:
Common Path Patterns
| Pattern | Matches |
|---|---|
src/api/**/*.ts | All TypeScript files in src/api and subdirectories |
*.test.ts | All test files in any directory |
src/components/*.tsx | Only direct children of components (not nested) |
**/*.css | All CSS files anywhere in the project |
Why Priority Matters: The Monolithic CLAUDE.md Problem
Claude's context window isn't flat. Different sources of information receive different priority levels in how the model weighs them during generation. Anthropic confirms that CLAUDE.md and rules files receive high priority - Claude treats these instructions as authoritative.
This created a problem with the old approach: stuffing everything into one massive CLAUDE.md meant all of that content received high priority. Your React patterns competed for attention with your API guidelines, even when you were working on database migrations.
High priority everywhere = priority nowhere.
When everything is marked important, Claude struggles to determine what's actually relevant to the current task. The result: instructions get ignored, context becomes noisy, and Claude's behavior becomes unpredictable.
The Context Priority Hierarchy
Understanding how Claude weighs different context sources:
| Source | Priority Level | Implication |
|---|---|---|
| CLAUDE.md | High | Treated as authoritative instructions |
| Rules Directory | High | Same weight as CLAUDE.md |
| Skills | Medium (on-demand) | Loaded only when triggered |
| Conversation history | Variable | Decays over long sessions |
| File contents (Read tool) | Standard | Normal context, no special weight |
The rules directory solves the monolithic problem by letting you distribute high-priority instructions across targeted files. Your API rules still get high priority - but only when you're working on API files.
Path Targeting = Priority Scoping
When a rule has paths frontmatter, it only loads (and receives high priority) when Claude is working on matching files:
This is the key insight: you're not just organizing files, you're scoping when instructions receive elevated attention.
Rules vs CLAUDE.md vs Skills
When do you use each?
| Feature | Priority | Best For | Loads When |
|---|---|---|---|
| CLAUDE.md | High | Universal operational workflows | Every session |
| Rules Directory | High | Domain-specific instructions | Every session (filtered by path) |
| Skills | Medium | Reusable cross-project expertise | On-demand when triggered |
Use CLAUDE.md for what applies everywhere: routing logic, quality standards, coordination protocols. Keep it lean - everything here competes for high-priority attention.
Use rules for what applies to specific areas: API patterns for API files, test requirements for test files. Path targeting ensures high priority only when relevant.
Use skills for what applies across projects: deployment procedures, code review checklists, brand guidelines. Lower priority until explicitly triggered.
Practical Examples
Security Rules for Sensitive Directories
Test File Standards
Database Migration Rules
Migration from Monolithic CLAUDE.md
If your CLAUDE.md has grown large, you're likely experiencing the priority saturation problem: too much high-priority content competing for attention. Extract domain sections into path-targeted rules:
Before (single 400-line CLAUDE.md):
After (lean CLAUDE.md + modular rules):
Your CLAUDE.md stays focused on universal behavior. Domain knowledge lives in targeted rules that only receive high priority when relevant.
The result: cleaner priority distribution. Your core operational instructions always get attention. Domain-specific rules get attention only when Claude is working in their target areas.
Best Practices
Keep rules focused: One concern per file. Security rules separate from styling rules.
Use descriptive filenames: api-validation.md beats rules1.md.
Leverage path targeting: Rules without paths load everywhere. Add paths to reduce noise.
Version control everything: Rules are code. Review changes, track history, roll back mistakes.
Document rule purpose: Start each file with a brief comment explaining when it applies.
Next Steps
- Audit your CLAUDE.md: Identify sections that apply only to specific file types
- Extract one rule: Move your most domain-specific section to
.claude/rules/ - Add path targeting: Make that rule activate only where it matters
- Iterate: As you work, notice when Claude receives irrelevant context and extract more rules
For the complete memory architecture and why monolithic files cause problems, see CLAUDE.md Mastery. To understand how priority fits into broader context strategy, explore context management and memory optimization.
The goal: Claude receives exactly the high-priority instructions it needs for the files it's touching. No more, no less. Priority where it matters, silence everywhere else.
Last updated on