Claude Code can run a command of yours before every tool call: a PreToolUse hook. It’s a great place for guardrails, but it’s easy to get subtly wrong. I built one that blocks secrets, and these are the lessons. (Check the current Claude Code hooks docs for exact field names, since they can change.)
How a hook works, in 30 seconds
Claude Code sends the tool call to your command as JSON on stdin, containing the tool name and its input. For a file write that includes the path and the content. Your command either lets it through or denies it.
Registering a hook looks like this in .claude/settings.json:
json
{ "hooks" : { "PreToolUse" : [ { "matcher" : "Write|Edit|MultiEdit|NotebookEdit|Bash" , "hooks" : [{ "type" : "command" , "command" : "your-hook" }] } ] } }
Enter fullscreen mode
Exit fullscreen mode
The matcher decides which tools trigger it.…