Bible Network Crypto DeFi Onchain RWA AI Agent Stablecoin Chain SAFU CryptoTax DeFAI AGI Claude Me Claude Skill Claude Design Claude Cowork
Independent Media
Not affiliated with any project
Learn Claude Skills. Do Everything Better.
claudeskill-me.com
LATEST
Watching Claude Code Compaction and Hooks Through OpenTelemetry: Reading the compaction, hook_execution_complete and precompute_reuse Fields  ·  Blind Spots in Bash Permission Rules: deny Misses /bin/rm, bash -c and git -C, and devbox run * Lets Any Inner Command Through  ·  CLAUDE.md Too Long? Use .claude/rules/ and paths So Rules Load Only When Claude Touches Matching Files  ·  Chromium Says No usable sandbox! Inside Claude Code's Sandbox: What --no-sandbox, excludedCommands and allowNestedUserNamespaces Each Give Up  ·  Claude Code v2.1.309 Fixes a Batch of Sandbox and Permission Gaps: Startup Files Outside the Sandbox, denyWrite Through Symlinks and More  ·  The auto mode $defaults Trap: Leave One String Out of autoMode.soft_deny and the Built-In Force-Push Rules Disappear
beginners

CLAUDE.md Too Long? Use .claude/rules/ and paths So Rules Load Only When Claude Touches Matching Files

30-Second Version · For the impatient
Break the YAML in the frontmatter and a path-scoped rule does not error; it quietly becomes one that loads in every session.

Full Explanation +
01 · Why did this happen?

Where do my rule files go?

Project rules go in .claude/rules/ and personal rules in ~/.claude/rules/, which applies to every project on your machine. One topic per .md file.

02 · What is the mechanism?

Why does my path rule show up in every session?

Check whether the frontmatter YAML is broken: the docs say a parse failure makes it load as if it had no paths, i.e. unconditionally. Run claude --debug to see the parse error.

03 · How does it affect me?

What is the difference between rules and skills?

Rules load every session or when matching files are opened; skills load only when you invoke them or Claude judges them relevant. For task-specific instructions that need not always be in context, the docs suggest a Skill.

04 · What should I do?

I'm not an engineer. One-line version?

Think of an employee handbook: rules everyone must follow go in the general section, and rules only accounting needs are handed to you when you walk into accounting.

Full Content +

A CLAUDE.md that keeps growing is one of the first problems people hit: the docs advise about 200 lines per file, since longer files use more context and reduce adherence. One official way out is .claude/rules/: split rules into several files, and make some load only when Claude touches certain files. This is a getting-started walkthrough from the docs. Except where marked, everything is from the official memory page (How Claude remembers your project); I did not run the steps on my own machine, so please try them once yourself to confirm.

Directory layout

your-project/
├── .claude/
│   ├── CLAUDE.md
│   └── rules/
│       ├── code-style.md      # no paths: loads at launch
│       └── api.md             # has paths: loads on matching files
└── src/api/users.ts

Put one topic in each .md file with a descriptive name (the docs' examples are testing.md and api-design.md); subdirectories such as frontend/ and backend/ are discovered recursively. Rules without paths load at launch with the same priority as .claude/CLAUDE.md.

Step one: a rule that appears only for API files

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

API rules

  • Validate all input before using it
  • Return errors in the standard error format

The docs say paths is the only frontmatter field Claude Code reads from a rule; other fields are ignored without an error, and the frontmatter is removed before the rule loads. paths accepts a YAML list or a comma-separated string.

When does it load

A path-scoped rule loads when Claude uses Read, Write or Edit on a matching file; it also loads when Claude views a single matching file with a Bash command the docs count as a read, such as cat or head. So just opening a session without touching a file leaves the rule out of context. A rule without paths loads unconditionally and applies to all files.

Common glob patterns

  • **/*.ts: all TypeScript files in any directory
  • src/**/*: all files under src/
  • *.md: Markdown files in the project root
  • src/components/*.tsx: React components in a specific directory
  • Brace expansion works, such as src/**/*.{ts,tsx}; a rule's whole paths list shares one budget of 1,000 expanded patterns and 4 MiB, and a pattern that would exceed it is used unexpanded, so its literal braces match no files.

Three pitfalls

  • Broken YAML becomes "load always": the docs say that if the YAML in the frontmatter does not parse, Claude Code ignores it and loads the rule as if it had no paths. A rule meant for API files only then loads in every session. Run claude --debug to see the parse error.
  • Escape [: glob treats [ as the start of a bracket expression, and a pattern that cannot be read as one (such as photos [2024/) is invalid and matches nothing while the rule's other patterns keep working; to match a literal [, write photos \[2024/.
  • Symlinked shared rules: .claude/rules/ supports symlinks so you can link a shared set into several projects, but a symlink whose target is outside the working directory needs external imports approved first, and after that only rules without a paths field load. To share without that approval, keep them in ~/.claude/rules/, which applies to every project on your machine.

How to confirm it actually loaded

What I can confirm from the official docs: an InstructionsLoaded hook can fire when instruction files load, and claude --debug shows frontmatter parse errors. A third-party writeup (claudefa.st) says /memory lists loaded CLAUDE.md and rules files, which I did not verify, so go by your version. A more direct approach (my suggestion, untested): start a new session without touching any file and ask Claude "which API rules apply now"; then have it read src/api/users.ts and ask again; the two answers should differ.

Rules, CLAUDE.md or a Skill

The docs say rules load every session or when matching files are opened; for task-specific instructions that need not be in context all the time, use a skill, which loads only when you invoke it or Claude judges it relevant. My summary: short rules for the whole project go in CLAUDE.md, rules tied to a kind of file go in path-scoped rules, and a whole workflow goes in a skill. A third-party writeup also says that after Compaction, path-scoped rules return only when Claude next reads a matching file; I did not find that in the official docs, so treat it as a possibility.

Sources: How Claude remembers your project - Claude Code Docs, What survives compaction - ClaudeFast
Diagram
路徑限定規則何時進入 context沒有 paths 的規則啟動時載入,有 paths 的規則等 Claude 碰到符合的檔案才載入;frontmatter YAML 寫壞會變成無條件載入How a path-scoped rule gets into context.claude/rules/code-style.md (no paths)api.md (paths: src/api/**/*.ts)testing.md (paths: **/*.test.ts)only "paths" is read from frontmatterLoads at launchsame priority as CLAUDE.mdWaits for a matching fileRead, Write, Edit, or cat/headRule enters contextwhen Claude touches src/api/users.tsPitfall: if the frontmatter YAML does not parse, the rule loads as if it had NO pathsA rule meant for API files only then loads in every session. Check with: claude --debugAlso: escape [ as \[ in globs; a pattern that cannot be read matches nothing~/.claude/rules/ applies to every project on your machineSymlinks to outside the working directory need external-import approval; only rules without paths loadTask-specific instructions that need not always be in context belong in a skillClaude Skill Me · claudeskill-me.com
Feel free to share. Please credit the source.
Ask a Question
Please enter at least 10 characters
Related Articles
Why Your Claude Code Skill Silently Stops Triggering: The 15,000-Character Description Budget No One Warns You About
practice · Sep 02
Claude Suddenly Stopped Using Your Skills After /compact? It's Not Broken — It's Designed Not to Restore Them Automatically
beginners · Oct 02
Claude Mods Aren't Just Another Plugin — They Can Draw Panes and Intercept Screen Rendering, Which External Extensions Simply Can't
skill-library · Oct 05
Claude Code Merged Custom Commands Into Skills — Most "Commands vs Skills" Guides Are Already Outdated
skill-library · Sep 05
Related News