Hooks are your commands that run automatically when Claude does something.
Why this matters: You can tell Claude "always format code after editing"—but it might forget. A hook guarantees it happens every time, because it's your code running automatically, not Claude choosing to run it.
Without hooks, you hope Claude remembers to:
With hooks, you guarantee these happen:
The key insight: By encoding rules as hooks instead of prompting instructions, you turn suggestions into app-level code that executes every time.
There are also advanced events (Stop, SubagentStop, PermissionRequest, Notification) for specialized workflows.
The pattern:
Exit codes matter:
Run:
This opens an interactive menu where you:
Hooks are configured in .claude/settings.json:
Key fields:
Let's log every Bash command Claude runs.
Prerequisite: Install jq for JSON processing (brew install jq on macOS, apt install jq on Linux).
Run /hooks in Claude Code
Select PreToolUse
Add matcher: Bash
Add hook command:
Choose User settings for storage
Press Esc to save
Now ask Claude to run ls and check your log:
Add to .claude/settings.json:
Restart Claude Code and test it.
Here's a real hook that tracks prompts (from this book's codebase):
Script (.claude/hooks/track-prompt.sh):
Configuration:
What happens:
Track when skills are invoked:
Configuration:
What this does:
Track subagent results:
Configuration:
What this does:
All hooks receive JSON via stdin. Common fields:
Event-specific fields:
Simple: Just print text to stdout:
Advanced: Output JSON for more control:
Block an action:
You can have multiple hooks for the same event:
Different matchers trigger different scripts based on which tool is used.
If hooks aren't working:
Tip: Use a _common.sh file for shared functions like JSON parsing.
📝 Create a Simple Hook:
"Help me create a SessionStart hook that shows the git branch and last commit message when I start Claude Code. Walk me through: the script, the settings.json config, and how to test it."
What you're learning: The complete hook lifecycle—from script to configuration to testing. This pattern applies to all hook types.
🔍 Understand Hook Events:
"I want to automatically run prettier after Claude edits a JavaScript file. Which hook event should I use? What would the matcher be? Show me the complete configuration."
What you're learning: Event selection and pattern matching—choosing the right trigger and scope for automated behavior.
🛡️ Validation Hook:
"Help me create a PreToolUse hook that warns me before Claude runs any command with 'rm' or 'delete' in it. The hook should print a warning but not block the command."
What you're learning: Safety guardrails through hooks—implementing "soft" warnings that inform without blocking, a pattern used in production systems.
📊 Logging Hook:
"I want to log all the tools Claude uses during a session. Help me create a PostToolUse hook that appends tool names and timestamps to a log file."
What you're learning: Observability through hooks—instrumenting AI behavior for debugging and analysis. This is how production systems gain visibility.
🔧 Debug a Hook:
"My hook isn't running. Help me debug: How do I test the script manually? How do I check if settings.json is correct? What does claude --debug show?"
What you're learning: Hook debugging methodology—the systematic approach when automation doesn't work. This skill saves significant debugging time.