Claude Code hooks

Claude Code Notification hook: get alerted when Claude needs you

A Notification hook runs when Claude Code sends a notification: Claude waits for a permission, has been idle, or a background session is done. It is the place to ring a bell or show a desktop message.

This page covers the types, the input, a script you can copy, and the traps. Every statement about Claude Code comes from the official hooks reference, linked at the end.

When the hook runs

Notification runs whenever Claude Code sends a notification. The matcher filters on the notification type. Leave it out to run the hook for every type.

The hook runs even when desktop notifications are off. The preferredNotifChannel setting, including notifications_disabled, changes how Claude Code alerts you, not whether your hook runs.

Notification types

The reference lists more types than this page shows, among them auth_storage_failure, elicitation_url_dialog, agent_needs_input and three quota_auto_resume_* types. These five cover the common cases:

permission_prompt
Claude needs you to approve a tool use or a sandboxed command's network request, and the prompt has waited about six seconds.
idle_prompt
Claude finished responding about 60 seconds ago and you have not typed since.
auth_success
Authentication completes.
elicitation_dialog
An MCP server opens an elicitation form and you have not typed for about six seconds.
agent_completed
A background session finishes or fails. Fires only while agent view is open in a terminal.

Use one matcher group per type to run different scripts. The matcher permission_prompt|idle_prompt in the example below lists two types.

What arrives on stdin

On top of the common fields, a Notification hook receives message (the notification text), an optional title, and notification_type. This is the example from the reference:

stdin
{
  "session_id": "abc123",
  "cwd": "/Users/...",
  "hook_event_name": "Notification",
  "message": "Claude needs your permission",
  "notification_type": "permission_prompt"
}

Notification hooks cannot block or modify a notification. Exit code and stderr are ignored. Claude Code discards the systemMessage and continue fields. One field still works: terminalSequence.

Example: desktop notification

Hooks run without a controlling terminal, so a script cannot write an escape sequence to /dev/tty. Instead it returns the sequence in terminalSequence, and Claude Code emits it. The field accepts these sequences: OSC 0, 1 and 2 (window and icon titles), OSC 9 (iTerm2, ConEmu, Windows Terminal and WezTerm notifications), OSC 99 (Kitty), OSC 777 (urxvt, Ghostty and Warp) and a bare BEL. Anything else is rejected and the field is ignored.

The script below builds an OSC 777 notification from the message, as in the reference. It runs as it stands: a test in our repository executes this exact code.

1. Add the hook

.claude/hooks/notify.mjs
import { readFileSync } from "node:fs";

const input = JSON.parse(readFileSync(0, "utf8"));

// Remove control characters and the field separator before building the sequence.
const clean = (text) =>
  String(text ?? "")
    .replace(/[\u0000-\u001f\u007f;]/g, " ")
    .slice(0, 200);

const message = clean(input.message) || "Needs your attention";
const sequence = "\u001b]777;notify;Claude Code;" + message + "\u0007";

console.log(JSON.stringify({ terminalSequence: sequence }));

It strips control characters and semicolons from the message first. Without that, a notification text with an escape character or a semicolon could break the sequence or end it early.

2. Register it

~/.claude/settings.json
{
  "hooks": {
    "Notification": [
      {
        "matcher": "permission_prompt|idle_prompt",
        "hooks": [
          {
            "type": "command",
            "command": "node",
            "args": [
              "${CLAUDE_PROJECT_DIR}/.claude/hooks/notify.mjs"
            ],
            "timeout": 10
          }
        ]
      }
    ]
  }
}

A hook for your own alerts belongs in ~/.claude/settings.json, not in the project file that your team shares.

Traps

  1. You only get these notifications when you look away. In terminal sessions, permission_prompt fires once you have not typed for about six seconds, and every keystroke defers it. idle_prompt fires about 60 seconds after Claude finished, and only if no background agent is still running. To run a hook the moment Claude asks for permission, the reference points to the PermissionRequest event.
  2. Desktop and VS Code time it differently. Where Claude Code sends permission requests to the Agent SDK's canUseTool callback, as Claude Desktop and the VS Code extension do, permission_prompt fires about six seconds after the request and typing does not defer it. Set CLAUDE_CODE_DISABLE_PERMISSION_PROMPT_NOTIFY_HOOKS to 1 to turn it off there.
  3. terminalSequence needs an interactive session. Claude Code writes it only while its interface is open. With the -p flag and in the Agent SDK it ignores the field. The hook must not run in the background, so leave async and asyncRewake off.
  4. tmux and GNU screen need a setting. In tmux 3.3 or later, add set -g allow-passthrough on to ~/.tmux.conf. In GNU screen, keep Claude Code's window in front, because sequences from a background window do not reach your terminal.
  5. Match the type exactly. A matcher made only of letters, digits, _, -, spaces, commas and | is an exact string or a list of exact strings. A matcher with any other character is read as a regular expression.

For the other events and what each can block, see the overview of all hook events.

Sources

Checked against the official docs on .

More: All Claude Code hook events · Stop hook · SessionEnd hook · All docs