A Claude Code rule is a Markdown file in a project's .claude/rules/ folder that Claude Code gives to Claude, either at the start of every session or only once Claude works with a file that matches the rule's paths. Rules split a long CLAUDE.md into topics, and keep instructions for one part of a project out of sessions that never touch it. See Organize rules with .claude/rules/ in the Claude Code documentation. This note describes Claude Code 2.1.337.

What problem do they solve?

You use Claude Code on my-app/, a small web API for orders. Its CLAUDE.md is a Markdown file that Claude Code loads into Claude's context, everything Claude can see in the conversation, at the start of every session. It says how to run the tests, that every function needs type hints, how to name tests, and what an API error looks like.

The file grows as you add instructions, and every session gets all of it. A session that only fixes the README still carries the testing and API instructions, which take up context and compete with the instructions that matter for the task. The documentation suggests keeping each CLAUDE.md under 200 lines, because longer files "consume more context and reduce adherence". See Write effective instructions.

Rules let you split the file by topic. A rule with paths reaches Claude only when Claude works on the files it is about.

How do I write one?

Create .claude/rules/ in my-app/, and move the testing instructions to .claude/rules/testing.md:

---
paths:
  - "tests/**/*.py"
---

# Testing rules

- Name tests `test_<function>_<case>`.
- Build orders with the `order` fixture from `tests/conftest.py`.

The block between the --- lines is YAML frontmatter. paths lists glob patterns, relative to my-app/, the folder that contains .claude/. tests/**/*.py matches every Python file under tests/, at any depth. A pattern such as *.md matches at any depth too, and /*.md only directly in my-app/. Braces match several endings at once: src/**/*.{py,pyi}. Claude Code removes the frontmatter before it gives the rule to Claude.

Move the API instructions to .claude/rules/api.md, with src/api/**/*.py as its pattern, and the type hint instruction to .claude/rules/style.md, with no frontmatter. CLAUDE.md keeps what every session needs:

my-app/
├── CLAUDE.md              what the app is, how to run the tests
├── .claude/
│   └── rules/
│       ├── style.md       no paths
│       ├── testing.md     paths: tests/**/*.py
│       └── api.md         paths: src/api/**/*.py
├── src/
│   ├── app.py
│   └── api/
│       └── orders.py
└── tests/
    └── test_orders.py

Commit .claude/rules/ with the code, so everyone who uses Claude Code on the project gets the same rules. Claude Code also finds rules in subfolders, such as .claude/rules/frontend/. For personal rules that apply to all your projects, use ~/.claude/rules/ in your home folder.

When does a rule load?

A rule without paths, such as style.md, loads when the session starts, together with CLAUDE.md. A rule with paths loads the first time Claude reads, writes, or edits a matching file, through the Read, Write, or Edit tool or a shell command that reads one file, such as cat tests/test_orders.py. The rule's text arrives with the result of that tool call, and stays in the conversation from then on. Figure 1 shows one session.

Three steps of a session in my-app. At launch, Claude has CLAUDE.md and rules/style.md, which has no paths, while rules/testing.md and rules/api.md are not loaded. After Claude reads tests/test_orders.py, rules/testing.md is loaded because it matched tests/**/*.py. After Claude edits src/api/orders.py, rules/api.md is loaded because it matched src/api/**/*.py, and rules/testing.md is still loaded.
Figure 1. The files under rules/ are in .claude/rules/. Reading src/app.py at any step would load nothing, because no pattern matches it.

To see which instruction files load, when, and why, add an InstructionsLoaded hook. A hook here is a command that Claude Code runs at a fixed point in a session. This one runs each time a CLAUDE.md or rule loads. It receives the file's path, the reason it loaded (load_reason, such as session_start or path_glob_match), and for a rule with paths, the file that matched.

How do rules compare with the other options?

Option When Claude gets it Use it for
CLAUDE.md At the start of every session Facts every task needs: commands, layout, conventions
Rule without paths At the start of every session The same, split into one file per topic
Rule with paths When Claude first uses a matching file Instructions for one folder or file type
@path import in CLAUDE.md With the CLAUDE.md that imports it Organizing a long file. It saves no context
Skill When you invoke it, or when Claude decides it's relevant Procedures for a task, such as a release checklist
Hook Not an instruction: Claude Code runs a command Anything that must happen every time, such as formatting after an edit

What should I watch out for?

  • A rule with paths arrives after Claude creates a new file. If Claude writes tests/test_refunds.py from scratch, testing.md loads with the result of that write, so the first version of the file is written without it. Ask Claude to read the rule, or an existing file it covers such as tests/test_orders.py, before it creates new ones.
  • A rule with broken frontmatter loads in every session. Claude Code reads only paths and ignores any other field without an error, so a rule copied from another tool with a globs: field has no paths. If the YAML doesn't parse, Claude Code ignores the frontmatter in the same way. Check new rules with claude --debug, which shows parse errors.
  • Patterns are relative to the project, not to the rule. testing.md uses tests/**/*.py, even though the rule itself is in .claude/rules/. Patterns in ~/.claude/rules/ are relative to the folder where you started Claude Code.
  • Rules guide Claude, but nothing enforces them. Claude usually follows them, more reliably when an instruction is specific. For something that must always happen, use a hook, and to block a command, use permission rules.
  • Conflicting rules don't override each other. User rules load before project rules, but if the two disagree, Claude may follow either one. Keep each instruction in one file.
  • /compact drops rules with paths until they match again. Compaction replaces the conversation with a summary. Claude Code then reloads CLAUDE.md and rules without paths from disk, but a rule with paths returns only when Claude next uses a matching file. If a rule must survive compaction, remove its paths or move it to CLAUDE.md. See what survives compaction.

When are rules worth it?

  • A long CLAUDE.md. Sections that apply to one folder or file type can move to rules with paths, so other sessions don't carry them.
  • Different conventions in different parts of a project. Tests, API code, and documentation can each have their own rules, and each file stays short enough to read in one go.
  • A shared project. One file per topic is easier to review and change than one long file.

Start with a short CLAUDE.md. When it nears 200 lines, or when a section matters only for some files, move that section to a rule with paths. Keep what every session needs in CLAUDE.md, and put procedures you run on request in skills.


Reading Time

~5 min read

Published

Category

Tags

Stay in Touch