Claude Code hooks | Blog

~/blog/tag/hooks

Hooks

The difference between asking the agent and making it so. What a hook is, when it beats a prompt, and where it bites back.

What I write about here ​

A hook is a script the harness runs at a fixed point in the agent's loop. Before a tool call, after an edit, when a session starts, when the model tries to stop. It runs whether or not the model was paying attention, and its exit code can refuse the thing that was about to happen.

That last part is the whole reason the tag exists. Almost everything else you can put in front of an agent is advice. A CLAUDE.md file is advice. A skill is advice with better packaging. A carefully worded instruction is advice you spent longer on. All of it is weighed by the model against everything else in the context, and any of it can lose.

A hook does not get weighed. This is the cheapest correctness win available in an agent setup, and it is the one people reach for last, usually after the third time the same rule got ignored on a long session.

The posts here cover both halves. What hooks are good for: enforcing a format, blocking a path, refusing a commit, keeping a check honest when the model would rather move on. And where they bite: a Stop hook that always blocks is an infinite loop, hooks over a certain size are silently skipped on some hosts, a hook that runs on every tool call is a tax you pay all day, and a hook that only watches Edit and Write never sees the agent topping up the same file through Bash.

If you are working out which mechanism fits a problem, the Agentic coding guide puts hooks next to skills, subagents and slash commands. What a hook may actually stop is a permissions question.

latest
hookspermissionstooling
9/10runs past the Edit|Write hook8 minClaude Pro #44

Your Claude Code file-protection hook doesn't see Bash

The PreToolUse hook in the docs protects files with an Edit|Write matcher. I gave Claude Code the same task ten times next to a protected test file, and nine times it wrote to the file anyway, with a heredoc in Bash. What held instead, and how to test your own hook.

read