Skip to main content
Hooks are event-driven automation triggers that execute actions automatically when Claude Code uses tools. They enable you to automate repetitive tasks, enforce standards, and create seamless workflows without manual intervention.

What Are Hooks?

Hooks are JSON configuration files (.json) stored in .claude/hooks/ that define:
  • Trigger events - When the hook should run (before/after tool use)
  • Matchers - Which tools trigger the hook (Edit, Bash, Write, etc.)
  • Actions - What to execute (commands, scripts, notifications)
  • Conditions - Optional filters for selective triggering
Hooks run automatically in the background. Claude Code executes them without requiring user interaction.

Hook Types

Claude Code supports two main hook types:
PreToolUse hooks run BEFORE Claude executes a tool.Use cases:
  • Validate operations before execution
  • Create backups before edits
  • Check permissions before dangerous operations
  • Block operations that violate policies

Hook Structure

Hooks are configured in settings files (.claude/settings.json or .claude/settings.local.json):

Hook Fields

Tool Matchers

Hooks can match specific Claude Code tools:
Use specific matchers to avoid unnecessary hook execution. Matching Edit won’t trigger on Bash commands.

Hook Categories

Automate git operations:
  • git-commit-formatter - Format commit messages automatically
  • prevent-force-push - Block dangerous git operations
  • auto-add-changes - Stage changes automatically
  • commit-msg-validator - Enforce commit message standards
Example:

Real-World Examples

Example 1: Build on Change Hook

File: Component downloaded to settings file
Behavior:
  • Triggers after every file edit
  • Detects build tool (npm, make, cargo, etc.)
  • Runs appropriate build command
  • Silent failures (|| true) prevent interruptions

Example 2: Git Commit Validator Hook

File: Component configuration
Behavior:
  • Runs before bash commands
  • Checks if command is git commit
  • Validates conventional commit format
  • Blocks commit if format is invalid

Example 3: Python Script Hook

Some hooks reference external Python scripts: Hook Configuration:
Supporting Script: .claude/hooks/custom-formatter.py The CLI automatically downloads supporting scripts when installing hooks.

Installing Hooks

Single Hook

You’ll be prompted to choose installation location:
  • User settings (~/.claude/settings.json) - All projects
  • Project settings (.claude/settings.json) - Shared with team
  • Local settings (.claude/settings.local.json) - Personal only

Multiple Hooks

With Category Prefix

Hooks merge with existing settings. Multiple hooks can trigger on the same tool.

Hook Execution Context

Hooks have access to context variables:

Environment Variables

Example usage:

Hook Best Practices

1. Silent Failures

Prevent hooks from interrupting workflow:
The || true ensures the hook never fails.

2. Conditional Execution

Only run when necessary:

3. Fast Operations

Hooks should be quick:

4. Idempotent Actions

Hooks should be safe to run multiple times:

5. Error Handling

Handle errors gracefully:

Hook Debugging

Debug hooks by adding logging:
Or use verbose output:

Disabling Hooks

Temporarily disable hooks:

Per-Session

Set environment variable:

Permanent

Remove hook configuration from settings file or comment out:

Hook vs Command

Hooks automate repetitive tasks. Commands handle complex workflows that need user input.

Advanced Hook Patterns

Chained Hooks

Multiple hooks on same matcher:
Hooks run sequentially in order.

Conditional Hooks

Use shell conditionals:
Only runs tests if file is a test file.

Hook with External Scripts

Call custom scripts:
Keep complex logic in external scripts for maintainability.

Common Hook Recipes

Auto-format Python

Run Tests on Change

Notify on Bash Execution

Next Steps

Browse Hooks

Explore 45+ available hooks

Create Custom Hooks

Build your own automation

Settings

Configure hook behavior

Commands

Learn about slash commands