CertSafari
    Anthropic Claude Certified Architect – Foundations (CCAR-F)· Lessons

    Domain 3 · Lesson 15/30

    Path-scoped rules in Claude Code: paths frontmatter and glob patterns

    Apply path-specific rules for conditional convention loading

    14 min read
    3.33% of exam
    6 sources
    Published 17 Nov 2026
    Docs as of 11 Nov 2026

    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.

    How rules and CLAUDE.md files come back after /compact
    MechanismAfter compaction
    Project-root CLAUDE.md and unscoped rulesRe-injected from disk
    Rules with paths: frontmatterClaude Code reloads them on demand
    Nested CLAUDE.md in subdirectoriesClaude 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?

    Sources123

    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/:

    A rules file scoped to TypeScript files under src/api/ at any depthmarkdown
    ---
    paths:
      - "src/api/**/*.ts"
    ---
    
    # API Development Rules
    
    - All API endpoints must include input validation
    - Use the standard error response format
    - Include OpenAPI documentation comments

    The 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 under terraform/ 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 comments

    Sources42

    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.

    What common pattern shapes match, relative to the base directory
    PatternMatches
    **/*.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:

    One rule covering .ts and .tsx under src/, .ts under lib/, and test files under tests/yaml
    ---
    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.

    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.

    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?

    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?

    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?

    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.

    Choosing where a scoped convention lives
    ApproachFile locationLoads whenUse when
    Per-directory CLAUDE.mdInside the directory, alongside its codeAt launch when started from that directory, or on demandDirectory owners maintain their own conventions; instructions are versioned with the code
    Path-scoped rule in .claude/rules/Central .claude/ at the repo rootWhen Claude works with a file matching the rule’s paths: globYou 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?

    Sources564

    Sources

    (6)

    Every claim above is drawn from one of these pages, quoted as it was written on the date shown.

    1. 1.
      “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. 2.
      “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. 3.
      “Every session, or when matching files are opened”
      ↩︎ From always loaded to conditionally loaded
    4. 4.
      “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. 5.
      “loaded on demand when Claude reads files in that directory, not at session start”
      ↩︎ Path-scoped rules or a subdirectory CLAUDE.md
    6. 6.
      “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

    Ready to test yourself?

    Practise the 12 questions on this subdomain.

    Spotted a mistake, or was something unclear? Tell us.