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

> Canonical: https://skillalmanac.com/learn/claude-code-hooks-explained | Updated: 2026-11-23 | Index status: not offered to search engines

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.

## Facts

- 2026-11-21: Claude Code lists 33 hook events, including PreToolUse, PostToolUse, UserPromptSubmit and Stop (https://code.claude.com/docs/en/hooks)
- 2026-11-21: Hooks are registered in ~/.claude/settings.json, .claude/settings.json, .claude/settings.local.json, managed policy settings, plugin hooks/hooks.json, or skill and subagent frontmatter (https://code.claude.com/docs/en/hooks)
- 2026-11-21: For command hooks, Claude Code writes a JSON object to stdin; tool events include tool_name, tool_input and tool_use_id (https://code.claude.com/docs/en/hooks)
- 2026-11-21: Exit code 2 blocks the action on events that can block, and stderr is returned as the reason; any other non-zero code is a non-blocking error (https://code.claude.com/docs/en/hooks)
- 2026-11-21: Typing /hooks at the Claude Code prompt opens the hooks browser listing configured hooks (https://code.claude.com/docs/en/hooks-guide)
- 2026-11-23: Plain hook matcher values are exact strings, while regex matchers use JavaScript RegExp.prototype.test; tool-event matchers are evaluated against tool_name (https://code.claude.com/docs/en/hooks)
- 2026-11-23: Stop hooks receive stop_hook_active=true when Claude Code is already continuing because of a Stop hook, and the documentation says to check it to avoid an unresolvable loop (https://code.claude.com/docs/en/hooks)

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.

2. 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:

```json
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/check-bash.sh",
            "timeout": 10
          }
        ]
      }
    ]
  }
}
```

3. 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`:

```bash
#!/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.

4. 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__.*`.

5. 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](/skills/skill-almanac/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](/skills/skill-almanac/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.

## Related skills

- [agent-hooks-authoring](https://skillalmanac.com/skills/skill-almanac/agent-hooks-authoring.md): Platform Score 47/100 (provisional)
- [coding-agent-guardrails](https://skillalmanac.com/skills/skill-almanac/coding-agent-guardrails.md): Platform Score 60/100

## Related guides

- [Claude Code plugins explained: install, build, validate](https://skillalmanac.com/learn/claude-code-plugins-explained.md)
- [What Is everything-claude-code (ECC)? Contents and Install](https://skillalmanac.com/learn/what-is-everything-claude-code.md)
- [CLAUDE.md examples: four stack templates, rules, imports](https://skillalmanac.com/learn/claude-md-examples.md)
- [What Is Claude Code and What It Can Touch on Your Machine](https://skillalmanac.com/learn/what-is-claude-code.md)
- [Codex vs Claude Code: Config Files and Sandbox](https://skillalmanac.com/learn/codex-vs-claude-code.md)
- [Terminal AI Coding Assistants: Claude Code, Codex, and Gemini CLI](https://skillalmanac.com/learn/terminal-ai-coding-assistants.md)

## Sources

- https://code.claude.com/docs/en/hooks
- https://code.claude.com/docs/en/hooks-guide

Platform Scores report our assessment under a published method. They are not a safety guarantee.
