A Claude Code setup that doesn't rot: rules, tests, review and guardrails
Claude Code best practices that hold up: a short CLAUDE.md, a check Claude can run, a reviewer that never saw the code, and hooks for hard rules.
- Published
- Reading
- 11 min
- Subject
- Systems
- Written by
- Aleem Ul Hassan

Key takeaways
- Most Claude Code setups rot because instructions pile up in CLAUDE.md until the lines that matter get lost. Keep it short and only write down what Claude cannot find by reading the repo.
- CLAUDE.md is a guideline Claude may decide does not apply. A rule with zero exceptions belongs in a hook, a deny rule or a managed setting.
- Give Claude a check that returns pass or fail, ask for a failing test first, and ask for the command output as evidence rather than a summary.
- Review with something that did not write the code: a fresh subagent or /code-review, checked against a written spec, with style out of scope.
- Review the setup itself on a schedule. Delete stale lines, delete hooks that only enforce preferences, and keep the enforced set small.
A Claude Code setup that doesn’t rot keeps four things apart. A short CLAUDE.md for what Claude cannot see in the code. A check Claude can run that comes back pass or fail. A reviewer that did not write the change. And hooks or deny rules for the few rules that can never break. Everything else is optional.
Sources checked October 27, 2026. The setup below follows Anthropic’s own Claude Code documentation and our working setup. We have not run a controlled experiment on it and quote no productivity gain of our own.
Here is the pattern, as an illustration rather than a case. Someone runs /init, the file gets a few rules, then a few more every time Claude does something annoying. A few months later CLAUDE.md is hundreds of lines long, half of it describes code that has moved, and the line that actually mattered (“never touch the migrations folder”) is buried on line 312. Claude still breaks it now and then, so someone adds IMPORTANT in capitals. That is what rot looks like.
Why does a Claude Code setup rot?
It rots because every fix goes into the same place. CLAUDE.md is loaded into every session, so each line costs a little attention on every task, relevant or not. As the file grows, the important lines get lost and stale ones point at code that no longer exists. The cure is to sort each instruction by what kind of instruction it is.
Anthropic’s best-practices page splits every instruction into two kinds:
- Claude decides. Your prompt, CLAUDE.md and skills. The model reads them and usually follows them, but there is no guarantee.
- Claude Code enforces. Hooks, deny rules and managed settings. The harness applies them every time, whatever the model decided.
Its own tip is blunt. If you keep adding emphasis to a CLAUDE.md line and Claude still misses it, the line belongs in a hook. And if you have hooks for things that are only preferences, delete them.
What belongs in CLAUDE.md, and what doesn’t?
Only what Claude cannot work out by reading the repo, or would waste time rediscovering every session. Anthropic’s test has two questions: could it find this by reading the code, and would it actually go and look? If both answers are yes, leave it out. That keeps the file short enough that every line still gets read.
In practice that leaves four kinds of line:
- How to check work here. The exact test, typecheck and lint commands, and how to run a single test file.
- Places where the code misleads. An old pattern that is everywhere but must not be copied, or a folder that looks dead but is not.
- Decisions that live outside the repo. Which error helper the team picked, how deploys work, what needs a migration review.
- Anything you have corrected twice.
The memory guide recommends staying under 200 lines. Run /context in a session to confirm the file actually loaded. Personal preferences go in CLAUDE.local.md, added to .gitignore, so they stop leaking into everyone else’s sessions.
Two more places take load off the main file. A rule that only matters in one part of the code goes in .claude/rules/ with a paths: filter, so it loads only when Claude works there. A procedure, like cutting a release, goes in a skill at .claude/skills/<name>/SKILL.md. Claude sees its name and description every session but reads the full file only when the task needs it.
| Instruction | Where it goes | Why |
|---|---|---|
| Test, build and lint commands | CLAUDE.md | Claude needs them on almost every task |
| A convention for one folder | .claude/rules/ with paths: |
Loads only when that folder is touched |
| A multi-step procedure | A skill | Body loads only when invoked |
| A rule with zero exceptions | Hook or deny rule | Applied by the harness, not remembered by the model |
| Your own preferences | CLAUDE.local.md |
Stays out of the shared repo |
When should a rule become a hook or a deny rule?
When “almost every time” is not good enough. A hook runs your command at a fixed point in Claude’s loop, such as before a tool call, after an edit, or when Claude tries to stop. A deny rule blocks a tool call outright in every permission mode. Both apply whatever the model decided, which a line in CLAUDE.md cannot promise.

Anthropic’s examples are the right size. “Never read .env” becomes a deny rule. “Always run the formatter after editing” becomes a PostToolUse hook on Edit|Write. “Don’t touch the migrations folder” becomes a PreToolUse hook that exits with code 2 for any path under migrations/. You do not have to write these by hand. A prompt like “write a hook that blocks edits to the migrations folder” works, and /hooks shows the result.
{
"permissions": {
"allow": ["Bash(pnpm tsc --noEmit)"],
"ask": ["Bash(git push *)"],
"deny": ["Read(./.env)", "Read(./.env.local)", "Read(./secrets/**)"]
}
}
That is the example from Anthropic’s page, saved as .claude/settings.json. Committed to the repo, it applies to everyone who works there.
Know where the edges are. Rules match tool calls, not intent. The Read deny catches cat .env, but not a script that opens the file. A WebFetch deny for a domain does nothing about curl to it unless the sandbox is on. For anything security related, Anthropic recommends deny rules and the sandbox together.
How do you make Claude work tests-first?
Give it something that returns pass or fail, then ask for the failing test before the fix. Claude stops when work looks done. Without a check it can run, “looks done” is its only signal, which makes you the test suite. With one, it makes the change, runs the check, reads the failure and fixes it on its own.
The prompt does most of the work. Anthropic’s own before and after:
Instead of: fix the login bug
Try: users report login fails after the session times out. look at
token refresh in src/auth/, write a failing test that reproduces it,
then fix it
The failing test does two jobs. It proves the bug is real before anything changes, and it becomes the definition of done. A person should still read what the test expects. A test written after the code, to match the code, proves very little. Our refactoring playbook covers keeping approved fixtures outside what the agent is allowed to edit.
Then decide how strict the check is. In the prompt (“keep going until the auth tests pass”) Claude judges for itself. As a Stop hook, Claude Code runs your command whenever Claude tries to end its turn and sends it back to work if the command exits with code 2:
{
"hooks": {
"Stop": [
{ "hooks": [{ "type": "command", "command": "pnpm tsc --noEmit 1>&2 || exit 2" }] }
]
}
}
Whichever you pick, ask for evidence: the command Claude ran and its output. “All tests pass” on its own tells you very little, and for a session you were not watching it is the only record you have.
How should the PR review loop work?
The reviewer should be something that did not write the code. A session that produced a change is persuaded by its own reasoning, so it reviews its own work poorly. Use /code-review, which reviews the diff in a fresh subagent that never saw the conversation, or a subagent you brief yourself. Then a person approves the purpose.
Point the review at the plan, not at the code in general:
use a subagent to review the rate limiter diff against SPEC.md, check
every requirement is implemented and the listed edge cases have tests,
report gaps that affect correctness and skip style
Keep the “skip style” part. Anthropic’s note on this is worth repeating: a reviewer asked to find problems will always find something. Fix every finding and you end up with extra abstraction and tests for cases that cannot happen.
Close the loop by feeding lessons back into the setup. InfoQ’s account of the Claude Code creator’s workflow (January 2026) describes teams at Anthropic keeping a shared CLAUDE.md in git and tagging Claude on pull requests to add what was learned. The rule we add: those updates go through a pull request like any other change, so the file gets reviewed instead of quietly growing.
Who approves what, which checks block a merge and how fast you can revert is a separate policy. We wrote that one up in a merge policy for AI-written code.
Which guardrails matter most for AI-written code?
The ones that run before the model gets a vote. Deny rules and PreToolUse hooks apply before auto mode’s classifier sees an action, and the sandbox confines shell commands at the operating-system level. Auto mode is useful, but it is a model judging each action, and Anthropic’s docs say plainly that it can be wrong.

Our own example. We run Claude Code headless inside our internal publishing tool, the one that drafted the first version of this post. Nobody watches those runs, so a PreToolUse hook does three things on every tool call:
- Writes and edits are allowed only inside that run’s own folder.
- Shell commands that publish, commit, push or deploy are blocked. A person approves first, then the tool does those steps itself.
- Anything that looks like a secret file is blocked, and if the hook cannot read its input it blocks rather than allows.
That last line is the one people skip. A guard that fails open lets everything through on the day it breaks. Anthropic’s docs offer onFailure: "block" for the same reason.
Keep the set small. Every hook and deny rule will sometimes fire when it should not, and someone has to maintain each one. Anthropic’s own guidance: if you find yourself at 40 of them, most are probably preferences Claude could have weighed itself.
How do you keep the setup from rotting?
Treat it like code with an owner and a review date. Once a month, cut CLAUDE.md lines that Claude can now work out from the code, delete hooks that enforce preferences rather than hard rules, and move any line Claude keeps missing into a hook. Every change to the setup goes through a pull request.
Claude Code ships two tools for the first part. /doctor suggests cuts for anything Claude can already work out from the code. /doctor prompt-audit flags instructions written for older models, pointing at files that no longer exist, or contradicting each other.
A 15-minute monthly pass:
- Run
/doctor prompt-auditand fix what it flags. - Read CLAUDE.md top to bottom. For each line, ask whether Claude would find it by reading the code. If yes, delete it.
- List the lines you have restated in a prompt in the last month. Those either go in CLAUDE.md or, if they are never-break rules, become hooks.
- List your hooks and deny rules. Any that blocked something legitimate more than once gets rewritten or removed.
- Check that
.claude/settings.json, the rules folder and CLAUDE.md all have a named owner in your code owners file.
The starter setup, in one table
If you are setting up a repo this week, this is the whole list. Start with the first three rows. Add the rest when a real mistake shows you need them.
| File | What goes in it | Add it when |
|---|---|---|
CLAUDE.md |
Commands, misleading code, off-repo decisions | Day one, after /init, mostly by deleting |
.claude/settings.json deny rules |
Secrets and paths Claude must never read or edit | Day one |
| Test command Claude can run | One test file, the full suite, typecheck | Day one, if the repo has tests |
Stop hook |
The check that must pass before Claude finishes | Claude has ended a turn with a failing check |
PreToolUse hook |
Never-break rules, such as protected folders | A CLAUDE.md line was ignored twice |
.claude/rules/ with paths: |
Conventions for one part of the code | CLAUDE.md passes about 100 lines |
| Skills | Release, deploy and incident procedures | You have explained the same procedure twice |
CLAUDE.local.md |
Your own preferences | Your preferences start showing up in team sessions |
None of this needs a new tool. It is a few files in the repo, owned by the team, reviewed like code. That last part matters for agencies and clients alike: if someone else set up your Claude Code workflow, you should hold the files and know why each rule exists. What we hand over lists what that looks like on our builds, and the handoff test is a quick way to find the rules still living in one person’s head.
If your team is shipping AI-written code and the setup has started to sprawl, book an Ops Teardown. Bring your CLAUDE.md and settings file. We will tell you what to cut, what to enforce, and what can stay as it is.
FAQ
How long should a CLAUDE.md file be?
Short. Anthropic’s memory guide recommends staying under 200 lines, and the best-practices page says every line takes some of Claude’s attention on every task. Keep the test commands, misleading parts of the code, decisions that are not in the repo, and anything you have corrected twice.
What is the difference between CLAUDE.md and a hook?
CLAUDE.md is read by the model, which decides what to do with it, so it is followed nearly every time but not guaranteed. A hook is run by Claude Code itself at a fixed point, such as before a tool call or when Claude tries to stop, so it applies every time.
Should Claude write the tests before the code?
For bug fixes and well-defined behaviour, yes. Ask for a failing test that reproduces the problem, then the fix. A person should still approve what the test expects, because a test written to match the code proves very little.
Can Claude review its own pull requests?
It can review a diff, but not from the session that wrote it. Use /code-review or a subagent with a fresh context, give it the spec, and tell it to skip style. A person still approves anything that touches money, data, access or customers.
Is auto mode enough to keep Claude Code safe?
No. Auto mode is a classifier judging each action, and Anthropic’s own docs say it can be wrong. Rules that must never break belong in deny rules, PreToolUse hooks or the sandbox, which apply before the classifier sees anything.


