Guides

Claude Code hooks explained, with examples

By JamesUpdated November 14, 2026

Short answer: A hook is a command that Claude Code runs automatically at a set moment, such as before a tool runs, after a file is edited, or when Claude is waiting for you. You define hooks in a hooks block in a settings file, usually ~/.claude/settings.json. Because Claude Code runs them itself, they happen every time, without relying on the model to remember.

What are hooks for?

Use a hook when something must always happen. Common uses:

  • Get a notification when Claude needs your input.
  • Format a file every time Claude edits it.
  • Block a command you never want run.
  • Add context when a session starts.

If the job needs judgment instead of a fixed rule, Claude Code also has prompt-based and agent-based hooks that ask a model to decide.

Where do hooks go?

Where you put a hook decides how far it reaches.

File Applies to Shared with your team
~/.claude/settings.json All your projects No
.claude/settings.json One project Yes, if you commit it
.claude/settings.local.json One project No

Organizations can also set hooks through managed policy, and plugins can ship their own.

Type /hooks inside Claude Code to see every hook that is active, grouped by event. If you edit a settings file while Claude Code is running, it normally picks up the change by itself.

What does a hook look like?

Every hook lives under an event name inside one hooks object. Each entry has a matcher, which narrows when it fires, and a list of hooks to run.

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

If your settings file already has a hooks key, add the new event next to the existing ones. Do not replace the whole object.

Which events matter most?

Claude Code has more than thirty hook events. These are the ones most people use:

Event When it fires
SessionStart A session begins or resumes
UserPromptSubmit You submit a prompt, before Claude processes it
PreToolUse Before a tool call runs. It can block the call
PermissionRequest A tool call needs a permission decision
PostToolUse After a tool call succeeds
Notification Claude Code sends a notification, for example when it is waiting for you
Stop Claude finishes responding
StopFailure The turn ends because of an API error
SubagentStop A subagent finishes
SessionEnd A session ends

The full list is in the official hooks reference, linked at the bottom.

Example 1: get a notification when Claude needs you

This runs when Claude has been waiting for you: about six seconds after a permission prompt appears, or about 60 seconds after Claude finishes and sits idle. On macOS:

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

osascript sends notifications through the Script Editor app. If Script Editor does not have notification permission, the command fails silently. Run osascript -e 'display notification "test"' in Terminal once, then open System Settings, then Notifications, and allow Script Editor.

On Linux, swap the command for notify-send 'Claude Code' 'Claude Code needs your attention'. To fire only for permission prompts, set the matcher to permission_prompt. There is a full guide to every notification option.

Example 2: format files after every edit

This runs Prettier on any file Claude edits or writes. The matcher limits it to the Edit and Write tools.

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

The hook receives details of the tool call as JSON on standard input. Here jq pulls out the file path and passes it to Prettier. --ignore-unknown makes Prettier skip file types it can’t format instead of failing.

Example 3: block a command

A PreToolUse hook can stop a tool call before it runs. This one blocks any shell command containing rm -rf. It only catches that exact spelling: rm -fr or rm -r -f get through, so treat it as an example of the mechanism, not protection. Save it as a script, for example ~/.claude/hooks/block-rm.sh, and make it executable with chmod +x ~/.claude/hooks/block-rm.sh:

#!/bin/bash
command=$(jq -r '.tool_input.command')
if echo "$command" | grep -q 'rm -rf'; then
  echo "Blocked: rm -rf is not allowed" >&2
  exit 2
fi
exit 0

Then register it for the Bash tool:

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "~/.claude/hooks/block-rm.sh" }
        ]
      }
    ]
  }
}

Exiting with code 2 denies the call. For most blocking jobs, a deny rule in your permission settings is simpler than a hook. See the guide to permission prompts.

What kinds of hook are there?

Most hooks are "type": "command", which runs a shell command. Four other types exist:

  • http posts the event data to a URL.
  • mcp_tool calls a tool on a connected MCP server.
  • prompt asks a model a single question.
  • agent runs a multi-step check with tools. This type is experimental.

What should I watch out for?

  • Hooks run in parallel. When several hooks match one event, all of them run to completion. One hook denying a call does not stop the others from running.
  • The strictest answer wins. For PreToolUse decisions the order is deny, then defer, then ask, then allow.
  • PostToolUse cannot undo anything. The tool has already run.
  • Notification hooks cannot block. They are for side effects such as alerts and logging.
  • Hooks have time limits. Command hooks default to 10 minutes, or 30 seconds on UserPromptSubmit. You can set a shorter timeout in seconds.
  • You can switch them all off. Set "disableAllHooks": true in your settings. This does not turn off hooks set by managed policy.
  • You can ask Claude to write one. Describe what you want in Claude Code and it will add the hook for you.

Where does Nubby fit?

Nubby sits on your desk and tells you when your AI agents need you, finish or fail, and you can answer yes or no without switching windows. Those are the same moments described here: a permission is waiting, a turn has finished, or a turn has failed. If you like the idea of hooks but do not want another pop-up on your screen, that is what it is for.

Nubby’s Founder Edition is not open yet. You can join the waitlist and we will email you the day it opens.

Sources

Last checked against the official documentation on November 14, 2026. Nubby is not affiliated with, endorsed by or sponsored by Anthropic. Claude and Claude Code are products of Anthropic.

No spam. Only Nubby news.