Claude Code Hooks Quick Start
Claude Code hooks are user-defined shell commands that execute automatically at specific points during Claude Code’s operation, providing deterministic control over behavior.
What are Hooks?
Hooks allow you to:
- Auto-format code after edits (prettier, black, etc.)
- Validate changes before they’re applied
- Send notifications when tasks complete
- Enforce security policies on file operations
- Integrate with external tools (linters, tests, etc.)
Quick Start
1. Create Configuration File
Create .claude/settings.json in your project:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "prettier --write $CLAUDE_FILE_PATHS"
}
]
}
]
}
}2. Test Your Hooks
# View current hooks configuration
/hooks
# Ask Claude to edit a file to see hooks in action3. Common Hook Patterns
Auto-formatting
{
"PostToolUse": [{
"matcher": "Edit|Write",
"hooks": [{
"command": "prettier --write $CLAUDE_FILE_PATHS"
}]
}]
}Security Check
{
"PreToolUse": [{
"matcher": ".*",
"hooks": [{
"command": "[[ \"$CLAUDE_FILE_PATHS\" =~ \\.env ]] && exit 2 || exit 0"
}]
}]
}Task Notifications
{
"Stop": [{
"hooks": [{
"command": "notify-send 'Claude Code' 'Task completed'"
}]
}]
}Key Concepts
Hook Events
- PreToolUse: Before tool execution (can block with exit code 2)
- PostToolUse: After successful tool execution
- Stop: When main agent finishes
- UserPromptSubmit: When user submits a prompt
Environment Variables
$CLAUDE_TOOL_NAME- The tool being used (Edit, Write, etc.)$CLAUDE_FILE_PATHS- Files being modified$CLAUDE_TOOL_INPUT- Tool input as JSON
Exit Codes
0- Success, continue normally2- Block operation (PreToolUse only)- Other - Warning, but continue
Hook Execution Lifecycle
Understanding the hook lifecycle is crucial for debugging and creating custom hooks. The process follows a clear sequence from registration to execution.
graph TD A[1. Hook Registration] --> B{2. Event Trigger}; B --> C[3. Hook Queue Processing]; C --> D{4. Middleware Execution}; D --> E[5. Data Transformation]; E --> F[6. Final Output];
Execution Steps:
-
Registration: Hooks are registered with the
HookManagerduring application startup. The system reads.claude/settings.jsonand validates all hook configurations. See Hook Design Patterns for examples. -
Event Trigger: An event occurs during Claude Code’s operation:
- A tool like
EditorWriteis about to execute (PreToolUse) - A tool has completed successfully (
PostToolUse) - The main agent completes its task (
Stop) - User submits a new prompt (
UserPromptSubmit)
- A tool like
-
Queue Processing: The
HookManagergathers all registered hooks for the triggered event:- Filters hooks by their matcher patterns
- Sorts hooks by priority (if specified)
- Prepares the execution environment with variables
-
Middleware Execution: Hooks execute sequentially:
- Each hook runs as a separate shell process
- Exit codes determine flow control
- Output is captured for debugging
-
Data Transformation: Hook outputs can modify Claude Code’s behavior:
- PreToolUse hooks can block operations (exit code 2)
- PostToolUse hooks can trigger additional actions
- Environment variables pass data between hooks
-
Final Output: The hook chain completes and Claude Code continues:
- Success: Normal operation proceeds
- Blocked: Operation is cancelled (PreToolUse only)
- Warning: Operation continues with logged warnings
Debugging Hook Execution
To trace hook execution, use the /hooks command to see your current configuration, or check the Claude Code logs for detailed execution traces. Each hook execution includes timing information and exit codes.
Example Flow: When you edit a TypeScript file:
- Claude Code triggers
PreToolUseevent - Security hook checks file permissions → Exit 0 (continue)
- Edit operation executes
- Claude Code triggers
PostToolUseevent - Prettier formats the file → Exit 0 (success)
- ESLint checks for issues → Exit 0 (success)
- File is saved with all transformations applied
Complete Reference
For detailed configuration options, all hook events, advanced patterns, and troubleshooting:
📖 Complete Configuration Reference
Next Steps
- Browse Hook Patterns - Production-ready hook examples
- Follow Tutorials - Step-by-step guides
- Troubleshooting Guide - Common issues and solutions
- Security Guide - Best practices for secure hooks