CLAUDE.md Can’t Enforce Anything. Here’s the Hook I Use Instead.
From the publisher’s feed, collected in the ZeroLabs newsroom.
How I moved the one rule I actually could not afford to lose out of a markdown file and into eight lines of bash.For months my CLAUDE.md held a list of rules. Never run the production deploy script. Do not touch the migrations folder. Run the formatter before committing. They worked most of the time, and that is the problem. The misses never showed up during the careful sessions when I was watching. They showed up on the long, messy ones, right after I had stopped paying attention.I kept doing the obvious thing. Claude slipped, so I added another line. The rule got bolder. ALWAYS. NEVER. In caps. It still slipped. It took me an embarrassingly long time to understand why, and the answer is written plainly in Anthropic’s own docs.CLAUDE.md is context, not configurationHere is the sentence from the Claude Code memory docs that reframed all of this for me. CLAUDE.md and auto memory “are loaded at the start of every conversation. Claude treats them as context, not enforced configuration.”Context, not configuration. CLAUDE.md is not a config file the tool parses and obeys. It is text that gets injected into the model’s context window at the start of the session, as a user message, and then it competes for attention with everything else in that window for the rest of the run. The troubleshooting section is even blunter: Claude “reads it and tries to follow it, but there’s no guarantee of strict compliance.”So a rule in CLAUDE.md is a request. On a short session it usually gets honored. On a long one, with a full context window and a couple of subagents deep, that one line is competing with thousands of tokens of actual work, and sometimes it loses. A rule that holds 95 percent of the time is not a rule. It is a default.It gets worse as the file grows, which is the part that stings. The docs put a number on it: target under 200 lines per CLAUDE.md, because “longer files consume more context and reduce adherence.” Every line you add to tighten control dilutes every line already there. The junk drawer fills up and the whole thing gets followed less.For most rules that is fine. Coding style, conventions, where files live, the model gets those right often enough and the cost of an occasional miss is low. But a handful of rules are not like that. “Never run the production deploy script” is not a 95 percent rule. The one time it slips is the only time that matters.The same rule, written as a hookThe docs tell you what to do here, in the same breath. Right after “context, not enforced configuration,” the line continues: “To block an action regardless of what Claude decides, use a PreToolUse hook instead.”That is the move. A hook is a shell command you register in settings.json that fires at a fixed point in Claude Code’s loop and runs as code, outside the model. The model does not get a vote on whether it runs. Claude Code fires it every time.Here is the actual thing that replaced my CLAUDE.md line. Two pieces. First, the registration in .claude/settings.json:{ "hooks": { "PreToolUse": [ { "matcher": "Bash", "hooks": [ { "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-deploy.sh" } ] } ] }}Then the script it points at:#!/bin/bashINPUT=$(cat)COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')if echo "$COMMAND" | grep -q "deploy-prod.sh"; then echo "Blocked: deploy-prod.sh is off-limits to the agent. Ask a human." >&2 exit 2fiexit 0That is the whole hook. Now walk through what happens, because the mechanics are the entire point.What is actually happeningPreToolUse fires right before any tool call executes. The matcher set to "Bash" scopes it, so this only runs ahead of a Bash command and never touches an Edit or a file read. Before Claude Code runs the command, it hands the hook a JSON blob on stdin. For a Bash call, the exact command Claude is about to run is sitting in .tool_input.command.The script reads that, greps it for the deploy script, and if it is in there, writes a reason to stderr and exits 2. Exit 2 is the load-bearing part. For a PreToolUse hook it blocks the tool call, and the text you wrote to stderr gets handed back to Claude as feedback. So the model does not hit a silent wall and thrash. It sees “deploy-prod.sh is off-limits, ask a human,” understands why, and routes around it.The decision happened in bash, outside the model, on every single call. There is no good day or bad day. There is no full context window for the rule to get buried in. The deploy script is now a fact about the environment the command runs in, not a sentence I am hoping gets read at the right moment.What it looks like when it firesI did not want to take this on faith, so I tested it. I pointed the agent at the deploy script and watched the tool call come back blocked before it ran. The model gets the hook’s own message handed back as feedback:Blocked: deploy-prod.sh is off-limits to the agent. Ask a human.The command never executed. The model read that line, understood it could not run the deploy, and asked me to do it. I also tried hiding the script in the middle of a chained command, something like cd infra && bash ./deploy-prod.sh && echo done, and the grep still caught it. A normal command like npm test passes straight through.One honest caveat. A substring match is a backstop, not a vault door. Someone who really wanted to could rename the script or call it by an absolute path and slip past a naive grep. That is fine for what I needed. The failure I was guarding against was the model running the deploy by accident on a long session, or me forgetting, and for that a check that fires every time is plenty. If you need it airtight, match harder or block at the path level.Where the line actually goesThis is the same split that runs through all of agent engineering. A prompt is where you express intent. Enforcement is where you guarantee it, and a guarantee has to live in code that runs whether or not the model cooperates. The Claude Code docs draw the same line for managed settings: “Settings rules are enforced by the client regardless of what Claude decides to do. CLAUDE.md instructions shape Claude’s behavior but are not a hard enforcement layer.”So I stopped treating CLAUDE.md as a control surface and started treating it as a context budget. The main file holds the facts every session genuinely needs and nothing else. Anything that only matters in one corner of the repo moves to a path-scoped rule, which the docs let you gate with a paths: glob so it loads only when Claude touches matching files. Anything I genuinely cannot afford to have ignored stops being a line of prose and becomes a hook.I did not delete the deploy rule from CLAUDE.md, to be clear. I kept a short note there so Claude understands the script is sensitive and why it should leave it alone. That note shapes behavior, and that is useful. But the thing standing between a bad session and a production deploy is the hook, not the note.The takeawayThe reflex to add one more line to CLAUDE.md when the model slips is the thing quietly making your setup worse. For most rules, write them clearly, keep them short, and accept that “usually followed” is good enough. For the few you cannot afford to get wrong, move them out of the prompt and into a hook, where the answer does not depend on the model being in a good mood. The docs have been saying it the whole time. Block the action regardless of what Claude decides.SourcesClaude Code docs: How Claude remembers your project (memory)Claude Code docs: Hooks referenceClaude Code docs: Get started with hooksCLAUDE.md Can’t Enforce Anything. Here’s the Hook I Use Instead. was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.