skip to content

How does a Claude Code .claude/rules file with paths: frontmatter differ from an unscoped rule, and when does it load?

level: middleimportance: should knowfreq 13%

answer

  1. CLAUDE.md split into topic files
  2. no frontmatter means every session
  3. a glob list in YAML
  4. a file tool on a matching file
  5. unparseable YAML loads it unscoped

basics

~20 s

A rule without paths loads every session like .claude/CLAUDE.md; one with a paths glob loads only when Claude works on a matching file, at 2.1.286 by reading it. paths is the only field read, and bad YAML makes the rule unscoped.

solid answer

~40 s

Markdown files under `.claude/rules/` are discovered recursively. A rule with no `paths` frontmatter loads at launch every session, with the same priority as `.claude/CLAUDE.md`. A rule whose YAML frontmatter has a `paths` list of globs, such as `"src/api/**/*.ts"`, loads only when Claude uses a file tool on a matching file, not on every tool use: at 2.1.286 that is the Read tool, and 2.1.288 added Write and Edit. `paths` is the only field Claude Code reads; others are ignored silently, and frontmatter that fails to parse is dropped, so the rule loads as if unscoped. Personal rules in `~/.claude/rules/` load before project rules, and neither overrides the other. The payoff is context: area-specific conventions cost nothing until Claude touches that code.

code

markdown · 11 lines
markdown
---
paths:
  - "src/api/**/*.{ts,tsx}"
  - "tests/api/**/*.test.ts"
---

# API rules

- Validate every request body with the shared schema helper.
- Return errors in the standard error envelope.
- Add an OpenAPI comment to each new handler.

go deeper

for a junior

Recall that .claude/rules holds Markdown instruction files and that a paths field limits when one applies.

for a middle

Explain both kinds: unscoped rules at launch, scoped rules on a matching file tool, with paths as the only field and globs with brace expansion.

for a senior

Catch the silent failures: unparseable YAML making a rule always-on, outside symlinks waiting for approval, and the 2.1.288 change to the trigger.

for a principal

Decide what a team keeps in CLAUDE.md, rules, nested files and skills to balance context cost against reliability.

## Rules: CLAUDE.md split into topic files For larger projects, Claude Code lets you split instructions into Markdown files under `.claude/rules/`. Each file covers one topic (`testing.md`, `api-design.md`, `security.md`), and Claude Code discovers every `.md` file **recursively**, so `frontend/` and `backend/` subfolders work. Rules come in two kinds: - **Unscoped rules** have no `paths` frontmatter. They load **at launch, every session**, with the same priority as `.claude/CLAUDE.md`. They are CLAUDE.md cut into tidier files. - **Path-scoped rules** carry YAML frontmatter with a `paths` field listing glob patterns. They load **only when Claude works with a file that matches**. ## When a path-scoped rule loads The trigger is a file tool acting on a matching file, **not every tool use**. At the 2.1.286 anchor that means Claude reading the file with the **Read** tool. Release 2.1.288 fixed rules not loading when **Write** or **Edit** creates or changes a file in scope, so on later releases those trigger it as well. Starting a session inside `src/api/`, running `grep` over the directory through Bash, or listing it with Glob does not load the rule. Matching also works when Claude reaches a file through a symlinked path to the project. That timing is the point: the rule costs no context until Claude touches the code it governs. The docs recommend moving instructions that matter for only part of the codebase out of CLAUDE.md and into path-scoped rules for exactly that reason. ## The frontmatter, exactly - `paths` is **the only field** Claude Code reads from a rule. Any other field, a `description` for instance, is ignored without an error, and the frontmatter is stripped before the rule enters context. - `paths` accepts a **YAML list or a comma-separated string**. - Globs behave as expected: `**/*.ts` matches every TypeScript file, `src/**/*` everything under `src/`, `*.md` Markdown files at the project root. - **Brace expansion** works, as in `src/**/*.{ts,tsx}`. A rule's whole list shares a budget of 1,000 expanded patterns and 4 MiB; a pattern that would exceed it is used unexpanded, and its literal braces match nothing. - If the YAML **does not parse**, Claude Code ignores the frontmatter and **loads the rule as if it had no `paths`**, so it silently becomes always-on. `claude --debug` shows the parse error. - A `[` that cannot be read as a bracket expression makes that one pattern match nothing while the others keep working; escape it as `\[`. ## Where rules live and how they combine | Location | Applies to | Loads | | --- | --- | --- | | `.claude/rules/` in the project | Everyone who clones the repository | Unscoped at launch; scoped on a match | | `~/.claude/rules/` | Every project on your machine | Before project rules | | A symlink inside `.claude/rules/` | Wherever the link exists | Like an external import when the target is outside the project | User rules load before project rules, so a project rule appears later in context. As with CLAUDE.md, **neither overrides the other**: if they conflict, Claude may follow either. Symlinks let several repositories share one rule set, but a link whose target sits outside the working directory is treated like an external import: the linked rules wait until the project's external imports are approved, and even then only the unscoped ones load. Keeping shared rules in `~/.claude/rules/` avoids that. Project rules are also skipped when `project` is excluded from `--setting-sources`. ## Troubleshooting a rule - **It never applies**: check the glob against the file's path from the project root (`api/**` does not match `src/api/users.ts`), then run `/context` after Claude reads a matching file. An `InstructionsLoaded` hook reports such loads with the reason `path_glob_match`. - **It always applies**: the frontmatter probably failed to parse, so the rule fell back to unscoped; `claude --debug` shows why. - **It loads in one repository but not another**: a symlinked rule whose target is outside the project is waiting for external-import approval. ## Choosing between rules and the alternatives 1. **Unscoped rule or CLAUDE.md**: facts needed in every session. 2. **Path-scoped rule**: conventions for one area kept centrally in `.claude/rules/`, or one convention that applies across scattered paths. 3. **Nested CLAUDE.md**: conventions owned by a directory's team and versioned beside its code; it loads when Claude reads a file there. 4. **Skill**: a procedure or task-specific instructions that should load only when invoked or judged relevant to the prompt. A rule, like everything in this family, is **guidance**: it shapes what Claude tends to do, while permission rules and hooks decide what it can do.

  • When is a skill a better home for instructions than a path-scoped rule?
    When the instructions are a procedure or a task rather than a convention tied to files, such as a release checklist. A path-scoped rule loads whenever Claude works with matching files; a skill loads only when invoked or judged relevant to the prompt, whatever files are open.
  • How do you share one set of Claude Code rules across several repositories on your machine?
    Either keep them in `~/.claude/rules/`, which applies to every project and needs no approval, or symlink a shared directory into each project's `.claude/rules/`. A symlink pointing outside the project is treated like an external import, so those rules wait for approval and then only the unscoped ones load.

  • Path-scoped rules load at startup and are filtered later
  • Any tool that touches a matching path, Bash included, triggers the rule
  • Rule files accept description and other skill frontmatter fields
  • Frontmatter YAML that fails to parse disables the rule entirely
  • Project rules override the rules in ~/.claude/rules/

Assumes Claude Code 2.1.286, where a path-scoped rule loads when Claude reads a matching file; 2.1.288 also loads it on Write or Edit.