Claude Code hooks vs CLAUDE.md: which one?
A line in CLAUDE.md is advice Claude may decide doesn't apply. A hook runs every time. Use a hook only for a rule with zero exceptions; here is how to tell.
On this page
Claude Code hooks and CLAUDE.md do different jobs. A line in CLAUDE.md is advice: Claude reads it and usually follows it, but whether it applies on a given turn is Claude’s call. A hook is a command Claude Code itself runs at a fixed moment, such as before every tool call, and it can block that call whatever Claude decided. So a rule with exceptions goes in CLAUDE.md, and a rule with zero exceptions becomes a hook, or, when it only protects a file or a command, a deny rule.
This is as of November 2026, from Claude Code’s memory docs, best practices, permissions and hooks reference.
A line in CLAUDE.md |
A deny rule | A hook | |
|---|---|---|---|
| Who enforces it | Claude, when it judges the line applies | Claude Code | Claude Code, by running your command |
| Can Claude skip it? | Yes | No | No, for command and http hooks |
| Good for | Conventions, preferences, how to work | Files Claude must not read or edit, commands it must not run | Checks with logic, and steps that must run at a set moment |
| Example | “Prefer named exports” | "Read(./.env)" |
Block every force-push; format each edited file |
| Cost | Context in every session | One line of settings | A script to write, test and maintain |
Why Claude doesn’t always follow CLAUDE.md
Claude doesn’t always follow CLAUDE.md because it isn’t built to. The memory docs say Claude treats these files “as context, not enforced configuration”, and the best practices add that CLAUDE.md “is for guidelines, and Claude is allowed to decide a guideline doesn’t apply”. That is the point of the file: most conventions do have exceptions, and you want Claude to notice them.
It also means stronger wording won’t turn advice into a rule. The best practices put it plainly: “If you keep adding emphasis to a CLAUDE.md line and Claude still misses it, that’s a sign the line belongs in a hook.”
The test: does the rule have exceptions?
Ask one question of each line: is there any case where Claude should break it? Anthropic draws the line in the same place: “a hook is for a rule with zero exceptions”, and “if something has to happen every time, make it a hook, a deny rule, or a managed setting.”
- “Prefer small commits.” Exceptions exist. It stays in
CLAUDE.md. - “Never read
.env.” No exceptions. A deny rule. - “Never force-push.” No exceptions, but the command comes in many shapes. A hook.
- “Run the formatter after every edit.” No exceptions, and it’s a step, not a refusal. A
PostToolUsehook.
Keep the zero-exception list short. Every hook is code that runs on every matching call, and a hook that blocks something Claude legitimately needs is worse than the line it replaced.
How do I block edits to a file?
To block edits to a file, use a deny rule, not a hook. In .claude/settings.json:
{
"permissions": {
"deny": ["Read(./.env)", "Edit(./db/migrations/applied/**)"]
}
}The permissions docs say Claude Code checks deny rules first, and “an allow rule can’t carve an exception out of a deny rule”. The best practices add that deny rules hold in every permission mode, bypassPermissions included. A Read deny rule also blocks editing that path, and both kinds apply to file commands Claude Code recognizes in Bash, such as cat, sed and tee. They don’t stop a script that opens the file itself: for that the docs point to the sandbox.
Write rules as Edit(...): the docs say Write(docs/**) doesn’t match.
How do I make Claude Code always do something?
To make Claude Code always do something, use a hook. Hooks are commands you register in .claude/settings.json (shared with the project), .claude/settings.local.json (just you) or ~/.claude/settings.json (every project), for events such as PreToolUse, PostToolUse, SessionStart and Stop. Claude Code sends the event to your command as JSON on stdin and acts on how it exits:
- Exit 0: carry on.
- Exit 2: block. For
PreToolUsethe tool call doesn’t run, and what you wrote to stderr is shown to Claude as the reason. - Any other code: a non-blocking error. The call goes ahead.
A hook is where command rules end up, because Bash deny rules match the command’s text. The permissions docs warn that “Bash permission patterns that try to constrain command arguments are fragile”: Bash(git push *) stops git push origin main but not git -C . push origin main. A hook can look at the whole command with whatever logic it needs.
A working hook: block force-pushes
This hook refuses git push --force and git push -f in any position, and still allows --force-with-lease. Save it as .claude/hooks/no-force-push.sh and make it executable:
#!/usr/bin/env bash
# Blocks git force-pushes. --force-with-lease is still allowed.
$ cmd=$(jq -r '.tool_input.command // ""')
$ if printf '%s' "$cmd" | grep -Eq 'git .*push' &&
$ printf '%s' "$cmd" | grep -Eq '(^| )(--force|-f)( |$)'; then
$ echo "Force-pushing is blocked in this repo. Use --force-with-lease, or push to a new branch." >&2
$ exit 2
$ fi
$ exit 0Register it for the Bash tool:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/no-force-push.sh"
}
]
}
]
}
}We tested the script by piping in the JSON a PreToolUse call sends. It blocked git push --force origin main, git push -f, git -C . push -f origin main and git status && git push origin feat --force, and let git push origin main, git push --force-with-lease, git commit and ls -f through. The stderr line is the reason Claude reads, so it says what to do instead.
Two limits, both worth knowing before you trust it:
- It reads text, too. It misses a force-push written as a refspec (
git push origin +main) or with combined flags (-uf). A hook narrows the gap a pattern leaves; it doesn’t close it. Branch protection on the server is the real guarantee. - A broken hook lets everything through. Our first test ran on a machine without
jq: the script exited 127, which isn’t 2, and every push went ahead without a word. Check the tools a hook depends on are installed wherever the project is used. The hooks reference also describes anonFailuresetting that makes a failing hook block.
Only command and http hooks are deterministic. A prompt or agent hook “asks another model to decide, so it isn’t deterministic either”, in the best practices’ words.
Hooks that ask instead of block
A hook doesn’t have to take the decision away from Claude. It can enforce the moment and leave the judgment to Claude, and that’s often the right split.
Memorinth’s own setup is the example. Asking agents in an instructions file to call wake_up at the start of a session wasn’t enough: the notes in its setup code say “instructions alone don’t make an agent use its memory”, because a session that starts with a task skips the call, and nothing tells the agent a session is ending. So memorinth agents on --harness claude-code installs two hooks in your user settings:
- A
SessionStarthook prints the briefing of current decisions, facts and recent memories, and Claude Code adds it to the context. Loading the briefing has no exceptions, so it moved out of the instructions. - A
Stophook counts file edits since anything was last saved. After three or more, it blocks the stop once, with a reason asking the agent to save what a future session would need. If nothing qualifies, the reason says, “just finish without mentioning this”. Whether something is worth keeping has exceptions, so the agent still decides. Whenstop_hook_activeis true, Claude is already continuing because of a stop hook, and Memorinth’s lets it stop rather than ask again.
What to save, and how to write a decision, stays as a few lines of advice. The hook only makes sure the question is asked.
Start with your longest-ignored line
Find the CLAUDE.md line you’ve corrected Claude on most often and ask whether it has exceptions. If it doesn’t, turn it into a deny rule or a hook, test the hook by piping in sample JSON, and delete the line. For sorting a whole file this way, see Your CLAUDE.md is too long. For what goes in instructions and what goes in memory, read What goes where, and for the hooks Memorinth installs, How agents use it and Give Claude Code a long-term memory.