What you will be able to do
- Turn an always-loaded .claude/rules/ file into a conditionally loaded one by adding paths frontmatter
- Write glob patterns that scope a rule to one directory tree, to a file type anywhere in the repo, or to several patterns at once
- Spot the glob mistakes that make a rule miss nested files or match the wrong directory
- Choose between a path-scoped rule and a subdirectory CLAUDE.md for a given convention
Key concept
Path-scoped rule — A .claude/rules/ markdown file whose YAML frontmatter has a paths field of glob patterns. It stays out of Claude's context until Claude works with a file that matches one of those patterns, and then it loads with that file.
1.From always loaded to conditionally loaded
Every instruction Claude Code loads takes up space in the context window, whether or not it is relevant to the current task. Say a repository has API rules about input validation, Terraform tagging conventions and React testing conventions. A session spent fixing a CSS bug needs none of them. If they all load at startup, they cost tokens and add noise on every turn of that session.
.claude/rules/ topic files split project instructions into one markdown file per topic. Without frontmatter, a rules file loads every session, the same as the project CLAUDE.md. A paths field in its frontmatter makes the file conditional: it loads only when Claude works with a file that matches one of its glob patterns. The .claude directory reference describes rules files as topic-scoped instructions, optionally path-gated, and the paths field is the gate.
The difference shows in how each kind loads. A rule without paths loads when the session starts. A rule with paths sits on disk until Claude reads or edits a matching file, and then it enters context alongside that file. A session that never touches src/api/ never pays for the API rules.
The difference also holds after compaction. When /compact replaces the conversation with a summary, unscoped rules are re-injected from disk straight away, but path-scoped rules are not. They reload on demand, just as they did the first time, so a compacted session doesn't fill back up with conventions for files it has stopped working on.
| Mechanism | After compaction |
|---|---|
| Project-root CLAUDE.md and unscoped rules | Re-injected from disk |
| Rules with paths: frontmatter | Claude Code reloads them on demand |
| Nested CLAUDE.md in subdirectories | Claude Code reloads them on demand |
Checkpoint 1 of 7· Check yourself
A repository has .claude/rules/terraform.md with no frontmatter. A developer spends a whole session fixing a CSS bug and never opens a Terraform file, yet the Terraform tagging conventions are in context on every turn. Which change keeps them out of sessions like this?
A rules file without frontmatter loads in every session, like CLAUDE.md. Adding paths makes it conditional, so it loads only alongside a matching file. Splitting the file or moving the text into CLAUDE.md still loads everything, and after /compact unscoped rules are re-injected from disk.
“path-scoped rules load automatically alongside matching files”Source: code.claude.com
2.Writing the paths frontmatter
The paths field goes in a YAML frontmatter block at the very top of the rules file, between two --- lines. The markdown instructions come after it. Here is the canonical example, an API rule that applies only to TypeScript under src/api/:
---
paths:
- "src/api/**/*.ts"
---
# API Development Rules
- All API endpoints must include input validation
- Use the standard error response format
- Include OpenAPI documentation commentsThe field is optional. If you leave it out, you get an ordinary always-loaded rule. Its value can be a YAML list, as above, or a comma-separated string. Each pattern is quoted, which keeps YAML from misreading characters such as * and {.
paths is the only frontmatter field that rules files support. Skills accept a long list of frontmatter keys, but the .claude directory reference lists exactly one for rules/*.md. A rules file written with include:, files: or globs: doesn't scope anything. The scoping key is always paths.
Here is how an infrastructure team would apply this. To load Terraform formatting and tagging conventions only when Claude edits Terraform code, create .claude/rules/terraform.md with this frontmatter:
paths:as the key- one entry,
"terraform/**/*", for every file underterraform/at any subfolder depth - the Terraform conventions as markdown below the closing
---
Checkpoint 2 of 7· Fill the gap
This rules file in .claude/rules/ should load only when Claude works with TypeScript files under src/api/. Which frontmatter key belongs 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 commentsRules files support only one frontmatter field, paths. A rules file that uses globs:, include: or files: is not scoped to any files, so its conventions are not limited to matching files the way they are with paths.
3.Glob patterns: matching by location and by file type
Glob patterns are resolved relative to a base directory, not relative to wherever Claude happens to be working. For a project rule, the base directory is the one that contains the rule's .claude folder, which is normally the repository root. For a user-level rule in ~/.claude/rules/, it is the session's primary working directory. So "api/**/*.ts" in a project rule looks for an api/ folder at the repository root. It never matches src/api/routes/users.ts. To scope a rule to src/api/, write the full path from the root: "src/api/**/*.ts".
Depth comes from the difference between * and **. A single * matches names within one directory. src/api/*.ts matches src/api/index.ts but misses route handlers in src/api/v2/users/. A ** segment matches any number of directories, so src/api/**/*.ts covers both. Prefixes go wrong in the other direction too: src/**/*.ts reaches every subfolder, but it also pulls in src/utils/ and src/components/, which the API team never meant to include. In the same way, **/terraform/* finds a terraform directory anywhere but takes only the files directly inside it. That is the wrong shape for conventions that must reach every nested module.
| Pattern | Matches |
|---|---|
| **/*.ts | .ts files anywhere under the base directory |
| src/**/* | All files under the base directory’s src/ |
| *.md | .md files anywhere under the base directory |
| /*.md | .md files directly in the base directory, not in subfolders |
| src/components/*.tsx | .tsx files directly in the base directory’s src/components/, not in subfolders |
A pattern that starts with **/ matches by file type, wherever the file lives. **/*.test.tsx matches src/components/Button.test.tsx, src/pages/checkout/Cart.test.tsx and any test file added later in a directory that doesn't exist yet. This is how you write one testing convention for test files spread across the whole codebase, without listing their directories.
Brace expansion and multiple entries keep a rule's pattern list short. {ts,tsx} covers both extensions in one entry, so src/**/*.{ts,tsx} does the work of src/**/*.ts plus src/**/*.tsx. Each further list entry adds another place the rule applies, and a file that matches any one entry loads the rule:
---
paths:
- "src/**/*.{ts,tsx}"
- "lib/**/*.ts"
- "tests/**/*.test.ts"
---Drop the third line and you have the smallest correct answer for ".ts and .tsx at any depth under src/, plus .ts at any depth under lib/". Two entries are enough. Splitting the braces into separate lines gives the right matches with more entries. Widening to **/*.ts matches TypeScript everywhere in the repo, and dropping the ** (src/*.{ts,tsx}) misses every nested file.
"src/api/**/*.ts". The src/api/ prefix is anchored at the base directory, which keeps utils and components out, and ** reaches every nested handler folder.
Checkpoint 3 of 7· Match them up
Match each glob pattern in a project rule's paths field to the files that make the rule load. Patterns resolve from the repository root.
Tap a term, then the definition that fits it.
A single * stays in one directory level, while reaches any depth. Patterns are anchored at the base directory, which for a project rule is the repository root, so api/ does not match src/api/. A leading / matches files by type wherever they are, which is why it suits test files spread across the codebase.
“src/components/*.tsx | .tsx files directly in the base directory’s src/components/, not in subfolders”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/.
Sources4
4.Path-scoped rules or a subdirectory CLAUDE.md
A subdirectory CLAUDE.md also loads conditionally, but the condition is a place, not a pattern. A CLAUDE.md inside frontend/ loads when Claude reads files in that subdirectory, not at session start. It loads at launch only if the session starts from that directory. A path-scoped rule loads when Claude works with any file its globs match, wherever that file is.
That difference settles most cases. Test files show why. If *.test.tsx files sit next to their components in dozens of folders, a CLAUDE.md approach needs a copy of the testing conventions in each of those folders, and every copy has to be kept in sync. A folder without one gets no conventions at all. One rules file with paths: - "**/*.test.tsx" covers every test file now and every one added later. Conventions defined by file type, such as test files, migrations or a language, belong in a glob rule.
A subdirectory CLAUDE.md is the better fit when conventions really belong to one location and the people who own it. A monorepo package or a subsystem like src/db/ may have its own stack: use the Knex query builder, never edit a merged migration. Keeping those instructions in that directory lets the directory's owners maintain them, and they are versioned alongside the code they describe.
| 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 |
The two can be combined. A repository can keep a lean root CLAUDE.md for build commands, per-package CLAUDE.md files for each stack, and a few path-scoped rules for cross-cutting file types. Whichever you use, look for contradictions between the layers: if a nested CLAUDE.md and a rules file disagree, Claude may pick one of the two instructions arbitrarily.
Checkpoint 7 of 7· Check yourself
Your team's *.test.tsx files sit next to their components in about forty folders, and new feature folders with tests are added every sprint. You want one set of testing conventions to reach every test file. Where should those conventions go?
These conventions depend on the type of file, not on where it lives. One glob rule starting with **/ matches test files in any folder, including folders added later. Per-folder CLAUDE.md copies have to be kept in sync and miss any folder without a copy. The root CLAUDE.md loads in every session. A tests/ prefix only matches files under a root-level tests/ folder, not tests that sit next to their components.
“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/context-windowOfficial docs
“path-scoped rules load automatically alongside matching files”
↩︎ From always loaded to conditionally loaded“Rules with paths: frontmatter | Claude Code reloads them on demand”
↩︎ From always loaded to conditionally loaded“path-scoped rules load automatically alongside matching files”
↩︎ Key concept - 2.https://code.claude.com/docs/en/claude-directoryOfficial docs
“Topic-scoped instructions, optionally path-gated”
↩︎ From always loaded to conditionally loaded“rules/*.md | paths | Rule frontmatter”
↩︎ Writing the paths frontmatter“rules/*.md | paths | Rule frontmatter”
↩︎ Exam trap 3 - 3.https://code.claude.com/docs/en/features-overviewOfficial docs
“Every session, or when matching files are opened”
↩︎ From always loaded to conditionally loaded - 4.https://code.claude.com/docs/en/memoryOfficial docs
“Glob patterns that scope the rule to matching files. Accepts a YAML list or a comma-separated string”
↩︎ Writing the paths frontmatter“All files under the base directory’s src/”
↩︎ Writing the paths frontmatter“Project rules: the directory that contains the rule’s .claude folder”
↩︎ Glob patterns: matching by location and by file type“User-level rules: the session’s primary working directory”
↩︎ Glob patterns: matching by location and by file type“.ts files anywhere under the base directory”
↩︎ Glob patterns: matching by location and by file type“if two instructions contradict each other, Claude may pick one arbitrarily”
↩︎ Path-scoped rules or a subdirectory CLAUDE.md“.tsx files directly in the base directory’s src/components/, not in subfolders”
↩︎ Exam trap 1“Project rules: the directory that contains the rule’s .claude folder”
↩︎ Exam trap 2“.tsx files directly in the base directory’s src/components/, not in subfolders”
↩︎ Prediction“src/components/*.tsx | .tsx files directly in the base directory’s src/components/, not in subfolders”
↩︎ Checkpoint - 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”
↩︎ Path-scoped rules or a subdirectory CLAUDE.md - 6.https://code.claude.com/docs/en/large-codebasesOfficial docs
“When Claude works with a file matching the rule’s paths: glob”
↩︎ Path-scoped rules or a subdirectory CLAUDE.md“Directory owners maintain their own conventions; instructions are versioned with the code”
↩︎ Path-scoped rules or a subdirectory CLAUDE.md“You want all conventions in one place, or the same rule applies to many scattered paths”
↩︎ Exam trap 4“You want all conventions in one place, or the same rule applies to many scattered paths”
↩︎ Checkpoint