Path-scoped rules: each instruction next to the code it is about

My answer to that is path-scoped rules. The first thing everyone does with a coding agent is create a CLAUDE.md or AGENTS.md and write in every rule that comes to mind. A few months later the file is a thousand lines, and the agent reads all of it at every start, even if today it only needs to change a button's colour.
The problem isn't just token cost. With many irrelevant rules, the important ones get lost. A model, like a person, handed a hundred instructions at once pays less attention to some of them.
Three homes for a rule: CLAUDE.md, path-scoped rules and specs
In SimaCMS rules have three definite places:
- A rule for the whole project goes in AGENTS.md: branch names, who merges, what needs a test. Short, and always loaded.
- A rule for one part of the tree goes in its own file under .claude/rules/ with a paths: glob at the top. Go rules load only when the agent opens a .go file; frontend rules only for web/.
- What a capability does isn't an instruction at all; it goes in that capability's spec.
One important point: modules get no instruction files of their own. If every module had a CLAUDE.md, a year later we'd have forty diverging copies of the platform's rules. Platform rules live in one place and reach modules by path.
A context budget
What always loads is a fixed cost on every session, so we measure it. A script counts the tokens of the always-loaded files, and a CI guard keeps them under a ceiling. If someone wants to add a paragraph to AGENTS.md and the budget is full, they remove something or move it to a path-scoped file.
That limit showed us many rules weren't needed at all. When space is expensive, only what actually prevents a mistake survives.
Every rule has a story
Most of our rules come from a real mistake. An agent once imported one capability's package from another: the rule went into the Go rules, and later a guard was written for it. An agent once read a huge document whole and ran out of context: "read documents by the section" was added.
My order is: first write the mistake down as a path-scoped rule. If it happens again and can be checked, it becomes a guard and the rule gets shorter. A rule with a guard needs no long explanation; the guard's error message is the explanation.
People benefit too
This isn't only for agents. A teammate working on the Go side reads .claude/rules/go.md, not a hundred-page wiki. The rule sits next to the code it's about, so when the code changes the rule is far more likely to change with it.
Path-scoped rules answer where to write an instruction. For what to write, read What goes in CLAUDE.md, and for system behaviour, a spec before code. The technical details of path-scoped rules are in the Claude Code documentation.
Comments
Sign in to leave a comment.
Sign inLoading…



