Claude CodeIntermediateClaude Code
PreToolUse hooks: a guard that always holds
An instruction in CLAUDE.md is followed almost always; a PreToolUse hook that exits with code 2 blocks the action every time.
A hook is tied to a lifecycle event, an optional matcher and a command. PreToolUse runs before the tool: if the script exits with code 2, the call is blocked and the reason (stderr) reaches Claude. PostToolUse runs afterwards, so it cannot block: use it to format, test or audit. Set them up with /hooks or in the settings.
Example
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read",
"hooks": [{ "type": "command", "command": "$CLAUDE_PROJECT_DIR/.claude/hooks/block-env.sh" }]
}
]
}
}
# block-env.sh: reads the call from stdin, and if the path is .env.production it writes the reason to stderr and exits with exit 2.