Introduction to Claude Code Hooks
Learn how Claude Code hooks let you run custom code before or after any tool execution. Understand PreToolUse and PostToolUse hooks, configuration locations, and practical applications.
Hooks are one of Claude Code's most powerful features. They let you run custom commands automatically before or after Claude executes any tool — giving you fine-grained control over what Claude can do and what happens after it acts. Think of hooks as event listeners for Claude's tool system: code formatters that run after every edit, security checks that block access to sensitive files, or test runners that verify changes immediately.
How Hooks Fit Into the Tool Execution Flow
In a normal Claude Code interaction, your query goes to the Claude model along with available tool definitions. Claude decides which tool to use, Claude Code executes it, and the result is returned. Hooks insert themselves into this flow at two points:
- PreToolUse hooks — run before a tool is executed, with the power to block the operation entirely
- PostToolUse hooks — run after a tool has completed, with the ability to provide feedback or trigger follow-up actions
Where to Configure Hooks
Hooks are defined in Claude Code settings files. You can place them at three different scopes, depending on how broadly you want them to apply:
Hook Configuration Scopes
Global (~/.claude/settings.json)
- •Applies to all projects on your machine
- •Ideal for personal security policies and formatting preferences
Project (.claude/settings.json)
- •Shared with your team via version control
- •Great for project-wide linting, testing, and validation hooks
Personal (.claude/settings.local.json)
- •Not committed to source control
- •Perfect for individual preferences that should not affect teammates
Interactive hook setup
You can also create hooks interactively by running the /hooks command inside Claude Code, which walks you through the configuration process step by step.
PreToolUse Hooks: Control What Claude Can Do
PreToolUse hooks intercept tool calls before they execute. Each hook specifies a matcher (which tools to watch) and a command (what to run when triggered):
1"PreToolUse": [2 {3 "matcher": "Read",4 "hooks": [5 {6 "type": "command",7 "command": "node /home/hooks/read_hook.js"8 }9 ]10 }11]Your command receives details about the proposed tool call via standard input. Based on that data, you can either allow the operation to proceed (exit code 0) or block it entirely (exit code 2) with a feedback message sent to Claude.
PostToolUse Hooks: React to What Claude Did
1"PostToolUse": [2 {3 "matcher": "Write|Edit|MultiEdit",4 "hooks": [5 {6 "type": "command",7 "command": "node /home/hooks/format_hook.js"8 }9 ]10 }11]PostToolUse hooks cannot block operations (the tool has already run), but they can run follow-up actions like formatting the edited file, running tests, or providing additional feedback to Claude about the result.
Practical Hook Applications
- Code formatting — automatically format files after Claude edits them
- Test execution — run tests when files change to catch regressions immediately
- Access control — block Claude from reading or editing sensitive files like .env
- Code quality — run linters or type checkers and feed errors back to Claude
- Logging — track which files Claude accesses or modifies for audit purposes
- Naming validation — enforce naming conventions and coding standards
Key Takeaways
- 01Hooks run custom code before (PreToolUse) or after (PostToolUse) any Claude Code tool execution.
- 02PreToolUse hooks can block operations; PostToolUse hooks can run follow-up actions and provide feedback.
- 03Configure hooks at global, project, or personal scope depending on your needs.
- 04Common uses include formatting, testing, security enforcement, and code quality checks.