Skip to content

Load conventions only where they apply

Write .claude/rules/ files with paths globs so a convention loads only for matching files, and choose a path rule or a directory CLAUDE.md for a given codebase layout.

Scenario

Scenario: Code Generation with Claude Code. Your monorepo has 38 feature folders, and each one keeps its Playwright end-to-end specs next to the code: checkout/checkout.e2e.ts, search/filters.e2e.ts, and so on.

The 90 lines of end-to-end conventions sit in the root CLAUDE.md, so they load in every session, including the many that never touch a spec. Putting a CLAUDE.md in each feature folder would mean 38 copies that drift apart.

One rule file with one glob does better than both.

Explanation

A file in .claude/rules/ without frontmatter loads in every session, like .claude/CLAUDE.md. Add a paths field and it becomes conditional: it loads only when Claude works with a file that matches one of its glob patterns, that is when it reads, writes or edits such a file. The exam guide says "when editing matching files". Sessions that never touch those files never pay for the rule.

---
paths:
  - "src/api/**/*.ts"
---

# API rules
- Validate every request body

Glob patterns match by directory, by file type, or both:

Pattern Matches
**/*.ts TypeScript files in any directory
src/**/* Every file under src/, at any depth
src/components/*.tsx .tsx files directly in that one folder
src/**/*.{ts,tsx} Both extensions, via brace expansion

* stays inside one folder; ** crosses any number of folders. paths takes a YAML list or a comma-separated string, and it is the only field a rule reads: a misspelled key such as path is ignored, and so is frontmatter whose YAML doesn't parse. In both cases the rule loads in every session.

Path rule or directory CLAUDE.md?

Choose When
A directory CLAUDE.md The convention belongs to one directory tree, and its owners keep it next to their code
A path-scoped rule The same convention covers files scattered across the codebase (tests, specs, migrations), or you want all conventions in one place

And the other two options: conventions every session needs go in the root CLAUDE.md, and procedures someone runs on purpose go in a skill.

Worked example

Before: the root CLAUDE.md, loaded in every session.

# Project
- Use pnpm for all scripts

## End-to-end specs (90 lines)
- Select elements by role or test id, never by class
- ...

After: the section moves to .claude/rules/e2e-specs.md.

---
paths:
  - "**/*.e2e.ts"
---

# End-to-end specs
- Select elements by role or test id, never by class
- Each spec creates its own user through the API
- No fixed sleeps: wait for a visible state instead

What changed:

  • **/*.e2e.ts matches a spec in any of the 38 folders, and in folder 39 when someone adds it, with no copy to maintain.
  • The root file is 90 lines shorter, so sessions about the payment service or the build scripts no longer carry spec conventions.
  • When Claude opens checkout/checkout.e2e.ts, the rule loads and the conventions apply as before.