Claude Code hooks: three real examples that block an agent's shortcuts
A hook is a command that Claude Code itself runs at a precise moment: before a tool call, at the end of a session. If the script exits with code 2, the action is blocked and the error message goes back to the model. I use three: one that forbids --no-verify, one that forbids disabling a test, one that prevents a session from ending with a red lint.
The problem: an instruction is not a guarantee
My CLAUDE.md has always said never to add an AI mention to a commit message. On one of my projects, an agent still added a Co-Authored-By line to four commits in a row before I noticed. It had not ignored the rule out of spite: another instruction, closer to it, said the opposite, and that one won.
That is the limit of any rule written in natural language. The model weighs it against the rest of its context. Most of the time it follows the rule, and the day it does not, nothing warns you.
A hook is not up for negotiation. The model does not run it, Claude Code does. So the rule also applies on the days the agent is in a hurry to finish.
How a hook works
Hooks are declared in a settings file: .claude/settings.json for one project, ~/.claude/settings.json for all your projects, or hooks/hooks.json inside a plugin. Each hook is attached to an event. The two I use:
- `PreToolUse` fires before a tool call. A
matcherpicks the tools it applies to, for exampleBashorEdit|Write. The script receives a JSON on standard input that describes the call: the command forBash, the file path and the text forEditandWrite. - `Stop` fires when Claude wants to finish its answer.
The exit code decides what happens next. With 0, everything carries on as usual. With 2, the action is blocked: the tool call does not happen, or Claude is not allowed to stop. Whatever the script wrote to standard error is sent back to the model, which then knows why it was blocked and what to do instead.
The /hooks command lists the active hooks and where each one comes from. It is the first thing to check when a hook does not seem to fire.
My three hooks
They live in the plugin that carries my agent chain, the one I describe in the article on tests written before the code. Here is the declaration, as is:
{
"hooks": {
"PreToolUse": [
{ "matcher": "Bash",
"hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/hooks/deny-no-verify.sh" }] },
{ "matcher": "Edit|Write|MultiEdit",
"hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/hooks/guard-test-integrity.sh" }] }
],
"Stop": [
{ "hooks": [{ "type": "command", "command": "\"${CLAUDE_PLUGIN_ROOT}\"/hooks/stop-require-green.sh", "timeout": 600 }] }
]
}
}- `deny-no-verify` reads every shell command before it runs. It blocks
git commit --no-verify, its short form-n,HUSKY=0and any override ofcore.hooksPath. Since the four-commit episode, it also blocks any commit whose message carries an AI mention. - `guard-test-integrity` reads every file edit. If it adds a
skip, anonlyor anxitto a test file, it is refused. Same for an edit to the coverage thresholds. The message sent back to the agent is clear: fix the code, or stop and flag the test. - `stop-require-green` fires at the end of a session. If source files changed, it runs the project's lint. Red lint: Claude is not allowed to stop and receives the last thirty lines of the error.
The timeout is in seconds. I set it to 600 on the end-of-session hook, because a full lint can be slow on a large project.
What went wrong, and the limits
- False positives. A first version simply looked for
--no-verifyin the command. So it also blocked agrepsearching for that text in the docs, and it let the short formgit commit -nthrough. That one had to be blocked without blockinggit log -n 3. The script now splits the command into segments and only looks at realgitinvocations. - Count instead of forbid. Some tests have a legitimate conditional
skip. So the hook compares the number of markers before and after the edit: it refuses an addition, not what already exists. - The endless loop. A
Stophook that blocks sends Claude back to work, Claude will want to stop again, and the hook fires again. The hook input contains astop_hook_activefield that istruein that case. My script reads it first and lets the stop through. - Lint only, not tests. In my chain, red tests along the way are normal: they have just been written and the code does not exist yet. Requiring green tests at every stop would have broken the method.
- The blind spot. The hook on
EditandWritedoes not see a file rewritten by a shell command, withsedfor instance. I do not pretend otherwise: the reviewer agent covers that case. - Stay in your lane. A plugin's hooks apply wherever the plugin is enabled. Mine all start by checking that a
.claude/pipeline.jsonfile exists in the project. Otherwise they exit without doing anything.
One last point: a hook is code, so it gets tested. Mine have a 36-case script that feeds them fake tool calls and checks the exit code. A hook that blocks wrongly is worse than no hook: you end up disabling it.
How to start
Start with a single hook, on the rule your agent has already broken. To forbid --no-verify, you need two files. The declaration, in .claude/settings.json:
{
"hooks": {
"PreToolUse": [
{ "matcher": "Bash",
"hooks": [{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/deny-no-verify.sh" }] }
]
}
}And the script, in .claude/hooks/deny-no-verify.sh, made executable. It needs jq to read the JSON:
#!/usr/bin/env bash
cmd="$(jq -r '.tool_input.command // ""')"
if printf '%s' "$cmd" | grep -Eq '(^|[[:space:]])--no-verify([[:space:]]|$)'; then
echo "Blocked: --no-verify bypasses the git hooks." >&2
exit 2
fi
exit 0This simple version has the false positive described above. It is enough to begin with. Refine it the day it gets in your way, and write a test for every case you fix.
The rule I apply to choose: what calls for judgment stays in CLAUDE.md, what must never happen becomes a hook. What I keep in that file, and how I write it, is covered in the article on CLAUDE.md best practices.
These hooks matter even more when nobody is watching, for instance in scheduled tasks that run every morning.
Article written with the help of AI, from my daily practice and my projects.
The whole notebook