Skip to content
AI Dev Toolkit.
Esc
  • Loading…

AI glossary · Agents and tools

What are Claude Code hooks?

Also called: hooks, PreToolUse hook, lifecycle hooks

Definition

Claude Code hooks are commands, HTTP calls, MCP tool calls, prompts or subagents that Claude Code runs automatically at fixed points in a session, such as just before a tool call, so a rule is applied every time.

Explained

How do Claude Code hooks work?

A hook pairs an event with a handler. Events include PreToolUse (before a tool runs), PostToolUse (after it succeeds), UserPromptSubmit, Stop and SessionStart, among many more. Handlers come in five types: command, http, mcp_tool, prompt and agent. On tool events, a matcher picks tool names and an optional if field narrows further with permission-rule syntax such as Bash(git *).

Hooks go under a hooks key in a settings file: ~/.claude/settings.json for all your projects, .claude/settings.json for one project (committed), or .claude/settings.local.json for just you. Plugins, skills and subagents can carry hooks too. A command hook receives the event as JSON on stdin. Exit code 0 means success; exit code 2 blocks the action on events that can block, and the hook’s stderr becomes the reason, which Claude sees on tool events such as PreToolUse; any other code is a non-blocking error.

Example

Block npm publish with exit code 2

Put this in .claude/settings.json. The if rule matches npm publish with or without arguments, because a trailing * also matches the bare command, so the hook runs only then. It prints a reason to stderr and exits 2: Claude Code cancels the tool call and shows Claude the reason, so it can take another route.

We ran the command in Bash: it printed the message to stderr and exited with code 2. It needs a POSIX shell such as Bash; on Windows without Git Bash, command hooks run in PowerShell, so write them for that shell.

.claude/settings.json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "if": "Bash(npm publish *)",
            "command": "echo 'Releases are published by CI. Do not run npm publish.' >&2; exit 2"
          }
        ]
      }
    ]
  }
}

Cost and quality

Why use hooks instead of instructions?

Instructions in CLAUDE.md are context: Claude usually follows them, but nothing forces it to. Claude Code’s docs point to a PreToolUse hook when an action must be blocked whatever Claude decides. Hooks also take routine chores, such as formatting after each edit, out of the model’s hands.

A hook is code that runs on your machine whenever its event fires. Read the hooks in any repository before you trust its settings.

Don’t mix up

Hooks vs CLAUDE.md vs permission rules: what’s the difference?

Hooks vs CLAUDE.md
CLAUDE.md tells Claude what you want, and Claude decides. A hook is run by Claude Code itself on every matching event, whatever Claude decides, and exit code 2 can stop an action outright.
Hooks vs permission rules
A permission rule such as a deny on Bash(npm publish *) also blocks a command, with no script. Use a hook when the decision needs logic, a message to Claude, or a side effect such as logging.

FAQ

Questions people ask

What hook events does Claude Code have?

The hooks reference lists more than 30 events across a session’s life, including SessionStart, UserPromptSubmit, PreToolUse, PermissionRequest, PostToolUse, Notification, SubagentStart, SubagentStop, Stop, PreCompact and SessionEnd. New events arrive with Claude Code releases, so read the hooks guide or the official reference for the current list rather than relying on a fixed count.

Which hooks can block an action?

Only some events honour exit code 2. PreToolUse blocks the tool call, UserPromptSubmit blocks the prompt, Stop and SubagentStop make Claude keep working, and PreCompact blocks compaction, among others. PermissionRequest ignores exit code 2: it denies through a decision object in the hook’s JSON output instead. On events that can’t block, exit code 2 at most passes stderr on: after PostToolUse Claude sees it, but the tool has already run.

Go deeper

Try it and read more

Related

All 41 terms in the AI glossary

Keep exploring