Skip to content
← All guides

Claude Code hooks: events, config and a blocking example

How Claude Code hooks work: events, settings JSON shape, exit code 2 and permissionDecision output, a script that blocks rm -rf, and the risks hooks add.

4 min read

Claude Code hooks are handlers you configure in settings JSON that run at points in a session. A PreToolUse hook can block a tool call before it runs. A command hook reads JSON on stdin. Exit code 2 blocks the action and sends your stderr to Claude. Exit 0 with a permissionDecision of "deny" also blocks it. Hooks run with your environment, so they are code you must trust.

Events

The hooks reference lists many events. The ones that matter for safety are PreToolUse, PermissionRequest, PermissionDenied, PostToolUse, UserPromptSubmit, ConfigChange, Stop and SessionStart. The full list also includes SubagentStart, FileChanged, WorktreeCreate, PreCompact, Elicitation and SessionEnd.

Config shape

Hooks nest three levels deep. An event holds matcher groups, and each group holds handlers.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "if": "Bash(rm *)",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/block-rm.sh"
          }
        ]
      }
    ]
  }
}

The matcher filters by tool name. Edit|Write is an exact-match list. The optional if field holds one permission rule. The type can be command, http, mcp_tool, prompt or agent. The docs call agent hooks experimental. Hooks can live in ~/.claude/settings.json, .claude/settings.json, .claude/settings.local.json, managed settings, plugin hooks/hooks.json and skill or subagent frontmatter.

Exit codes and decisions

ResultEffect
Exit 0Claude Code applies any JSON you printed. The action goes ahead unless the JSON blocks it.
Exit 2Blocking error. On PreToolUse the call is blocked and stderr becomes the message.
Any other codeNon-blocking error. The action goes ahead.

The docs warn that exit 1 is non-blocking, and that a policy hook should use exit 2. For JSON output, print this and exit 0.

{
  "hookSpecificOutput": {
    "hookEventName": "PreToolUse",
    "permissionDecision": "deny",
    "permissionDecisionReason": "Destructive command blocked by hook"
  }
}

A script that blocks rm -rf

This is the vendor’s example. The PreToolUse input carries tool_name and tool_input. The script needs jq and chmod +x.

#!/bin/bash
COMMAND=$(jq -r '.tool_input.command')
 
if echo "$COMMAND" | grep -q 'rm -rf'; then
  jq -n '{
    hookSpecificOutput: {
      hookEventName: "PreToolUse",
      permissionDecision: "deny",
      permissionDecisionReason: "Destructive command blocked by hook"
    }
  }'
else
  exit 0
fi

A grep on command text misses variants such as rm -fr or a command inside bash -c. Treat a hook like this as one layer.

How hooks interact with permission rules

Per the permissions docs, a PreToolUse hook that returns allow does not override deny or ask rules. A hook that exits 2 stops the call before permission rules are evaluated, even when an allow rule would permit it. So the documented pattern for “allow all Bash except a few” is a bare Bash allow rule plus a blocking hook. Claude Code permissions

Risks of hooks themselves

  • Timeouts. Defaults are 600 seconds for command, http and mcp_tool hooks. On PreToolUse, a timed-out hook does not block the call unless you set "onFailure": "block", which needs v2.1.295 or later.
  • Best-effort filters. The docs say the if filter is best-effort and to use the permission system for hard allow and deny.
  • Inherited environment. A hook process inherits Claude Code’s environment, secrets included.
  • Shared files. A .claude/settings.json committed to a repository can define hooks that run on every teammate’s machine. Review it like code.
  • Switches. "disableAllHooks": true turns hooks off, though it can’t disable managed hooks. allowManagedHooksOnly blocks user, project, local and plugin hooks.

Hooks are not sandboxed. The sandbox covers shell commands, and the docs list hooks as running outside it. Claude Code sandbox

Checked against the vendor documentation on November 25, 2026. Hooks reference · Permissions · Sandboxing

Where Excellent fits

Excellent Pro adds checks before supported actions. Claude Code can display an ask prompt. With Pro, it blocks or asks before the risky actions it checks for. It blocks some steps (your email going to a website, keys typed into commands, deleting protected folders) and asks before others (reading key files, a push to main). Agent coverage · Pricing. Pro is US-only for now.

Excellent’s free scan reads supported logs on the Mac and reports findings for review. Run the free scan · See a sample report. For Codex, see Codex hooks.

Related guides

View all