What you will be able to do
- Explain why a rules file with paths frontmatter costs no context until Claude works on a matching file
- Write paths frontmatter that scopes a rule to a directory tree, a file type anywhere, or several patterns at once
- Tell apart *, ** and brace patterns, and predict which files each one matches
- Choose between a glob-scoped rule and a subdirectory CLAUDE.md for a given convention
Key concept
Path-scoped rule — A file in .claude/rules/ whose YAML frontmatter has a paths list of glob patterns. It stays out of context until Claude works with a file that matches one of those patterns, and then it loads automatically.
1.Why some conventions should load only on matching files
Every instruction that loads at startup costs context in every session, whether or not the task needs it. A rule about OpenAPI comments on API handlers does nothing when Claude is fixing a CSS bug, but if it sits in the project CLAUDE.md it still takes up space and competes for attention. Size has a second cost besides tokens: longer instruction files reduce how closely Claude follows them. That is why the guidance for a CLAUDE.md is to keep it under about 200 lines and move anything that applies to only part of the codebase somewhere else.
That somewhere is a path-scoped rule. Teams already split instructions into .claude/rules/ topic files. With no frontmatter, such a file loads every session just like CLAUDE.md. Add a paths field with glob patterns and the same file becomes conditional: it stays out of context until Claude works with a file that matches, and then it loads automatically next to that file. The text of the rule doesn't change, only when it loads.
Splitting a long CLAUDE.md with @path imports does not get you this saving. Imported files load at launch along with the file that imports them, so the long file gets tidier but costs just as much context. Only a paths scope delays loading.
| Mechanism | Loads | Best for |
|---|---|---|
| CLAUDE.md | Every session | Core conventions and build commands |
| .claude/rules/ without paths | Every session | Topic-organised conventions that always apply |
| .claude/rules/ with paths | When Claude works with a matching file | Language-specific or directory-specific guidelines |
| Skill | On demand, when invoked or relevant | Reference material, repeatable workflows |
Conditional loading also changes what happens after /compact, the command that replaces the conversation with a structured summary, so the instructions Claude had loaded are no longer sitting in the conversation itself. The project-root CLAUDE.md and unscoped rules are re-injected from disk straight away. Rules with paths: frontmatter are not. Claude Code reloads them on demand, the next time Claude touches a matching file. A scoped rule never sits in a compacted context just because it once applied.
Checkpoint 1 of 7· Check yourself
A project's CLAUDE.md has grown to about 400 lines. Most of it is OpenAPI and validation conventions that only matter for the API handlers under src/api/. The team wants sessions spent on frontend work to stop paying the context cost of those conventions. What should they do?
Only a paths scope keeps a rule out of context until Claude works with a matching file. @path imports and unscoped rules files both load at launch, so they make the file tidier but cost just as much context. Headers help Claude follow the file but do not change when it loads.
“Move instructions that matter for only part of the codebase into path-scoped rules, which load only when Claude works with matching files.”Source: code.claude.com
2.Writing the paths frontmatter
A rule's frontmatter has one field, paths. It is optional: leave it out and the rule applies everywhere. Its value is a set of glob patterns, written either as a YAML list or as a comma-separated string. The frontmatter sits between --- lines at the top of the markdown file, and the rule's instructions follow below it.
---
paths:
- "src/api/**/*.ts"
---
# API Development Rules
- All API endpoints must include input validation
- Use the standard error response format
- Include OpenAPI documentation commentsAn infrastructure team uses the same pattern. A file at .claude/rules/terraform.md whose frontmatter is paths: ["terraform/**/*"] (or the same pattern as a one-item YAML list) holds the formatting and tagging conventions. Claude gets them while it edits anything in the Terraform tree and never sees them during application work.
The key has to be spelled paths. Claude Code does not read include, files, globs or any similar name in rule frontmatter. A file that puts its patterns under one of those keys has no paths field, so it is not path-scoped at all.
Checkpoint 2 of 7· Fill the gap
This rule file should load only when Claude works with TypeScript files under src/api/. Which frontmatter key goes in the blank?
---
? :
- "src/api/**/*.ts"
---
# API Development Rules
- All API endpoints must include input validation
- Use the standard error response format
- Include OpenAPI documentation commentspaths is the only frontmatter field a rules file supports. If the patterns sit under include, globs or files, the file has no paths field, so it isn't path-scoped and loads every session.
Source: code.claude.com3.Reading glob patterns: *, ** and braces
Almost every scoping mistake comes down to three facts about globs. First, * matches within a single directory level, while ** matches any number of nested directories. src/components/*.tsx gets only the components directly in that folder. src/**/* gets every file under src/ at any depth. So terraform/* catches terraform/main.tf but misses terraform/modules/vpc/main.tf, and only terraform/**/* covers the whole tree.
Second, patterns are matched from the project root. *.md matches markdown files in the root, not every markdown file in the repo. A pattern that leaves off a leading directory points at a different place: api/**/*.ts means a top-level api/ directory, not src/api/. Starting a pattern with **/ removes that anchor. **/*.ts matches TypeScript files in any directory. The same move lets **/*.test.tsx reach every component test, whether it sits next to its component, in a __tests__ folder, or in a root-level tests/ directory. Note that **/terraform/* matches a terraform directory at any depth but still only one level inside it.
Third, braces list alternatives inside one pattern, so src/**/*.{ts,tsx} covers both extensions under src/. Several entries in paths add together, and the rule loads when a file matches any one of them.
---
paths:
- "src/**/*.{ts,tsx}"
- "lib/**/*.ts"
- "tests/**/*.test.ts"
---| Pattern | Matches |
|---|---|
| **/*.ts | All TypeScript files in any directory |
| src/**/* | All files under src/ directory |
| *.md | Markdown files in the project root |
| src/components/*.tsx | React components in a specific directory |
| src/**/*.{ts,tsx} | .ts and .tsx files at any depth under src/ |
| src/api/**/*.ts | TypeScript files at any depth under src/api/, and nowhere else in src/ |
A scope can also be too broad. src/**/*.ts covers every API handler, but it also loads the API rule for src/utils/ and src/components/, which is exactly the irrelevant context path scoping is supposed to avoid. A good pattern starts at the smallest directory that holds every target file and uses ** below it.
Those patterns match TypeScript files everywhere, including scripts, tests and anything outside src/ and lib/, so the rule loads far more often than it should. "src/**/*.{ts,tsx}" and "lib/**/*.ts" also use two entries but match only the intended files.
Checkpoint 3 of 7· Match them up
Match each paths pattern to the files it actually brings into scope
Tap a term, then the definition that fits it.
A single * stays within one directory level, while reaches any depth. Patterns are matched from the project root unless they start with /, and braces list alternative extensions inside one pattern.
“**/*.ts | All TypeScript files in any directory src/**/* | All files under src/ directory *.md | Markdown files in the project root”Source: code.claude.com
Checkpoint 4 of 7· Exam question
An API team wants a rule requiring input validation and OpenAPI comments to apply only to TypeScript files inside src/api/, including deeply nested route handler subfolders, but not to TypeScript files elsewhere in the repo such as src/utils/ or src/components/. Which paths frontmatter correctly scopes the rule to this requirement?
Correct answer: A — paths: - "src/api/**/*.ts"
- A. Correct. This pattern restricts matches to TypeScript files under src/api/, and the double-star ensures nested route handler subfolders are included.
- B. This pattern matches every TypeScript file anywhere under src/, including src/utils/ and src/components/, which the team explicitly wants excluded from the rule's scope.
- C. The single-star pattern only matches files directly inside src/api/, so it misses TypeScript files in nested route handler subfolders like src/api/users/handlers.ts.
- D. This pattern omits the src/ prefix, so it would not match files under the actual src/api/ path and would fail to scope the rule as intended.
Checkpoint 5 of 7· Exam question
In a Claude Code rules file, the same conventions must apply to .ts and .tsx files at any depth under src/, plus .ts files at any depth under lib/ (including nested subdirectories), using as few paths glob pattern entries as possible. Which paths frontmatter accomplishes this most efficiently?
Correct answer: A — paths:
- "src/**/*.{ts,tsx}"
- "lib/**/*.ts"
- A. Correct. Claude Code path-specific rules use YAML frontmatter with a
pathsfield containing glob patterns. The brace syntax*.{ts,tsx}matches either extension in a single entry, while**matches files recursively. This covers.tsand.tsxat any depth undersrc/and.tsat any depth underlib/in only two entries, making it the most compact of the options shown. - B. Incorrect. This pattern set is functionally correct and covers the requested files, but it uses three separate entries instead of two. The question asks for the most efficient approach, and Anthropic's documented brace syntax
*.{ts,tsx}expresses the samesrc/coverage in a single entry. - C. Incorrect. This applies the conventions to every
.tsand.tsxfile anywhere in the project, including.tsxfiles underlib/and files outsidesrc/andlib/. That is broader than the requested scope and can load irrelevant rules into context, reducing token efficiency. - D. Incorrect. Without
**,src/*.{ts,tsx}andlib/*.tsonly match files directly in the top-levelsrc/orlib/directories and do not recurse into nested subdirectories. The requirement states the rules must apply at any depth undersrc/and underlib/, so these patterns do not satisfy the scenario.
Checkpoint 6 of 7· Exam question
An infrastructure team wants Claude Code to load Terraform-specific formatting and tagging conventions only when Claude edits files under the terraform/ directory, at any subfolder depth. Which YAML frontmatter for a new .claude/rules/terraform.md file correctly scopes the rule to this requirement?
Correct answer: A — --- paths: - "terraform/**/*" ---
- A. Correct. The paths field takes a YAML list, and the double-star glob terraform/**/* matches files under terraform/ at any depth, including nested subfolders.
- B. A single star only matches files directly inside terraform/ and would miss files in nested subfolders such as terraform/modules/network/main.tf.
- C. The frontmatter field recognized for path scoping is paths, not include; a rule using include would not be treated as path-scoped.
- D. This pattern matches only files sitting directly inside a folder named terraform, at any location in the repository. It misses files in nested subfolders such as terraform/modules/network/main.tf, so the rule would not load at every depth under terraform/.
Sources1
4.Glob rules vs a subdirectory CLAUDE.md
A subdirectory CLAUDE.md is also conditional. It is not read at session start. It loads when Claude reads files in its directory. So lazy loading is not what separates the two. What separates them is how the scope is defined. A nested CLAUDE.md is scoped by where it sits: it covers the directory it lives in. A path-scoped rule is scoped by a pattern, so a single file in the central .claude/rules/ at the repo root can cover files spread across the whole tree.
| Approach | File location | Loads when | Use when |
|---|---|---|---|
| Per-directory CLAUDE.md | Inside the directory, alongside its code | At launch when started from that directory, or on demand | Directory owners maintain their own conventions; instructions are versioned with the code |
| Path-scoped rule in .claude/rules/ | Central .claude/ at the repo root | When Claude works with a file matching the rule’s paths: glob | You want all conventions in one place, or the same rule applies to many scattered paths |
Test conventions show the difference clearly. Component tests sit in src/components/__tests__/, in src/pages/tests/, next to the components they test, and in a root tests/ folder. Covering them with directory files means a copy of the test rules in a CLAUDE.md in each of those folders. Every new test folder then needs another copy, and the copies drift apart over time. Conflicting instructions are a real hazard, because when two contradict each other, Claude may pick one arbitrarily. A single rule with **/*.test.tsx follows the file type wherever it lives.
The subdirectory file is the better choice when the convention really belongs to one place. A rule like "write database queries with the Knex query builder" for one service, or a package's own commit prefix in a monorepo, is a good fit for a CLAUDE.md inside that package. Its owners edit it in the same pull requests as the code, and it moves with the directory. So: if the convention follows a file type, use a glob rule. If it follows a location, use a directory CLAUDE.md.
Checkpoint 7 of 7· Check yourself
Your team's React test files live in src/components/__tests__/, in src/pages/tests/, next to the components they test, and in a root tests/ folder. You want one set of testing conventions to load only when Claude is working on these files. Which setup fits best?
These conventions follow a file type, not a location. One path-scoped rule with **/*.test.tsx matches every test file wherever it lives. Separate CLAUDE.md copies drift apart, tests/**/*.test.tsx only matches the root tests/ folder, and path-scoped rules load lazily too, so src/CLAUDE.md has no advantage there.
“You want all conventions in one place, or the same rule applies to many scattered paths”Source: code.claude.com
Sources
(6)
Every claim above is drawn from one of these pages, quoted as it was written on the date shown.
- 1.https://code.claude.com/docs/en/memoryOfficial docs
“Longer files consume more context and reduce adherence.”
↩︎ Why some conventions should load only on matching files“Move instructions that matter for only part of the codebase into path-scoped rules, which load only when Claude works with matching files.”
↩︎ Why some conventions should load only on matching files“Imports help you organize a long file but don’t reduce its context cost, because imported files also load at launch.”
↩︎ Why some conventions should load only on matching files“Glob patterns that scope the rule to matching files. Accepts a YAML list or a comma-separated string”
↩︎ Writing the paths frontmatter“**/*.ts | All TypeScript files in any directory”
↩︎ Reading glob patterns: *, ** and braces“*.md | Markdown files in the project root”
↩︎ Reading glob patterns: *, ** and braces“src/components/*.tsx | React components in a specific directory”
↩︎ Reading glob patterns: *, ** and braces“if two instructions contradict each other, Claude may pick one arbitrarily.”
↩︎ Glob rules vs a subdirectory CLAUDE.md“Move instructions that matter for only part of the codebase into path-scoped rules, which load only when Claude works with matching files.”
↩︎ Key concept“paths | No | Glob patterns that scope the rule to matching files.”
↩︎ Exam trap 1“src/**/* | All files under src/ directory”
↩︎ Exam trap 2“Imports help you organize a long file but don’t reduce its context cost, because imported files also load at launch.”
↩︎ Exam trap 4“**/*.ts | All TypeScript files in any directory src/**/* | All files under src/ directory *.md | Markdown files in the project root”
↩︎ Checkpoint - 2.https://code.claude.com/docs/en/features-overviewOfficial docs
“Every session, or when matching files are opened”
↩︎ Why some conventions should load only on matching files - 3.https://code.claude.com/docs/en/context-windowOfficial docs
“path-scoped rules load automatically alongside matching files”
↩︎ Why some conventions should load only on matching files“you run /compact, which replaces the conversation with a structured summary”
↩︎ Why some conventions should load only on matching files“Rules with paths: frontmatter | Claude Code reloads them on demand”
↩︎ Why some conventions should load only on matching files“Project-root CLAUDE.md and unscoped rules | Re-injected from disk”
↩︎ Why some conventions should load only on matching files - 4.https://code.claude.com/docs/en/claude-directoryOfficial docs
“rules/*.md | paths | Rule frontmatter”
↩︎ Writing the paths frontmatter - 5.https://support.claude.com/en/articles/14553240-give-claude-context-claude-md-and-better-promptsOfficial docs
“loaded on demand when Claude reads files in that directory, not at session start”
↩︎ Glob rules vs a subdirectory CLAUDE.md - 6.https://code.claude.com/docs/en/large-codebasesOfficial docs
“You want all conventions in one place, or the same rule applies to many scattered paths”
↩︎ Glob rules vs a subdirectory CLAUDE.md“Directory owners maintain their own conventions; instructions are versioned with the code”
↩︎ Glob rules vs a subdirectory CLAUDE.md“Write database queries with the Knex query builder.”
↩︎ Glob rules vs a subdirectory CLAUDE.md“You want all conventions in one place, or the same rule applies to many scattered paths”
↩︎ Exam trap 3