How-To Guide

How to Use Hooks in Claude Code

Set up Claude Code hooks to run custom scripts before or after tool calls. Automate linting, testing, and validation.

How to Use Hooks in Claude Code

1

Understand hook types

Hooks run shell commands at specific points: 'PreToolUse' (before a tool runs), 'PostToolUse' (after a tool runs), 'Notification' (when Claude sends a notification), and 'Stop' (when Claude finishes a turn).

2

Add hooks to your settings

Add hook configurations to `.claude/settings.json`. Each hook specifies a matcher (which tool to hook into) and a command to run.

3

Create a pre-commit hook example

Hook into 'PostToolUse' with matcher 'Write' to run your linter after every file write: `{"hooks": {"PostToolUse": [{"matcher": "Write", "command": "eslint --fix $FILE_PATH"}]}}`

4

Test your hooks

Make a change that triggers your hook and verify it runs correctly. Hook output is shown in the Claude Code interface.

Pro tips

  • *

    Use hooks for automated linting, formatting, and test running after edits

  • *

    Hook commands can block Claude's action — use 'PreToolUse' hooks as guardrails

  • *

    Keep hook commands fast (under 5 seconds) to avoid slowing down the workflow

More Claude Code guides

Explore more