← Doki

Claude Code hooks explained: events, settings and five examples

By , maker of Doki · Updated 28 November 2026

A hook is a command that Claude Code runs at a set moment: when it finishes, before it runs a tool, when it needs you. Hooks are how you get notifications, block risky commands, format files after every edit, or give Claude extra context when a session starts.

The short answer

Hooks go in ~/.claude/settings.json under "hooks". Each one names an event (such as Stop), an optional matcher (such as a tool name), and a command to run. Claude Code passes the command a JSON description of what's happening on stdin. The command's exit code decides what happens next: 0 carries on, 2 blocks the action (for events that can be blocked) and sends your stderr text to Claude. Type /hooks in Claude Code to see which hooks are loaded.

The events you'll use most

Claude Code has more than 30 hook events. These are the ones most setups need; the rest are in the hooks reference.

EventRuns whenCan it block?Typical use
StopClaude finishes respondingYes: Claude keeps going"Done" notifications, checks before stopping
NotificationClaude Code wants your attention, for example a permission promptNo"Needs you" notifications
PreToolUseBefore a tool call runsYes: the call is cancelledBlocking risky commands
PostToolUseAfter a tool call succeedsNo (it already ran)Formatting or linting edited files
UserPromptSubmitYou send a prompt, before Claude sees itYes: the prompt is droppedAdding context, filtering prompts
SessionStartA session starts or resumesNoLoading context such as git status
SessionEndA session endsNoCleanup, logging
SubagentStopA subagent finishesYesChecking a subagent's work
PreCompactBefore the conversation is compactedYesSaving notes first

Where hooks go, and what they look like

Hooks can live in any settings file, and Claude Code merges them all:

The shape is always the same: event, then a list of matcher groups, then the commands:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          { "type": "command", "command": "echo edited >> ~/claude-edits.log" }
        ]
      }
    ]
  }
}

What a hook receives

Every command hook gets a JSON object on stdin. All events include session_id, transcript_path, cwd (the project folder) and hook_event_name. Tool events add tool_name and tool_input; Notification adds the message and its notification_type. A PreToolUse hook for a shell command receives something like:

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

The examples below read it with jq, which comes with macOS 15 and later (brew install jq on older versions). The environment variable CLAUDE_PROJECT_DIR holds the project's folder.

Exit codes: carry on, or block

For finer control a hook can print a JSON object instead, for example permissionDecision (allow, deny or ask) from PreToolUse. The JSON must be the only thing on stdout, so make sure your shell profile doesn't print anything.

Five examples you can copy

1. A banner when Claude is done, with the project's name

{
  "hooks": {
    "Stop": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "osascript -e \"display notification \\\"$(basename \"$CLAUDE_PROJECT_DIR\") is done\\\" with title \\\"Claude Code\\\" sound name \\\"Glass\\\"\""
          }
        ]
      }
    ]
  }
}

The banner comes from Script Editor, so allow Script Editor in System Settings, Notifications. More ways to get alerts are in how to get notified when Claude Code finishes.

2. An alert when Claude needs your permission

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

permission_prompt fires once a permission prompt has waited about six seconds. idle_prompt fires when Claude finished about a minute ago and you haven't typed.

3. Block dangerous shell commands

Save this as ~/.claude/hooks/block-danger.sh and run chmod +x on it:

#!/bin/bash
cmd=$(jq -r '.tool_input.command // empty')
if echo "$cmd" | grep -Eq 'rm -rf (/|~)|git push (-f|--force)'; then
  echo "Blocked by a hook: $cmd" >&2
  exit 2
fi
exit 0

Then point a PreToolUse hook at it:

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

A pattern list like this is a safety net, not a sandbox. For hard rules, use Claude Code's permission settings as well.

4. Format every file Claude edits

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "jq -r '.tool_input.file_path // empty' | xargs -I {} npx prettier --write --ignore-unknown \"{}\""
          }
        ]
      }
    ]
  }
}

Put this one in the project's .claude/settings.json, so it only runs where Prettier is set up.

5. Start each session knowing what's changed

{
  "hooks": {
    "SessionStart": [
      {
        "hooks": [
          { "type": "command", "command": "cd \"$CLAUDE_PROJECT_DIR\" && git status --short && git log --oneline -5" }
        ]
      }
    ]
  }
}

Whatever a SessionStart hook prints is added to Claude's context, so Claude starts the session knowing which files are changed and what was committed last.

When a hook doesn't work

A word on safety

Command hooks run with your full user permissions, without asking. Read the hooks in a project's .claude/settings.json before you trust a repository you didn't write, quote variables ("$VAR"), and use full paths in commands.

Hooks without editing JSON

If what you want is to know when sessions finish or need you, Doki sets up the Stop, Notification, UserPromptSubmit and SessionEnd hooks with one click and removes them when you disconnect. A desktop pet walks over and says which session it was and what Claude did. Its hook only talks to Doki on your own Mac and gives up after two seconds. To compare it with other tools, see Claude Code notification tools compared.

Questions

Where do Claude Code hooks go?

In a settings file, under "hooks": ~/.claude/settings.json for all your projects, .claude/settings.json to share them with a project's team, or .claude/settings.local.json for one project without committing them.

Why doesn't my Stop hook run when Claude asks for permission?

Because Claude hasn't finished; it's waiting. Use a Notification hook with the permission_prompt matcher for that.

How do I block a command with a hook?

Use PreToolUse, exit with code 2, and write the reason to stderr. Exit code 0 doesn't approve the command; it still goes through your normal permission rules.

Do hooks slow Claude Code down?

Claude waits for a hook to finish, and matching hooks run in parallel. A hook that shows a banner returns at once; one that waits on a slow server holds Claude up, so give those a short timeout.