Hooks allow you to run commands before or after Claude attempts to run a tool. They're incredibly useful for implementing automated workflows like running code formatters after file edits, executing tests when files change, or blocking access to specific files.
To understand hooks, let's first review the normal flow when you interact with Claude Code. When you ask Claude something, your query gets sent to the Claude model along with tool definitions. Claude might decide to use a tool by providing a formatted response, and then Claude Code executes that tool and returns the result.
Hooks insert themselves into this process, allowing you to execute code just before or just after the tool execution happens.
There are two types of hooks:
- PreToolUse hooks - Run before a tool is called
- PostToolUse hooks - Run after a tool is called
Hooks are defined in Claude settings files. You can add them to:
- Global -
~/.claude/settings.json(affects all projects) - Project -
.claude/settings.json(shared with team) - Project (not committed) -
.claude/settings.local.json(personal settings)
You can write hooks by hand in these files or use the /hooks command inside Claude Code.
PreToolUse hooks run before a tool is executed. They include a matcher that specifies which tool types to target:
"PreToolUse": [
{
"matcher": "Read",
"hooks": [
{
"type": "command",
"command": "node /home/hooks/read_hook.ts"
}
]
}
]Before the Read tool is executed, this configuration runs the specified command. Your command receives details about the tool call Claude wants to make, and you can:
- Allow the operation to proceed normally
- Block the tool call and send an error message back to Claude
PostToolUse hooks run after a tool has been executed. Here's an example that triggers after write, edit, or multi-edit operations:
"PostToolUse": [
{
"matcher": "Write|Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "node /home/hooks/edit_hook.ts"
}
]
}
]Since the tool call has already occurred, PostToolUse hooks can't block the operation. However, they can:
- Run follow-up operations (like formatting a file that was just edited)
- Provide additional feedback to Claude about the tool use
Here are some common ways to use hooks:
- Code formatting - Automatically format files after Claude edits them
- Testing - Run tests automatically when files are changed
- Access control - Block Claude from reading or editing specific files
- Code quality - Run linters or type checkers and provide feedback to Claude
- Logging - Track what files Claude accesses or modifies
- Validation - Check naming conventions or coding standards
The key insight is that hooks let you extend Claude Code's capabilities by integrating your own tools and processes into the workflow. PreToolUse hooks give you control over what Claude can do, while PostToolUse hooks let you enhance what Claude has done. By using hooks strategically, you can create a powerful, automated development environment that maintains code quality and enforces best practices without manual intervention.