Claude Code Hooks: Guardrails That Actually Hold
What are Claude Code hooks? They’re your own scripts that Claude Code runs at fixed points in a session: before a tool call (PreToolUse), after one (PostToolUse), when you submit a prompt, when Claude stops, and about thirty other events. A command hook that exits with code 2 blocks the action and hands its stderr to Claude as the reason. That’s from the Claude Code hooks reference, checked November 26, 2026.
Your CLAUDE.md is a request. A hook is a rule. That difference is the whole reason to bother with them.
I learned it the boring way. A founder we work with had a line in CLAUDE.md that said “never run migrations against production”. Three weeks later Claude, trying to fix a failing signup, ran supabase db push with the linked project still pointing at prod. It even apologised afterwards, quite nicely. The migration added a NOT NULL column with no default and every insert into profiles failed for about forty minutes on a Saturday. The instruction was in the file. It just wasn’t enforced by anything.
Where hooks live
Hooks go in settings files, in a hooks block. The ones you care about for a project are .claude/settings.json (committed, so the whole team gets them) and .claude/settings.local.json (yours only). There’s also ~/.claude/settings.json for every project on your machine, plus managed settings for companies, plugins and skills.
The shape is event, then a matcher, then the handlers:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/guard-bash.sh"
}
]
}
]
}
}
The matcher is a tool name (Bash, Edit|Write) or * for everything. Each handler also takes an if field in permission-rule syntax, like "if": "Bash(rm *)", so a script only runs for matching commands. Handler types are command, http, mcp_tool, prompt and an experimental agent type. For guardrails, use command. It’s a shell script, it’s fast, and you can read exactly what it does.
Type /hooks in a session to see what’s loaded and which file each one came from. It’s read-only, so you still edit the JSON by hand.
Exit codes, and the one mistake everyone makes
The script gets the event as JSON on stdin. For PreToolUse that includes tool_name and tool_input, and for Bash the tool_input.command string is the thing you’ll grep.
- Exit
0: fine, carry on. If stdout is a JSON object, Claude Code reads it as a decision. - Exit
2: blocked. Claude sees your stderr and usually tries another way. - Any other exit code, including
1: a non-blocking error. You get a “hook error” notice and the action goes ahead anyway.
That last one bites. People write a guard in bash, end it with exit 1 because that’s what “failure” means everywhere else, test it, see the error message, and think it works. It doesn’t block anything. The docs say it straight: if a hook is meant to enforce a policy, use exit 2.
Timeouts are the other trap. Command hooks get 600 seconds by default, and a PreToolUse hook that times out does not block the call unless you set "onFailure": "block" on it. If your guard calls an API that sometimes hangs, set that.

Three hooks I’d put on any AI-built app
1. Stop writes to the production database
This is the one from the Saturday story. It reads the command and refuses anything that pushes migrations or resets a database unless you’re pointed at a branch:
#!/usr/bin/env bash
cmd=$(jq -r '.tool_input.command // ""')
if echo "$cmd" | grep -Eq 'supabase (db push|db reset)|psql .*prod|DROP TABLE'; then
echo "Blocked: migrations go through a pull request and CI, not from this session. Write the migration file and stop." >&2
exit 2
fi
exit 0
Write the stderr message for Claude, not for you. “Blocked” on its own makes it try a creative workaround. “Write the migration file and stop” tells it what to do instead, and in my experience it does.
2. Keep secrets out of the repo
A PreToolUse hook on Edit|Write that rejects writes to .env* files, or content matching sk_live_ or a Supabase service_role key pattern. Lovable and Claude Code both love to “helpfully” paste a key into a config file to get a failing call working. One grep stops it.
3. Run the tests before Claude says it’s done
A Stop hook that runs npm test (or just the fast suite) and returns "decision": "block" with the failure output as the reason. Claude keeps working instead of announcing success on red tests. Keep this one quick. If your suite takes four minutes, every “done” takes four minutes, and you’ll switch it off by Thursday.
What hooks are not
They aren’t a security boundary on their own, and the docs are honest about that. The if filter is best-effort, and the reference says to use the permission system rather than a hook for a hard allow or deny. A hook that returns "permissionDecision": "allow" still can’t override a deny rule in your settings, which is good: put the absolute bans in permissions.deny and use hooks for the judgment calls, like “migrations only on a branch”.
Hooks also run with your user’s full permissions. A hook script from a repo you just cloned is code you’re about to execute, so read it. Project hooks only run after you accept the workspace trust prompt, and that prompt deserves more attention than people give it.
And a PostToolUse hook can’t undo anything. By the time it fires, the file is written and the command has run. If you want to stop something, it has to be PreToolUse.
The setup we actually ship
For founders who build with Claude Code and want a developer watching the parts that can hurt them, this is part of our AI-ready setup from $600: the hooks above, deny rules, a CLAUDE.md written for your app, CI that runs tests and a security scan on every pull request, and Supabase with a staging branch so a bad migration lands somewhere harmless. After that, the guardrail retainer at $500, $1,500 or $3,000 a month means a person reviews every AI-written pull request within a working day.
If the app is already live and wobbling, taking a Claude Code or Cursor app to production starts from $500. If you’re still deciding what this all costs per month, Claude Code pricing has the numbers, and plan mode is the other setting worth turning on today. Or just tell us what you’re building.