Original Reddit post

Been meaning to write this up for a while. If you’re on Claude Code and it’s ever done the exact thing you told it not to, hooks fix that, and I put off setting them up for months because I assumed it was config hell. It isn’t, it’s a shell script. Short version of what they are: Claude Code fires events at fixed points in its loop and you can attach a script to any of them. The one that matters most is PreToolUse , which runs before a tool call happens and can block it. Not discourage it, block it. Your script gets the tool name and the actual command on stdin as JSON, and you decide. What made it click for me is that the block isn’t silent. Whatever reason you hand back goes into Claude’s context, so it can act on it. Mine returns this when it catches npm in a pnpm repo: { “hookSpecificOutput”: { “hookEventName”: “PreToolUse”, “permissionDecision”: “deny”, “permissionDecisionReason”: “this repo is managed by pnpm (found pnpm-lock.yaml), re-run with pnpm” } } and it re-runs with pnpm on its own without me typing anything. If you don’t want to deal with the JSON you can just exit 2 and print the reason to stderr, same effect. The ones I actually run: secrets: blocks any command or file write with an API key or token pattern in it destructive: rm -rf / , force push to main, DROP DATABASE , the usual package manager: reads the lockfile, rejects the wrong one typecheck gate: this one’s a Stop hook rather than PreToolUse . runs tsc when the agent thinks it’s finished and blocks the handoff if there are errors The Stop hook is the one I’d push hardest right now, especially with Opus 5 dropping. Because the new model goes way further before it comes back for approval, there’s a lot more unreviewed work sitting there when it finally stops. Having it typecheck itself first catches a fair bit. One warning if you build one: a Stop hook that always blocks will loop forever, you need it to give up after the first round. I put mine up with the settings.json wiring, which is honestly fiddlier than the scripts themselves: https://techpotions.com/products/claude-code-hooks MIT, no signup. Anyone doing anything smarter with Stop hooks? Feels underused next to PreToolUse and I’ve barely scratched it. submitted by /u/techpotions

Originally posted by u/techpotions on r/ClaudeCode