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.mdand what goes into.claude/rules/ - The syntax of the
pathsfrontmatter 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.mdexceeds 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.mdalready 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.
| Mechanism | Loaded when | Recommended use |
|---|---|---|
CLAUDE.md | At every session, in full | Always-true facts: stack, commands, global conventions |
.claude/rules/<name>.md without paths | At every session, in full | Cross-cutting rules too large for CLAUDE.md |
.claude/rules/<name>.md with paths | When Claude reads a file that matches | Rules specific to an area (API, tests, migrations) |
~/.claude/rules/<name>.md | Same mechanism, on all your projects | Your 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>.mdwithpaths
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 behaviourWithout 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:
| Pattern | Matches |
|---|---|
**/*.py | All Python files of the project |
app/**/* | All files under app/ |
tests/**/*.py | All Python tests |
app/api/**/*.py | API only |
migrations/*.sql | Migration 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.
-
Create the rules folder
Terminal window cd ~/Projets/lab-claudemkdir -p .claude/rules -
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 -
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 -
Lighten your
CLAUDE.mdRemove the Tests and API sections that now live in the rules. Keep in
CLAUDE.mdonly what holds for the whole project: stack, validation commands, global conventions. -
Start a session and check
Terminal window claudeThen in the session:
/contextThe rules without
pathsshow up right away under Memory files. The rules withpathsshow 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.
-
The positive case. Ask Claude to read a file that matches the pattern, for example
tests/test_sante.pyfor apathsequal to"tests/**/*.py". Run/contextagain: the rule must now appear under Memory files. If it is not there, the problem is in the rule file, not in the glob. -
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 lines | CLAUDE.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 shared | CLAUDE.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.
| Symptom | Likely cause | Fix |
|---|---|---|
The rule does not appear in /context | Unclosed frontmatter or wrong paths | Check the three dashes and the YAML syntax |
| Rule ignored on new files | Glob too restrictive | Change app/*.py to app/**/*.py |
CLAUDE.md still just as long | Cross-cutting rules left inside | Move only the rules that are really targeted |
| Contradictory rules between scopes | A user rule conflicts with a project rule | Project 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.mdby removing the rules I moved - I checked with
/contextthat the rules are loaded - I know when to keep a rule in
CLAUDE.mdand when to take it out
Key takeaways
CLAUDE.md= global facts,.claude/rules/= targeted rules- The
pathsfrontmatter avoids polluting the context with irrelevant rules - Each rule covers one topic, to stay readable and maintainable
/contextis the checking tool: it shows what is loaded in the session, whereas/memoryonly 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
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
Review
(0/0)Result saved on this device. It will be sent to your account at the next sync. Sync now
Score by domain
A domain groups several guides and gets several questions, so its score is more reliable than a per-topic score, which often rests on a single question.
Summary
Strengths
No topic passed at 80% on at least two questions yet.
Weak areas
No topic below the pass mark.
Recommended next
Hints before you try again?
A new full quiz with random questions
Work again only on the questions you missed