Claude Code Rules Directory: Modular Instructions That Scale
Organize Claude Code instructions into modular files with path-specific targeting. Rules activate only where they matter, saving context tokens.
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.
User-Level Rules: Personal Defaults Across All Projects
Beyond project-specific rules, you can create personal rules that apply to every project you work on. Place them in ~/.claude/rules/:
User-level rules load before project rules, giving project rules higher priority. This means your personal defaults apply everywhere, but any project can override them.
This is ideal for preferences that follow you regardless of project: indentation style, commit message format, preferred testing patterns, or anything that reflects how you personally work rather than how a specific project operates.
Brace Expansion in Path Patterns
Path patterns support brace expansion for matching multiple extensions or directories in a single pattern:
{ts,tsx} matches both .ts and .tsx files. {src,lib} matches both the src/ and lib/ directories. This keeps your frontmatter compact when a rule applies to related file types or directories.
Symlinks: Sharing Rules Across Projects
The .claude/rules/ directory supports symlinks, allowing you to maintain a single source of rules shared across multiple projects:
Symlinks are resolved and their contents load normally. Circular symlinks are detected and handled gracefully, so you don't need to worry about infinite loops.
This pattern works well for organizations where teams share common coding standards. Maintain one canonical rules repository and symlink it into each project.
Recursive Subdirectory Discovery
Rules can be organized into subdirectories for better structure. All .md files are discovered recursively:
Every .md file in the tree loads automatically. Use subdirectories to keep related rules grouped without sacrificing discoverability.
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