Apply path-specific rules for conditional convention loading
Large codebases carry many conventions, and loading all of them on every request wastes context and dilutes the model's attention. Path-specific rules in `.claude/rules/` attach conventions to glob patterns so a rule only enters the context window when Claude edits a matching file. This lesson covers the `paths` frontmatter, how conditional loading saves tokens, and why glob rules beat directory-level CLAUDE.md for conventions that span scattered files like tests.
What a path-specific rule file looks like
Path-specific rules live in the .claude/rules/ directory of your project. Each rule is a plain markdown file, and what makes it conditional is an optional block of YAML frontmatter at the top. The paths field takes a list of glob patterns; the markdown body below the frontmatter is the actual guidance Claude should follow.
.claude/
rules/
testing.md
terraform.md
api-handlers.md
A single rule file looks like this:
---
paths: ["**/*.test.tsx", "**/*.test.ts"]
---
# Test conventions
- Use React Testing Library, never Enzyme.
- Query by role or label text, not by test id.
- One top-level describe() named after the unit under test.
Because .claude/ is committed to the repository, these rules are project-scoped and shared with the whole team, the same hierarchy level as the project CLAUDE.md and .claude/commands/. You can keep many small, focused rule files instead of one monolith, and each file can list several globs in its paths array.
Conditional activation, and why it saves context
The defining behavior of a path-scoped rule is conditional loading: its content enters the context window only when the session is working on a file that matches one of its glob patterns. Edit a Terraform file and terraform.md is pulled in; edit a React test and testing.md is pulled in; work anywhere else and neither costs you a single token.
Contrast this with the root CLAUDE.md, which is loaded on every request regardless of what you are doing. In a large codebase with conventions for tests, infrastructure, API handlers, migrations, and more, putting everything in an always-on file bloats the base context and dilutes the model's attention with rules that are irrelevant to the current file.
Path scoping fixes both problems at once. It reduces token usage, because irrelevant conventions are simply absent, and it sharpens focus, because only the conventions that apply to the file in front of Claude are present. This is the exam's stated advantage: rules load only when editing matching files, reducing irrelevant context and token usage.
Glob patterns match by file type, not by folder
The real power of glob patterns is that they match on the path and extension, not on a folder boundary. A pattern like **/*.test.tsx matches components/Button.test.tsx, features/login/LoginForm.test.tsx, and any other test file no matter how deep or where it lives. The convention is bound to the file type, not to a location.
A directory-level CLAUDE.md cannot do this. It only covers its own contiguous subtree, and it applies to every file in that subtree. It has no way to say 'just the test files' or 'just the .tf files' when those are scattered across dozens of directories, and no way to target a single extension. Common patterns you will see: **/*.test.tsx for all React test files, terraform/**/* for everything under a Terraform tree, **/migrations/*.sql for migration scripts anywhere, and **/*.stories.tsx for Storybook stories.
Colocated tests are the canonical case: teams place Button.test.tsx right next to Button.tsx, so there is no single tests/ directory to attach a CLAUDE.md to. A glob rule reaches all of them; a directory file reaches almost none.
Path-specific rules vs subdirectory CLAUDE.md vs skills
Three mechanisms can carry conventions, and the exam expects you to pick the right one:
- Directory-level
CLAUDE.md: best when a convention applies to a self-contained subtree and to essentially everything in it, for example apackages/billing/package with its own rules. - Path-specific rules (
.claude/rules/withpathsglobs): best when a convention attaches to a file type or pattern spread across many directories, for example tests, migrations, or.tffiles. - Skills (
.claude/skills/): on-demand workflows that are invoked explicitly or selected by Claude for a task. They do NOT trigger automatically when you touch a matching file.
So when the requirement is 'automatically apply the correct conventions based on the file path, regardless of directory', path-specific rules are the answer. This is exactly the reasoning in the sample question about a codebase with React, API, and database conventions plus tests spread throughout: a root CLAUDE.md with headers relies on inference, per-directory CLAUDE.md files cannot reach scattered files, and skills require invocation. Only glob-scoped rule files apply the right conventions deterministically by path.
Organizing, scoping, and verifying rules
Treat rule files the way you would treat well-factored code: one topic per file. A testing.md, an api-conventions.md, and a deployment.md, each scoped to its own globs, is the modular alternative to a monolithic CLAUDE.md, the same split-by-topic idea from the CLAUDE.md hierarchy task. A single rule file may list multiple globs when one set of conventions covers several related patterns.
Keep globs as tight as the convention warrants. A broad pattern like **/* reintroduces always-on loading and mixes unrelated rules back together, undoing the benefit. Prefer extension-specific or subtree-specific patterns.
When behavior looks inconsistent, meaning a convention is or is not being applied, use the /memory command to see which memory and rule files are currently loaded. That tells you whether a path rule actually matched the file you are editing, which is the fastest way to diagnose a mis-scoped paths glob.
Anti-patterns to avoid
Why it fails: Test files are colocated with source across the codebase (Button.test.tsx sits next to Button.tsx), so no single directory contains them. A directory CLAUDE.md only covers its own subtree, so most tests never receive the conventions.
instead Create .claude/rules/testing.md with paths: ["**/*.test.tsx"] so the conventions attach to the file type wherever it lives.
Why it fails: That file is always loaded, wasting tokens on every request, and it relies on inference rather than explicit matching, so React rules can bleed into API files and vice versa.
instead Split the conventions into path-scoped rule files that load only for matching files, making application explicit and keeping the base context lean.
Why it fails: Skills are on-demand: they run when explicitly invoked or when Claude selects them for a task, not automatically whenever a matching file is touched. Convention application becomes non-deterministic.
instead Use path-specific rules for automatic, file-triggered conventions, and reserve skills for invokable task workflows.
Why it fails: An over-broad glob reintroduces always-on loading, defeating the token and attention savings and mixing unrelated conventions back together.
instead Scope each rule as tightly as the convention warrants, by extension or specific subtree, and split into multiple focused rule files.
Worked example: Applying conventions across React, API, DB, and scattered test files
Scenario: You use Claude Code on a codebase with three distinct areas. React components use a functional style with hooks, API handlers use async/await with a specific error-handling shape, and database models follow a repository pattern. On top of that, test files are colocated throughout the tree (Button.test.tsx sits next to Button.tsx), and you want every test to follow the same conventions regardless of where it lives. The goal is for Claude to apply the correct conventions automatically when it generates or edits code.
Wrong turn 1, root CLAUDE.md with headers. You could put all four convention sets in the root CLAUDE.md under '## React', '## API', '## Models', '## Tests'. But that file loads on every request (token waste), and it forces Claude to infer which section applies, which lets React rules bleed into API files and vice versa.
Wrong turn 2, per-directory CLAUDE.md. A CLAUDE.md in each area's folder handles the three source areas, but it cannot handle tests: they are scattered next to their source files, so there is no single directory to attach a test-conventions file to.
The fix, path-scoped rule files. Create one focused rule per concern:
.claude/rules/react-components.md paths: ["src/components/**/*.tsx"]
.claude/rules/api-handlers.md paths: ["src/api/**/*.ts"]
.claude/rules/db-models.md paths: ["src/db/**/*.ts"]
.claude/rules/testing.md paths: ["**/*.test.tsx", "**/*.test.ts"]
The testing rule is the one only a glob can express: **/*.test.tsx matches every test file anywhere in the tree. Now when Claude edits features/login/LoginForm.test.tsx, testing.md loads, and when it edits a plain handler, only api-handlers.md loads. Each file gets exactly the conventions it needs, nothing is always-on, and because .claude/rules/ is committed, every teammate gets the same behavior on pull.
This is precisely why the sample exam question marks the .claude/rules/ glob approach correct over root CLAUDE.md (inference), skills (must be invoked), and per-directory CLAUDE.md (directory-bound).
Exam tips
- ✓Path-specific conventions live in `.claude/rules/` as markdown files with a YAML frontmatter `paths` field holding glob patterns, e.g. paths: ["terraform/**/*"].
- ✓A path-scoped rule loads ONLY when Claude edits a file matching its glob, reducing irrelevant context and token usage. The root CLAUDE.md is always loaded on every request.
- ✓Glob rules match by file type or pattern regardless of directory, so **/*.test.tsx covers test files scattered across the whole codebase. A directory-level CLAUDE.md is bound to one subtree.
- ✓Choose path-specific rules over a subdirectory CLAUDE.md when a convention must apply to files SPREAD across many directories (tests, migrations, .tf files).
- ✓Skills are on-demand (invoked or selected), not automatic per file edit, and a root CLAUDE.md with headers relies on inference. Neither reliably auto-applies file-type conventions the way glob rules do (sample Q6).
- ✓.claude/rules/ is committed to the repo, so path rules are version-controlled and shared with the whole team, unlike personal ~/.claude rules.
Official exam objectives for 3.3
- .claude/rules/ files with YAML frontmatter paths fields containing glob patterns for conditional rule activation
- How path-scoped rules load only when editing matching files, reducing irrelevant context and token usage
- The advantage of glob-pattern rules over directory-level CLAUDE.md files for conventions that span multiple directories (e.g., test files spread throughout a codebase)
- Creating .claude/rules/ files with YAML frontmatter path scoping (e.g., paths: ["terraform/**/*"]) so rules load only when editing matching files
- Using glob patterns in path-specific rules to apply conventions to files by type regardless of directory location (e.g., **/*.test.tsx for all test files)
- Choosing path-specific rules over subdirectory CLAUDE.md files when conventions must apply to files spread across the codebase
Flashcards from this lesson
Where do path-specific rules live, and what frontmatter field scopes them?
In .claude/rules/ as markdown files. A YAML frontmatter paths field holds a list of glob patterns, e.g. paths: ["**/*.test.tsx"].
When does a path-scoped rule enter the context window?
Only when the session edits or works on a file matching one of the rule's glob patterns. Otherwise it is not loaded, which saves tokens and keeps attention focused.
Why prefer a glob rule over a subdirectory CLAUDE.md for test conventions?
Test files are scattered across the codebase next to their source. A glob like **/*.test.tsx matches them by type anywhere, while a directory CLAUDE.md only covers one contiguous subtree.
React, API, and DB code each have conventions, and tests are spread throughout. What mechanism auto-applies the right conventions by file path?
Rule files in .claude/rules/ with YAML frontmatter glob patterns (path-specific rules). Not root CLAUDE.md (inference), not skills (on-demand), not per-directory CLAUDE.md (directory-bound).
Why are skills the wrong tool for automatic file-type conventions?
Skills are invoked on-demand or selected for a task, not triggered automatically when a matching file is edited, so convention application would be non-deterministic.
Are .claude/rules/ path rules shared with teammates?
Yes. They live in the repo under .claude/, so they are version-controlled and apply for everyone on clone or pull, unlike personal ~/.claude rules.
What is the risk of scoping a rule with paths: ["**/*"]?
An over-broad glob reintroduces always-on loading and mixes unrelated conventions together, defeating the token and attention savings. Scope tightly by extension or subtree instead.