Path-Specific Rules (.claude/rules/)

Core

Apply path-specific rules for conditional convention loading · Difficulty 2/5

0%
rulesconfigurationglob-patternsconditional-loading

The .claude/rules/ directory allows organizing project instructions into focused, topic-specific files that can be conditionally loaded based on file paths.

Structure

.claude/rules/
  testing.md         # Testing conventions
  api-conventions.md # API patterns
  react-style.md     # React component patterns

Conditional Loading with Globs

Add YAML frontmatter to conditionally apply rules:

---
paths: ["**/*.test.tsx", "**/*.spec.ts"]
---
All tests should follow the AAA pattern (Arrange, Act, Assert)...

Rules load only when editing files that match the glob patterns, reducing irrelevant context and token usage.

Glob Patterns vs Directory-Level CLAUDE.md

Directory-level CLAUDE.md files apply to everything in that directory. But conventions like "test file standards" apply to test files spread across the entire codebase, not confined to one directory.

Glob-pattern rules solve this by matching file type regardless of directory location:

  • **/*.test.tsx -- all test files anywhere
  • terraform/**/* -- all Terraform files
  • src/api/**/*.ts -- API source files
  • When to Use Each

    ConfigurationBest For

    |---|---|

    Directory CLAUDE.mdConventions specific to one directory subtree
    Path-specific rulesConventions for file types spread across the codebase
    Root CLAUDE.mdUniversal conventions that always apply

    Key Takeaways

    • Use .claude/rules/ with glob patterns for file-type-specific conventions
    • Rules load only when editing matching files, reducing token usage
    • Glob patterns beat directory CLAUDE.md when conventions span multiple directories
    • Use paths frontmatter field for conditional rule activation

    Test Yourself1 of 2

    Your team's CLAUDE.md file has grown to over 500 lines, mixing various conventions and procedures. Developers find it difficult to locate and update relevant sections. What approach does Claude Code support for organizing instructions?