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

    Domain 3 · Lesson 15/30

    Path-scoped rules in .claude/rules/ with paths globs

    Apply path-specific rules for conditional convention loading

    14 min read
    3.33% of exam
    6 sources
    Published 20 Oct 2026
    Docs as of 14 Oct 2026

    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.

    When each kind of instruction file enters context
    MechanismLoadsBest for
    CLAUDE.mdEvery sessionCore conventions and build commands
    .claude/rules/ without pathsEvery sessionTopic-organised conventions that always apply
    .claude/rules/ with pathsWhen Claude works with a matching fileLanguage-specific or directory-specific guidelines
    SkillOn demand, when invoked or relevantReference 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?

    Sources123

    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.

    A rule that loads only for TypeScript files anywhere under src/api/markdown
    ---
    paths:
      - "src/api/**/*.ts"
    ---
    
    # API Development Rules
    
    - All API endpoints must include input validation
    - Use the standard error response format
    - Include OpenAPI documentation comments

    An 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 comments

    Sources14

    3.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.

    One rule scoped to three areas, with a brace pattern covering two extensionsyaml
    ---
    paths:
      - "src/**/*.{ts,tsx}"
      - "lib/**/*.ts"
      - "tests/**/*.test.ts"
    ---
    What each pattern matches
    PatternMatches
    **/*.tsAll TypeScript files in any directory
    src/**/*All files under src/ directory
    *.mdMarkdown files in the project root
    src/components/*.tsxReact components in a specific directory
    src/**/*.{ts,tsx}.ts and .tsx files at any depth under src/
    src/api/**/*.tsTypeScript 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.

    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.

    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?

    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.

    Choosing where a 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

    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?

    Sources561

    Sources

    (6)

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

    1. 1.
      “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. 3.
      “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
    3. 4.
      “rules/*.md | paths | Rule frontmatter”
      ↩︎ Writing the paths frontmatter
    4. 5.
      “loaded on demand when Claude reads files in that directory, not at session start”
      ↩︎ Glob rules vs a subdirectory CLAUDE.md
    5. 6.
      “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

    Ready to test yourself?

    Practise the 16 questions on this subdomain.

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