Claude Corner
Claude Applying Your React Rules to Python Files? Scope Each Rule With paths.
By Steven Sorensen ยท 30 Oct 2026

At a glance
- Move instructions for one part of the repo out of
CLAUDE.mdinto.claude/rules/. - Add
pathsfrontmatter so each rule loads only for matching files. - Log scoped rules as they load to confirm the scoping works.
Say your CLAUDE.md started as ten lines. Now it holds API error formats, React conventions and migration rules, and Claude carries all of it into every session, even one that only touches a Python script. Claude Code can load each rule only when it is relevant.
Why it matters
Anthropic's docs suggest keeping each CLAUDE.md under 200 lines, because longer files use more context and reduce adherence. Imports with @path do not help, since imported files load with the file that imports them. Rules in .claude/rules/ with a paths field load only when Claude reads, writes or edits a matching file, so your API rules stay out of context until Claude opens an API file. The move: anything that applies to one folder or file type becomes its own scoped rule.
How to scope rules to paths
We built these steps from Anthropic's docs and have not run them in a live session.
- Mark the candidates. Run
/memory, open yourCLAUDE.md, and flag every section that only matters for part of the codebase. - Create the folder. Make
.claude/rules/with one topic per file, such asapi.mdorreact.md. Subfolders are found too. - Add the scope. Start each file with YAML frontmatter holding a
pathslist of globs. Patterns are relative to the folder that contains.claude, and braces work, sosrc/**/*.{ts,tsx}covers both extensions. Each#line in the snippet only names the file. Leave those lines out, because the frontmatter must open the file and JSON allows no comments. - Leave always-on rules unscoped. A rule with no
pathsloads at launch, like.claude/CLAUDE.md. Personal rules go in~/.claude/rules/and apply to every project. - Check launch. Run
/contextin a new session to see which files loaded at launch. Our read: a scoped rule should not be on that launch list, since it loads only when Claude touches a matching file. - Watch scoped rules load. Add an
InstructionsLoadedhook with the matcherpath_glob_matchto.claude/settings.json. Its input on stdin includes the rule'sfile_pathand thetrigger_file_paththat loaded it. The example needsjqinstalled, and/tmp/claude-rules.logis just our example path. If your settings already have ahooksblock, add theInstructionsLoadedentry to it.
# .claude/rules/api.md
---
paths:
- "src/api/**/*.ts"
---
- Validate every endpoint's input.
- Use the standard error response format.
# .claude/rules/react.md
---
paths:
- "src/components/**/*.{ts,tsx}"
---
- Function components only.
# .claude/settings.json: log each scoped rule as it loads
{
"hooks": {
"InstructionsLoaded": [
{
"matcher": "path_glob_match",
"hooks": [
{ "type": "command",
"command": "jq -c '{file_path, trigger_file_path}' >> /tmp/claude-rules.log" }
]
}
]
}
}
Gotchas
- Bad YAML means always on. If the frontmatter does not parse, Claude Code loads the rule as if it had no
paths. Runclaude --debugto see the error. - File access is the trigger. A scoped rule loads on Read, Write or Edit of a matching file, or a single-file
catorheadin Bash. Until then, Claude lacks it. - Big rule files get cut. Since v2.1.309, a scoped rule over the per-file limit (40,000 characters on a 200K context window) arrives truncated. Check the size with
wc -m. - Rules guide, settings enforce. A rule is not a hard enforcement layer. To block a tool or file path outright, use
permissions.deny.
Sources:
- Manage Claude's memory, Organize rules with .claude/rules/, Claude Code docs
- Hooks reference, InstructionsLoaded, Claude Code docs
Recommended AI prompt
Copy this paragraph into ChatGPT, Claude, Gemini, Grok, or whatever AI you use.
My Claude Code CLAUDE.md has grown long and mixes rules for different parts of my repo. I will paste it below with my folder layout. Sort each instruction into always-on or folder-specific, write the .claude/rules files I need with YAML paths frontmatter, and tell me what stays in CLAUDE.md. Use only behavior from the official Claude Code memory docs and link me to that page.