Claude Code hooks: how to intercept every tool call before it runs
Claude Code hooks: how to intercept every tool call before it runs One of the most powerful — and least documented — features revealed in the Claude Code source is the hooks system. You can interce...

Source: DEV Community
Claude Code hooks: how to intercept every tool call before it runs One of the most powerful — and least documented — features revealed in the Claude Code source is the hooks system. You can intercept every single tool call Claude makes, before it executes. This means you can: Auto-approve certain commands (no more hitting Enter 40 times) Block dangerous operations entirely Log every file Claude touches Inject context before tool execution Here's how it works. The hooks directory Create a .claude/hooks/ directory in your project: mkdir -p .claude/hooks Hooks are shell scripts that Claude executes at specific lifecycle points. PreToolUse hook This runs before any tool call. The tool name and arguments are passed as environment variables: # .claude/hooks/PreToolUse.sh #!/bin/bash # Auto-approve file reads — stop asking me every time if [ "$TOOL_NAME" = "Read" ]; then exit 0 # 0 = approve fi # Block rm -rf entirely if [ "$TOOL_NAME" = "Bash" ] && echo "$TOOL_INPUT" | grep -q 'rm -r