A rule in AGENTS.md says "never edit .env." The agent reads it, agrees, and then edits .env anyway, because a rule is a suggestion the model can weigh against the task in front of it. A hook is different: it is your script, running outside the model, that can stop the action before it happens.
Short answer: Claude Code hooks are user-defined handlers that run at fixed points in a session — before a tool call, after a tool call, when a prompt is submitted, when the session starts or ends. You configure them in settings.json, they receive JSON about the event on stdin, and a hook that exits with code 2 blocks the action. Use them for the rules you cannot afford to leave to the model's judgment: blocking dangerous commands, formatting on write, logging every tool call.
What is a hook, exactly?
A hook is a handler — usually a shell command — that Claude Code runs when a specific event fires and a matcher matches. The model does not decide whether to run it. Claude Code does, deterministically, every time the event occurs (hooks reference).
That is the whole point. An instruction in a rules file is probabilistic: the model usually follows it. A hook is mechanical: it runs or it doesn't, and when it exits 2 on a blocking event, the action stops regardless of what the model wanted.
Which events can a hook fire on?
Hooks cover the whole session lifecycle, and the matcher means something different per event (matcher reference):
| Event | Fires when | Matcher filters |
|---|---|---|
PreToolUse |
Before a tool runs (can block) | Tool name: Bash, Edit|Write, mcp__.* |
PostToolUse |
After a tool succeeds | Tool name |
UserPromptSubmit |
You submit a prompt (can block) | — |
SessionStart |
Session begins | startup, resume, clear, compact |
SessionEnd |
Session ends | clear, logout, other |
PreCompact / PostCompact |
Around compaction | manual, auto |
SubagentStart / SubagentStop |
A subagent runs | Agent type |
Notification |
Claude needs attention | permission_prompt, idle_prompt |
The two you will reach for first are PreToolUse (gate an action before it happens) and PostToolUse (react after it succeeds). The rest are for logging, setup, and cleanup.
How do you configure one?
Hooks live in settings.json, under a hooks key keyed by event. Each event holds a list of matcher groups, and each group holds handlers:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "~/.claude/hooks/block-rm.sh" }
]
}
]
}
}
The handler receives JSON on stdin describing the event — for a Bash call, that includes tool_input.command. Your script reads it, decides, and signals with its exit code.
What does exit code 2 do?
Exit code 2 is how a hook says "stop, don't do this." On a blocking event, Claude Code halts the action and feeds your stderr back to the model as the reason (exit-code behavior). A PreToolUse hook blocks the tool call; a UserPromptSubmit hook rejects the prompt.
Here is a complete hook that blocks rm and leaves everything else to the normal permission flow:
#!/bin/bash
input=$(cat)
command=$(jq -r '.tool_input.command' <<<"$input")
if [[ "$command" =~ rm[[:space:]] ]]; then
echo "Blocked: rm is not allowed. Move files to a trash dir instead." >&2
exit 2
fi
exit 0
Exit 0 lets the action proceed. Exit 2 blocks it. Any other non-zero code is a non-blocking error: the action continues and the failure is logged. That distinction matters — a buggy hook that exits 1 does not protect you, it just complains.
Four recipes worth pasting
- Block dangerous commands —
PreToolUseonBash, exit 2 onrm -rf,git push --force, or reads of.env. - Format on write —
PostToolUseonEdit|Write, run your formatter on the changed file so the agent never leaves unformatted code. - Log every tool call —
PostToolUsewith a broad matcher, append the event JSON to a file you can audit later. - Inject context —
UserPromptSubmit, prepend a reminder or the current branch name to every prompt.
Where hooks fall short
Hooks are powerful and easy to get wrong:
- They run with your permissions. A hook is your shell script, with your access. A careless hook is a foot-gun, not a sandbox.
- A slow hook slows every matching action. A
PreToolUsehook onBashruns before every command. Keep it fast; a network call there adds latency to everything. - Exit 1 is not exit 2. Only code 2 blocks. A hook that crashes with code 1 lets the action through. Test the failure path.
- They are deterministic, not smart. A regex that blocks
rmalso blocksnpm run formatif you write it loosely. Hooks enforce patterns, not intent. - Config is strict JSON. A trailing comma in
settings.jsonis a syntax error that Claude Code reports at the next start.
For rules that should guide rather than hard-block, a lean AGENTS.md is still the right tool; hooks are for the lines you draw in permanent marker. And because every tool call carries context, a hook that injects text on every prompt has a token cost — see our token cost breakdown.
How we use hooks at Muvon
We keep the model's instructions in AGENTS.md and the hard stops in hooks: a PreToolUse gate on destructive shell commands, and a PostToolUse formatter so generated code matches the repo before it lands. For the rules that should improve over time rather than stay fixed, Besydr learns lessons from finished sessions and injects the relevant ones per request. Hooks enforce today's rules; memory grows tomorrow's.
The takeaway: put suggestions in a rules file, and put the rules you cannot break in a hook that exits 2.



