Claude Corner

Claude Applying Your React Rules to Python Files? Scope Each Rule With paths.

By ยท 30 Oct 2026

A developer seen from behind faces a monitor showing three colored folder shapes, with a magenta card clipped onto only the orange folder.

At a glance

  • Move instructions for one part of the repo out of CLAUDE.md into .claude/rules/.
  • Add paths frontmatter 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.

  1. Mark the candidates. Run /memory, open your CLAUDE.md, and flag every section that only matters for part of the codebase.
  2. Create the folder. Make .claude/rules/ with one topic per file, such as api.md or react.md. Subfolders are found too.
  3. Add the scope. Start each file with YAML frontmatter holding a paths list of globs. Patterns are relative to the folder that contains .claude, and braces work, so src/**/*.{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.
  4. Leave always-on rules unscoped. A rule with no paths loads at launch, like .claude/CLAUDE.md. Personal rules go in ~/.claude/rules/ and apply to every project.
  5. Check launch. Run /context in 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.
  6. Watch scoped rules load. Add an InstructionsLoaded hook with the matcher path_glob_match to .claude/settings.json. Its input on stdin includes the rule's file_path and the trigger_file_path that loaded it. The example needs jq installed, and /tmp/claude-rules.log is just our example path. If your settings already have a hooks block, add the InstructionsLoaded entry 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. Run claude --debug to 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 cat or head in 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:

Recommended AI prompt

Copy this paragraph into ChatGPT, Claude, Gemini, Grok, or whatever AI you use.

Prompt

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.