DEV Community

Cover image for Claude Code hooks: I wired 5 and 2 never fired
Agent Shipped
Agent Shipped

Posted on

Claude Code hooks: I wired 5 and 2 never fired

I wired five Claude Code hooks on a demo repo: three of three reads of a secrets file were blocked, and two hooks stayed at zero until I fixed one matcher each.

GitHub logo agentshipped / notes-api

Agent Shipped demo repo (notes-api): the public playground used in the channel's videos.

notes-api

A tiny Express notes API. This is a public demo repo for the Agent Shipped YouTube channel techniques from real projects are shown here, on code anyone can clone.

npm install
npm start          # http://localhost:3000/notes
npm test           # node:test (no tests yet - that is on purpose)
Enter fullscreen mode Exit fullscreen mode

Endpoints: GET /health, GET /notes, GET /notes/:id, POST /notes {title, body}, DELETE /notes/:id.

Shown in

Videos on the Agent Shipped channel that use this repo:

A hook that never matches prints no error, so the settings file cannot tell you which hooks will fire. Counting what each hook did found the two silent ones.

A guard hook with a log

A hook is a command that Claude Code runs around a tool call. You register it in .claude/settings.json under an event name, with a matcher that picks the tools it listens to. Claude Code passes the command a JSON object on standard input with the tool name and the tool input.

The first hook is a guard that listens before every Bash and Read call. The script reads stdin with jq, takes tool_input.command if it exists, otherwise tool_input.file_path, and calls that the target. If the target mentions .env, it exits with code 2 and writes one sentence to stderr. That is the whole blocking mechanism. The agent gets the sentence as the reason the call failed.

The script also appends one line to a log on every call, so you can count what it did instead of trusting it. I told the agent not to run the script, because a test run would add extra lines.

A secret to protect

There is no real secret in the repo. The terminal builds a .env with a random token at run time:

printf 'TOKEN=%s\n' "$(openssl rand -hex 18)" > .env && echo 'created .env' && cat .claude/hooks/guard.sh
Enter fullscreen mode Exit fullscreen mode

The script parses with jq, picks the target, logs one line, then decides. A match writes the blocked line, speaks on stderr and exits 2. Anything else exits 0.

Three ways in

In a fresh session, so the hook is loaded, I asked for exactly three attempts at the same file, with no other way around a block:

  1. The Read tool on .env.
  2. The Bash tool running cat .env.
  3. The Bash tool running a grep that would print the token line.

One matcher, Bash|Read, has to catch three routes. Each attempt came back with the stderr sentence, and no file content appeared.

Count the blocks

grep -c '^blocked' .claude/hooks.log
Enter fullscreen mode Exit fullscreen mode

The number is 3: three attempts, three blocked.

A terminal showing the grep count of lines starting with blocked in the hooks log, with the result 3.
The grep count of blocked lines: 3 of 3 attempts. · watch this step

A guard that works is the easy half of the story.

Four more hooks, two planted wrong

The second prompt asked for four more scripts, registered next to the guard: a syntax check, a stop marker, an audit log and a change log. Each appends one line with its own label to the same log, which makes it countable.

I planted two mistakes and told the agent to write the matchers exactly as given. The audit hook got bash in lower case. The change hook got Write. A model that tidies a typo would hide the lesson.

The syntax hook runs after Edit or Write and calls node --check. The stop hook has no matcher, because the Stop event fires when the agent finishes its turn and there is no tool to match.

What is registered

jq .hooks .claude/settings.json
Enter fullscreen mode Exit fullscreen mode

PreToolUse holds the guard on Bash|Read and the audit hook on bash. PostToolUse holds the syntax hook on Edit|Write and the change hook on Write. Stop holds one entry with no matcher. The paths are right and the JSON is valid. Nothing looks wrong, and that is the trap.

A real edit

I asked the agent to edit an existing file with the Edit tool, not rewrite it: change not found to note not found, then run node --check src/index.js. Silence means the file still parses. The screen only shows the edit, not which hooks ran.

The count

One count per label, zeros included:

for h in guard blocked syntax stop audit changed; do printf '%s %s\n' "$(grep -c "^$h" .claude/hooks.log)" "$h"; done
Enter fullscreen mode Exit fullscreen mode

A terminal listing one count per hook label, with blocked at 3 and audit and changed at 0.
Look at the two zeros: audit and changed. Blocked is 3. · watch this step

blocked is still 3. guard, syntax and stop are above zero. audit and changed are 0, each for its own reason.

  • audit: matchers are case sensitive. The tool is Bash and the matcher says bash. No error, no warning.
  • changed: the agent never used Write. Changing an existing file is an Edit call, and Write is for creating a file or replacing it whole. The syntax hook has Edit in its matcher, which is why it has a count.

The one-line fix

Two substitutions in the settings file, then a list of every matcher:

sed -i -E 's/("matcher" *: *)"bash"/\1"Bash"/; s/("matcher" *: *)"Write"/\1"Edit|Write"/' .claude/settings.json && jq -c '[.hooks[][].matcher]' .claude/settings.json
Enter fullscreen mode Exit fullscreen mode

A terminal showing the compact list of matchers in the settings file after the sed fix.
The matcher list after the fix; the Stop entry shows null. · watch this step

The pattern only touches a matcher whose whole value is that word, so the syntax hook stays as it was. The scripts are untouched: the only wrong thing was the line that tells Claude Code when to call them. The Stop entry shows null because it has no matcher by design.

Edit once more

Same kind of task in a fresh session, so the fixed settings load: change note not found to Note not found in the same file, then run node --check src/index.js. Only the two matchers differ.

The verdict

The same count loop again:

A terminal listing one count per hook label after the fix, with audit and changed above zero.
Same count loop after the fix: audit and changed are no longer zero. · watch this step

audit and changed are above zero. blocked is still 3, because nothing tried to read the secrets file this time. All five hooks have now run.

What to copy

  • Give every hook script a one-line log with its own label, then print a count per hook. Trust that number, not the settings file.
  • Blocking is exit code 2 plus one sentence on stderr.
  • Write matchers as exact tool names: Bash, not bash.
  • Match the tool the agent really uses: Edit|Write for a change hook.
  • Do not switch a hook off to make a demo pass. Fix the line that decides when it runs.

Not just JavaScript

The same hook contract works in any stack. In a Python repo, use python -m py_compile instead of node --check, and ruff or mypy instead of a JS linter. The guard reads the same stdin JSON.

Video with the full run: https://youtu.be/aR5c3T0jqvc

Techniques from my real projects, shown on a public demo repo.
Narration: AI voice. The sessions, code and errors are real. Not affiliated with Anthropic or any tool vendor.

Top comments (1)

Collapse
 
salparvez profile image
Sal Parvez | ML Systems RI •

"Trust that number, not the settings file" is the line to keep. A guard that never fires looks identical to a guard that never needed to, and the config can't tell you which one you have. The job-site version is a smoke detector with no test button: installed, inspected, and silent either way. Counting what each hook did is the test button. The case-sensitive matcher (Bash, not bash) failing with no error is something I'd want surfaced at load time. Did you find any way to make an unmatched hook warn, or is the per-hook count the only signal today?