Claude Code Rules: Split Your CLAUDE.md by Path
Introduction
A CLAUDE.md grows with every mistake the agent makes. Each new line is reasonable. Together they become a file too long for the agent to apply with equal attention on every edit, and the usual fix, trimming it, throws out the rules that cost the most to learn.
The Codyssey backend, a Laravel 13 and Statamic v6 app, went the other way. Its CLAUDE.md is 63 lines, and its instruction about coding standards is one sentence: "Do not restate rules elsewhere; edit the rule file." The rules themselves live in .ai/rules/: 21 files and an index, 2,907 lines in all as of 2026-10-08, and each file loads only when the agent works on a path it covers.
The rules themselves are specific to this codebase, so copying them would not help you. What is worth copying is how they are kept: routed by path, each with its reason, its severity, and the code to use instead. This is part 4 of the series that starts with My Claude Code Workflow for Laravel, From Idea to Merged PR.
Rules are routed by file path
Every rule file starts with frontmatter that lists the paths it governs. This is the top of statamic-nplus1.md, the rule file for loading Statamic content without extra queries:
---
paths:
- "Codyssey/Content/**"
- "Codyssey/PublicContent/**"
- "Framework/Models/Entry.php"
- "Framework/Models/Term.php"
- "Framework/Models/Asset.php"
---Claude Code reads .claude/rules/, which in this repo is a symlink to .ai/rules/, and loads a rule file automatically when the agent reads, writes, or edits a file that matches one of its paths. Laravel Boost regenerates .ai/rules/index.md on every update, one row per file, as a fallback for when rules do not auto-load, which the agent can read itself: "Before planning or editing, find the row whose globs match the file's path and read that rule file."
How many files load depends on where the agent is working. I ran the index's globs against four real paths:
File being edited | Rule files that load |
|---|---|
| 3 of 21 |
| 7 of 21 |
| 8 of 21 |
| 10 of 21 |
A few files apply almost everywhere, like orchestrators-di.md and isolation.md. The rest are narrow. The rules for Statamic queries never load while the agent is writing a vendor webhook, and the Filament rules never load outside the admin panel.
Each rule file needs one discipline: its paths: frontmatter. Without it, Claude Code loads the file in every session instead of only where it applies.
Each rule says why it exists
The house style for a rule is written down in the project's infer-conventions skill: "state the constraint, then one line of why it exists (the trap it prevents, the incident it traces to, the pattern it protects)". A rule with an incident behind it survives the next agent that finds it inconvenient. A rule that reads like someone's preference gets argued away.
Some rules trace to production errors. The top of statamic-nplus1.md:
Born from Sentry N+1 issues BACKEND-5D/5Z/6J/6P/6Z (fixed 2026-08-22) and BACKEND-7W (code reviews list, 2026-10-15); the batching pattern lives in
Framework/Models/ContentListReferences.php(the author, term and asset resolution every list item shares [...]),Codyssey/Content/Courses/CourseListReferences.php(adds lesson counts) [...]
The header goes on to name three more files and the methods a new list slice reuses. It gives the reason, the Sentry issue IDs, the dates they were fixed, and the files that show the correct pattern. The search rules do the same for a schema error: "(Sentry BACKEND-72: staging 2026-08-23, production 2026-09-03 once the change was promoted)".
Most rules trace to a decision instead. 17 of the 21 files cite at least one ADR (architecture decision record, a short file that records one decision and its alternatives), and content-access.md cites ADRs on 16 lines. Three files cite Sentry issues. Three cite neither, models-naming.md, seeders.md and vertical-slices.md; their rules point at the architecture docs or the code they enforce.
Severity is marked per rule, not per file
Each rule ends with its weight: **high.**, **med.** or **low.**. One file holds rules of different weights, and the agent can tell them apart without the author splitting the file. A full rule from social-images.md, constraint first, then the why, then the tag:
A job dispatched by a publish must never
$entry->save().GenerateSocialImageJobwrites its result toog_image, which is safe from a deliberate operator click and fatal from a listener: the save raises a freshEntrySaved, which re-entersContentPublishedListenerand the whole broadcast path. This is why the automatic path is a separate job that persists nothing and is found again by re-deriving its content-addressed filename, the same wayRenderSlideDeckJobis. high.
The tags are not a private scale. They match "the severity vocabulary /codyssey-reviewer's auditor contract already uses", so a rule's weight and the weight of a review finding against it are the same word. 15 of the 21 files carry tags, on 160 lines; adminpanel-filament.md has the most. Part 6 of the series shows the reviewer using them.
Rules name the replacement
A rule that only says "don't" gets worked around. The agent still needs the data, so it finds another way to get it, often a worse one. The N+1 rule in statamic-nplus1.md bans reading reference fields inside a loop, and in the same paragraph names the methods to use instead:
Instead: extract raw values and batch-load once per collection through the
Framework\Models\Entrystatics:publishedByIdsInCollection(),publishedAuthorProfilesByIds(),byIds(). high.
The next rule in that file goes further. A batched query has to re-apply the filters Statamic would have applied, or unpublished content leaks into the API, and the rule names the one place that does it: "That filter is never hand-rolled. Framework\Models\EntryVisibility is the single chokepoint". The agent does not have to invent the right filter, and no second copy of it appears.
Rules rot, and the maintenance is real
As of 2026-10-08, 99 commits had touched .ai/rules/ since it was created on 2026-08-07. Some add rules. Some fix rules that went stale because the code they describe changed. One commit title says it directly: "Update authorization.md's stale canAccessPanel quote and record the dual-enum rule" (ec6955ce). The rule quoted canAccessPanel() as a role check at Admin.php:78, and then a branch added a status guard to that method and moved it. The rule kept describing the old code until someone noticed.
A rule that quotes code, with a line number, is a rule that will be wrong once that code changes. A vague rule never goes stale because it never says anything checkable, so the cost is worth paying. Two habits keep the cost down. Rules point at files rather than copying their code, and every change that touches a rule's subject checks the rule. CLAUDE.md lists the matching "rule file in .ai/rules/" among the things to update before opening the PR.
New rules arrive the same way, during the work. Boost's record-rule tool captures one mid-session. The project's infer-conventions skill describes it: it "appends ## {title}\n{note} verbatim to the bottom" of the owning file and regenerates the index. The output "has no severity tag, no why line, and no cross-references, so treat it as a fast capture step and immediately edit the appended section into house style". Capturing a rule is cheap. Writing its reason is the part that needs a person, and I review every new rule in the pull request that adds it.
Keep vendor guidance from contradicting you
Laravel Boost installs guidance for each package it detects, written for a default Laravel app. Some of it contradicts the project. The project config records how badly: laravel-best-practices "contradicts the hard rules in .ai/rules/ on a dozen points (mass assignment, route-model binding, event discovery, LazilyRefreshDatabase, helpers over DI, inline caching, constrained() foreign keys, Markdown mailables)."
ADR-021 sets the fix: "Boost's vendor guidance is curated at the source, so the rendered block never contradicts .ai/rules/ and no rule file needs an 'overrides Boost' clause." Guidelines that add nothing are excluded in config/boost.php. Guidelines that are partly right are replaced by files at the same path under .ai/guidelines/. The rule for the next one is short: "exclude it if it adds nothing, override it if part of it is worth keeping. The rule file stays the single statement of the rule."
The check has to be repeated on every update. Removing a skill from Boost's install list is not enough, because the next update reinstalls whatever the detected packages ship: "The first boost:update on this branch proved it by installing the MCP and Passport skills unasked." Part 5 covers the skills half of this, replaced by project versions with the same names.
Where to start
Take the rule your agent breaks most often and give it its own file. Add paths: frontmatter for the folders it governs. Under the rule, write one line on why it exists, a severity tag, and the code or method to use instead of what it bans. Delete the old line from CLAUDE.md.
Related Links
- My Claude Code Workflow for Laravel, From Idea to Merged PRpart 1, the whole process followed through one feature, with links to every other part
- Claude Code memory and ruleshow CLAUDE.md and path-scoped rule files are loaded
- Laravel Boostthe package that generates the index, provides record-rule, and installs the vendor guidance this piece curates