Claude Code Hooks: A Beginner's Guide with 3 Examples

Maximin
Published
4 min read
4 min read

Key takeaways

  • A hook is a command Claude Code runs at a set moment, like after each file edit or before each shell command. It always runs: Claude doesn't choose.
  • Put hooks in a settings file: .claude/settings.json for one project (shareable), or ~/.claude/settings.json for all your projects.
  • The most useful events: PostToolUse (after a tool), PreToolUse (before a tool, can block it), Notification, Stop, and SessionStart.
  • To block an action, the hook exits with code 2, and what it writes to stderr tells Claude why.
  • Type `/hooks` in Claude Code to see every hook and where it comes from.

Make Claude Code work your way

The free guide covers the settings that change everything, with every prompt ready to copy.

Get the free guide

What are Claude Code hooks?

Hooks are your own commands that Claude Code runs at specific points: when a session starts, before or after it uses a tool, when it needs your attention, when it finishes. The difference with CLAUDE.md is control. A rule in CLAUDE.md is advice Claude may forget. A hook runs every time.

Use a hook for anything that must happen: format the code, run a check, block a command, send a notification.

Claude Code hook events

Claude Code has more than 30 events. These are the ones you'll use first:

EventWhen it runsCan block?
SessionStartWhen a session starts or resumesNo
UserPromptSubmitWhen you send a prompt, before Claude reads itYes
PreToolUseBefore a tool runs (Bash, Edit, Write…)Yes
PostToolUseAfter a tool succeedsNo, it already ran
NotificationWhen Claude Code needs your attentionNo
StopWhen Claude finishes its answerYes
SubagentStopWhen a subagent finishesYes
PreCompactBefore the conversation is compactedYes
The most useful hook events, from the official docs.

Where to put your hooks

FileApplies toShared?
~/.claude/settings.jsonAll your projectsNo
.claude/settings.jsonThis projectYes, commit it
.claude/settings.local.jsonThis project, only youNo
Hook locations.

Each hook has three levels: the event (PostToolUse), a matcher that picks the tool (Edit|Write), and the command to run. If the file already has a hooks key, add your event next to the others instead of replacing it.

3 Claude Code hook examples

1. Format every file Claude edits

This runs Prettier on each file after Claude edits or writes it. Put it in .claude/settings.json:

.claude/settings.json
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path' | xargs npx prettier --write"
          }
        ]
      }
    ]
  }
}

Claude Code sends the hook a JSON description of the tool call on stdin. Here, jq pulls out the file path and passes it to Prettier.

2. Get a notification when Claude needs you

This shows a Mac notification each time Claude Code waits for you. Put it in ~/.claude/settings.json so it works everywhere:

~/.claude/settings.json
{
  "hooks": {
    "Notification": [
      {
        "matcher": "",
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
          }
        ]
      }
    ]
  }
}

On Windows, the docs give a PowerShell version that opens a message box instead.

3. Block a dangerous command

A PreToolUse hook on Bash can stop a command before it runs. This script, from the docs, blocks any command that starts with rm:

.claude/hooks/check.sh
#!/bin/bash
input=$(cat)
command=$(jq -r '.tool_input.command' <<<"$input")

if [[ "$command" == rm* ]]; then
  echo "Blocked: rm commands are not allowed" >&2
  exit 2
fi

exit 0

Exit code 2 blocks the action and sends your message to Claude. Exit code 0 lets the normal flow continue. Any other code is an error that doesn't block, so a policy hook must use 2.

Get the free Claude Code guide

The install and the 5 settings to know, step by step, with every prompt ready to copy.

Download the guide

Tips for writing safe hooks

  • Hooks run with your permissions. Read any hook before you add it, especially from a shared project.
  • For a hard allow or deny, use Claude Code's permission settings. Hooks are best for checks and automation.
  • `PostToolUse` can't undo anything: the tool already ran. Block in PreToolUse instead.
  • Turn every hook off for a while with "disableAllHooks": true in a settings file.
  • Not sure what's running? Type /hooks to see each hook and the file it comes from.

Frequently asked questions

What are hooks in Claude Code?

Commands Claude Code runs at set moments, like after each file edit or before each shell command. Unlike instructions in CLAUDE.md, they run every time.

Where do I put Claude Code hooks?

In a settings file under a hooks key: .claude/settings.json for one project, or ~/.claude/settings.json for all your projects.

How does a hook block an action?

It exits with code 2. Claude Code stops the action and shows Claude the hook's stderr message. This works on blocking events like PreToolUse.

How do I see my hooks?

Type /hooks in Claude Code. It lists every hook with the file it comes from.

About the author

Maximin

I’m a software engineer, and I help businesses and individuals turn their ideas into real projects with AI agents.

Make Claude Code work your way

Get the free guideSee the project

Ready to Build
Your Project?

Get complete source code from my public builds.
Explore a project, study it, and make it your own.

Browse projects