All guides

Claude Code hooks, a practical guide with working examples

Updated 17 November 2026

Add a hooks block to ~/.claude/settings.json or .claude/settings.json. Each entry names an event such as PreToolUse, PostToolUse, Notification or Stop, an optional matcher, and a shell command. The command gets JSON on stdin, and exit code 2 blocks the action.

Hooks are shell commands that Claude Code runs at fixed points: before a tool call, after one, when it sends a notification, when it finishes a reply. They always run, which makes them the right tool for rules you do not want to leave to the model’s judgment.

The examples below are from the hooks guide in the Claude Code docs, or follow its shapes closely. Several use jq; install it with brew install jq.

Where do hooks go?

File Scope
~/.claude/settings.json All your projects
.claude/settings.json One project, can be committed
.claude/settings.local.json One project, yours only

Every hook has the same three levels: the event, a matcher group, and one or more handlers.

{
  "hooks": {
    "EventName": [
      {
        "matcher": "pattern",
        "hooks": [
          { "type": "command", "command": "your-command" }
        ]
      }
    ]
  }
}

If the file already has a hooks key, add your event beside the existing ones. Run /hooks inside Claude Code to see what is configured.

How does a hook read input and answer?

Claude Code passes JSON to the command on stdin. For a Bash call, a PreToolUse hook gets something like this:

{
  "session_id": "abc123",
  "cwd": "/Users/sarah/myproject",
  "hook_event_name": "PreToolUse",
  "tool_name": "Bash",
  "tool_input": {
    "command": "npm test"
  }
}

The hook answers with its exit code:

  • Exit 0 means no objection. For PreToolUse, that does not approve the call; the normal permission flow still applies.
  • Exit 2 blocks the action. Whatever you write to stderr goes back to Claude as feedback.

For finer control, exit 0 and print JSON to stdout. Pick one approach per hook.

PreToolUse: how do I block a command before it runs?

Save this as .claude/hooks/block-drop.sh and make it executable with chmod +x:

#!/bin/bash
INPUT=$(cat)
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command')

if echo "$COMMAND" | grep -q "drop table"; then
  echo "Blocked: dropping tables is not allowed" >&2
  exit 2
fi

exit 0

Register it for Bash calls:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/block-drop.sh"
          }
        ]
      }
    ]
  }
}

A PreToolUse hook can also return a decision as JSON. This one denies the call and tells Claude why:

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "Use rg instead of grep for better performance"
  }
}

permissionDecision can be "deny", "ask" to show the normal prompt, or "allow" to skip it. An "allow" does not override your deny or ask rules. A "deny" holds in every permission mode, including bypassPermissions.

PostToolUse: how do I format files after Claude edits them?

This runs Prettier on every file Claude edits. The matcher limits it to the file-editing tools.

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "f=$(jq -r '.tool_input.file_path') && npx prettier --write --ignore-unknown \"$f\""
          }
        ]
      }
    ]
  }
}

When it succeeds, nothing appears in the conversation. Check that the file was reformatted.

Notification: how do I get pinged when Claude needs me?

{
  "hooks": {
    "Notification": [
      {
        "matcher": "permission_prompt",
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e 'display notification \"Claude Code needs your attention\" with title \"Claude Code\"'"
          }
        ]
      }
    ]
  }
}

The permission_prompt matcher fires when a permission prompt has waited about six seconds. Use idle_prompt for the case where Claude finished about 60 seconds ago and you have not typed, or an empty matcher for every notification type. The terminal-by-terminal details are in getting notified when Claude Code is waiting.

If no notification appears, the docs explain that osascript sends through Script Editor. Run osascript -e 'display notification "test"' once, then turn on Allow Notifications for Script Editor in System Settings > Notifications.

Stop: how do I run something when Claude finishes a reply?

Stop fires when Claude finishes responding. The simplest use is a sound:

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "afplay /System/Library/Sounds/Glass.aiff"
          }
        ]
      }
    ]
  }
}

A Stop hook can also block, which makes Claude keep working. That is useful for “do not stop until the tests pass”, and it is easy to turn into a loop. Claude Code overrides a Stop hook after it blocks eight times in a row, and the docs tell you to check stop_hook_active first:

#!/bin/bash
INPUT=$(cat)
if [ "$(echo "$INPUT" | jq -r '.stop_hook_active')" = "true" ]; then
  exit 0  # Allow Claude to stop
fi
# ... rest of your hook logic

Why is my hook not firing?

  • Run /hooks and confirm it is listed under the right event.
  • Matchers are case-sensitive and must match the tool name exactly: Bash, Edit, Write.
  • Check the event. PreToolUse runs before the tool, PostToolUse after it succeeds.
  • Make the script executable.
  • If JSON output has no effect, check that your shell profile is not printing text before it.

To switch every hook off while you debug, set "disableAllHooks": true in the settings file.

What do hooks not do?

A hook runs a command and gets a decision back. It gives you nowhere to look and nothing to click. If you want to read a request and answer it by hand from outside the terminal, something has to hold the request open and show it to you.

NotchDaddy uses the same hook system to do that. It registers its own hooks in ~/.claude/settings.json, shows each permission request in the MacBook notch with the exact command, and sends your Allow or Deny back as the hook’s decision. If you do not answer, the request falls back to the terminal prompt. Your own hooks keep working beside it.

You do not need it if your hooks only automate things, such as formatting and blocking, and you are happy answering prompts in the terminal. See approving Claude Code from the notch for how the approval flow works.

Start a 3-day trial for $0. After that it is $19 once.