Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Claude Code rules: scope rules to folders and take them out of CLAUDE.md

14 min read
Improve this page

Logo Claude

Your CLAUDE.md has grown a lot: API conventions, test rules, infra scripts, all mixed together. Claude reads the whole file at every session, and some rules pollute the areas where they do not apply. Targeted rules (.claude/rules/*.md) solve this problem: you take the rules that are too specific out of CLAUDE.md and load them only when Claude reads a file of the area concerned, thanks to the paths frontmatter.

What you will learn

  • What stays in CLAUDE.md and what goes into .claude/rules/
  • The syntax of the paths frontmatter with globs
  • Apply a targeted rule on lab-claude
  • Check which rules are really loaded in the session with /context

When should you use this feature?

The trigger is not only the size of the file, it is the relevance ratio. The Claude Code documentation recommends keeping each CLAUDE.md under 200 lines: beyond that, it uses more context and its instructions are followed less well. But a 120-line CLAUDE.md where half of the lines concern only tests/ already deserves to be split. Every useless line takes up context at every session and dilutes the rules that really matter.

  • Your CLAUDE.md exceeds 150 lines and mixes several areas
  • A rule applies to one folder only (e.g. tests/, app/api/)
  • Several teams contribute and want local rules
  • You want to prevent test rules from guiding changes on the API side

Prerequisites

  • CLAUDE.md already in place (see configure CLAUDE.md (in French))
  • Running project ready: ~/Projets/lab-claude

CLAUDE.md vs .claude/rules/

The distinction comes down to one word: the moment of loading. CLAUDE.md and the rules without paths enter the context at session startup, with the same priority. A rule with a paths field is not there: it is injected later, when Claude reads a file that matches the pattern. It is therefore not a filter applied to every tool call, but a trigger on reading.

MechanismLoaded whenRecommended use
CLAUDE.mdAt every session, in fullAlways-true facts: stack, commands, global conventions
.claude/rules/<name>.md without pathsAt every session, in fullCross-cutting rules too large for CLAUDE.md
.claude/rules/<name>.md with pathsWhen Claude reads a file that matchesRules specific to an area (API, tests, migrations)
~/.claude/rules/<name>.mdSame mechanism, on all your projectsYour personal preferences, independent of the repository

The last line is the one people forget: rules also exist at the user level, in ~/.claude/rules/. They follow exactly the same loading rules, paths included, but apply to all your repositories. This is the place for your personal conventions, the ones you do not impose on the team by committing them.

A simple rule:

  • If the rule holds for the whole project and fits in a few lines, keep it in CLAUDE.md
  • If it holds only for a specific folder, move it into .claude/rules/<area>.md with paths

Structure of rules

Rules live in .claude/rules/ and are discovered recursively: a file stored in .claude/rules/frontend/react.md is found without declaring anything. Each Markdown file covers one topic and can be scoped with a YAML frontmatter:

lab-claude/
├── CLAUDE.md
├── .claude/
│ ├── settings.json
│ └── rules/
│ ├── tests.md
│ └── api.md
└── ...

Example of a rule targeted at tests:

---
paths:
- "tests/**/*.py"
---
# Rules for tests
- Use FastAPI's `TestClient` for endpoint tests
- Each endpoint has at least one nominal-case test and one error test
- Fixtures live in `tests/conftest.py`
- Do not mock Pydantic validation: test the real behaviour

Without paths, the rule is loaded at every session. With paths, it enters the context only at the first file read that matches the glob.

Syntax of the paths frontmatter

The patterns follow the usual glob syntax, with a distinction that often traps people: * does not cross directories, ** does. Writing app/*.py therefore covers only the files placed directly in app/, not those in app/routers/. This is a very frequent cause of rules that seem never to trigger.

Beware of the bracket [ too: glob reads it as the start of a character class ([abc]). A pattern containing a [ that is not properly closed matches nothing, silently, without breaking the other patterns of the same rule. For a literal bracket in a file name, escape it: \[.

A few useful patterns:

PatternMatches
**/*.pyAll Python files of the project
app/**/*All files under app/
tests/**/*.pyAll Python tests
app/api/**/*.pyAPI only
migrations/*.sqlMigration SQL files at the root of migrations/

You can combine several patterns and use brace expansion:

---
paths:
- "app/**/*.{py,pyi}"
- "tests/**/*.py"
---

Applying it on lab-claude

Goal: move the test rules and the API endpoint rules out of CLAUDE.md into two dedicated rules.

Step 4 is the one people forget, and it is the only one that produces the benefit you are after. Creating the files in .claude/rules/ without removing the matching sections from CLAUDE.md only adds context: the rules are now present twice, with the risk that they diverge at the first change. The move must be a move, not a copy.

  1. Create the rules folder

    Terminal window
    cd ~/Projets/lab-claude
    mkdir -p .claude/rules
  2. Create .claude/rules/tests.md

    ---
    paths:
    - "tests/**/*.py"
    ---
    # Rules for tests
    - Each endpoint has at least one nominal-case test and one error test
    - Use FastAPI's `TestClient` for endpoint tests
    - Name of test functions: `test_<endpoint>_<case>`
    - No mock on Pydantic validation
  3. Create .claude/rules/api.md

    ---
    paths:
    - "app/**/*.py"
    ---
    # Rules for the API
    - Each endpoint declares its return type (`-> dict[str, str]`)
    - Explicit HTTP status through `status_code=` if different from 200
    - Document the endpoint in one line (docstring)
    - Do not duplicate the logic of `/health`: factor it out if needed
  4. Lighten your CLAUDE.md

    Remove the Tests and API sections that now live in the rules. Keep in CLAUDE.md only what holds for the whole project: stack, validation commands, global conventions.

  5. Start a session and check

    Terminal window
    claude

    Then in the session:

    /context

    The rules without paths show up right away under Memory files. The rules with paths show up only when Claude reads a file that matches.

Checking what is loaded

Two commands, two distinct uses, and mixing them up wastes time. /context shows what is really loaded in the current session, under the Memory files heading: it is the diagnostic command when Claude seems to ignore a rule. /memory lists and opens the locations of the instruction files (CLAUDE.md, CLAUDE.local.md, automatic memory), including those that do not exist yet: it is the editing command, not a checking one.

The two-step test, positive then negative

A rule missing from /context proves nothing until you have triggered its loading. The check is therefore done with two files at different paths, and it takes two sessions.

  1. The positive case. Ask Claude to read a file that matches the pattern, for example tests/test_sante.py for a paths equal to "tests/**/*.py". Run /context again: the rule must now appear under Memory files. If it is not there, the problem is in the rule file, not in the glob.

  2. The negative case. Open a new session, which is essential since a rule already loaded stays loaded, and have Claude read a file outside the scope, for example app/main.py. The rule must stay absent.

The second step is the one people skip, and it is the only one that catches a glob that is too broad. A rule that loads everywhere is no longer targeted: it weighs on every session without anything signalling it, which is exactly the problem you were trying to solve by taking it out of CLAUDE.md.

When to keep a rule in CLAUDE.md, when to move it

The last line of the table below settles a frequent confusion. A rule is an always-true fact that Claude must keep in mind; a procedure is a sequence of steps to run in a particular case. Procedures have no business in rules: they would take up context permanently to be used once. They go into skills, which are loaded only when invoked.

The rule...Where to put it
applies to the whole project and fits in 2 linesCLAUDE.md
applies to the whole project but takes 20+ lines.claude/rules/<topic>.md without paths
concerns only one folder.claude/rules/<area>.md with paths
is personal and must not be sharedCLAUDE.local.md
is personal and holds for all your projects~/.claude/rules/<topic>.md
is a procedure (not a fact)to move into a skill

One limit deserves to be stated before you write your first rule: like CLAUDE.md, a rule is an instruction that Claude reads, not a configuration that Claude Code enforces. It guides, it does not prevent. For a guaranteed behaviour, whatever happens, you need hooks (in French) and permissions, not a better-worded rule. Writing "never run this command" in a rule reduces the probability; a hook brings it down to zero.

Common mistakes

The first two lines look alike but are not diagnosed the same way. A rule missing from /context was not read at all: the problem is in the file itself, an unclosed frontmatter or a wrong location. A rule present but with no effect on some files was read: it is the glob that does not cover the expected area. Always check presence before touching the pattern.

SymptomLikely causeFix
The rule does not appear in /contextUnclosed frontmatter or wrong pathsCheck the three dashes and the YAML syntax
Rule ignored on new filesGlob too restrictiveChange app/*.py to app/**/*.py
CLAUDE.md still just as longCross-cutting rules left insideMove only the rules that are really targeted
Contradictory rules between scopesA user rule conflicts with a project ruleProject rules come after those of ~/.claude/rules/ and take precedence; settle it once and for all

End-of-guide checklist

Go through these five points on a real project, not on the lab. The third is the one that decides the result: as long as CLAUDE.md has not slimmed down, the context consumed at every session has not moved and the exercise brought nothing.

  • I created the .claude/rules/ folder
  • I have at least one rule with a working paths
  • I lightened CLAUDE.md by removing the rules I moved
  • I checked with /context that the rules are loaded
  • I know when to keep a rule in CLAUDE.md and when to take it out

Key takeaways

  • CLAUDE.md = global facts, .claude/rules/ = targeted rules
  • The paths frontmatter avoids polluting the context with irrelevant rules
  • Each rule covers one topic, to stay readable and maintainable
  • /context is the checking tool: it shows what is loaded in the session, whereas /memory only lists and opens the files
  • Procedures do not go into rules: they go into skills

Knowledge check

Check that the essentials of this guide are mastered. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer