Claude Code Hooks: Setup, One Example, and Failure Modes

How to add a Claude Code hook in settings.json, read its JSON input, block a tool call with exit code 2, and debug hooks that do not fire.

Updated · Markdown version

Claude Code hooks are shell commands that the harness runs at fixed points in a session, such as before a tool call, after a file edit, or when Claude stops responding. You add them under a hooks key in a settings file, and each one receives a JSON description of the event on stdin.

Steps to add a first hook

  1. Choose a settings file. ~/.claude/settings.json applies to all your projects. .claude/settings.json applies to one project and can be committed. .claude/settings.local.json is for personal project settings. Hook entries from different levels merge rather than replace each other.
  1. Add a hooks block. The structure has three levels: the event name, a matcher group, and one or more handlers. This example runs a script before any Bash tool call:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/check-bash.sh",
            "timeout": 10
          }
        ]
      }
    ]
  }
}
  1. Write the script. Save it at .claude/hooks/check-bash.sh, then make it executable with chmod +x. Read the input from stdin. For a Bash call, the command text is in tool_input.command:
#!/bin/bash
CMD=$(jq -r '.tool_input.command')
if echo "$CMD" | grep -q 'rm -rf'; then
  echo "Blocked: rm -rf is not allowed in this project." >&2
  exit 2
fi
exit 0

The script uses jq, so install it first. The docs list separate install commands for macOS, Debian and Ubuntu.

  1. Set the matcher. An empty string or "*" matches everything. A plain name or a |-separated list such as Edit|Write matches exactly. Any other string is treated as a regular expression, such as mcp__memory__.*.
  1. Check that it is registered. Type /hooks at the Claude Code prompt and confirm the entry appears under PreToolUse.

Check that it worked: ask Claude to run a command that matches your pattern. The tool call should stop, and the stderr text from your script should appear as the reason. Then run a harmless command and confirm it still goes through.

How the hook sees a tool call

Tool events add tool_name, tool_input and tool_use_id to the common fields (session_id, cwd, transcript_path, permission_mode, hook_event_name). Exit codes decide the outcome: 0 lets the action proceed and parses stdout as JSON if it is a single object, 2 blocks the action on events that support blocking, and any other code lets the action proceed with an error notice. Exit 1 is therefore not a way to enforce a rule.

What hooks are not

A hook is not a prompt. It runs your command every time the event fires, so it is deterministic code, not guidance Claude may or may not follow. A hook also cannot undo an action that already happened: a PostToolUse hook that exits 2 sends its stderr to Claude but does not reverse the edit.

Pitfalls

  • The hook never runs. Tool-event matchers filter on the tool name, not on file paths, so a Bash matcher will not fire for an edit. Plain matcher names are exact and case-sensitive; regex matchers use JavaScript regular-expression behavior. Open /hooks to confirm the entry appears under the event you expected.
  • Exit 1 does not block. Scripts that call exit 1 to refuse a command let it through and show an error notice. Use exit 2 and write the reason to stderr.
  • `jq` is missing. A script that depends on jq fails before it reads the command, so the policy check quietly passes. Test the script by piping sample JSON into it.
  • The hook is slow. Long commands hold up every matching tool call. Set a timeout in seconds on the handler, and keep checks to small local operations.
  • Stop hooks loop. A Stop hook that always blocks can keep Claude working. Read stop_hook_active from the hook input and allow the stop when it is true; Claude Code also caps consecutive Stop-hook continuations, but the guard keeps your own rule explicit.

When to use a skill instead

Hooks suit rules that must run on every matching event, such as blocking a command or formatting a file after each edit. Ad-hoc prompting or a plain instruction file is enough when the goal is guidance Claude can weigh with judgment. agent-hooks-authoring is a related skill on this site that writes, installs, tests and debugs hook configurations, which helps when the hook logic is more than a few lines. coding-agent-guardrails is a related skill on this site that provides a pre-action checklist for destructive git commands, generated files, weakened tests and committed secrets, which can complement a hook rather than replace it.

FAQ

What are hooks in Claude Code? Hooks are user-defined shell commands that Claude Code runs at lifecycle points such as before a tool call, after an edit, or at session start. They are configured in settings files and receive event data as JSON on stdin.

Claude Code hooks vs skills: which should I use? Use a hook when an action must happen every time an event fires, such as blocking a command. Use a skill when Claude needs instructions or steps it can apply with judgment, because skills guide behavior rather than enforce it.

How do I block a tool call with a hook? Write a script that reads tool_input from stdin, writes the reason to stderr, and exits with code 2. Registering that script under a PreToolUse matcher stops the matching call.

Platform Score is our assessment under a published method. It is not a safety guarantee.

Terms this guide uses

This item is a worked example in the AI glossary for:

Next steps

  1. Claude Code plugins explained: install, build, validate Read next.
  2. What Is everything-claude-code (ECC)? Contents and Install Read next.
  3. Claude Code compatibility Install evidence per tool.
  4. All guides by topic Browse everything.

Sources

This page is not offered to search engines yet: The content file does not mark this page indexable yet.