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
- Choose a settings file.
~/.claude/settings.jsonapplies to all your projects..claude/settings.jsonapplies to one project and can be committed..claude/settings.local.jsonis for personal project settings. Hook entries from different levels merge rather than replace each other.
- Add a
hooksblock. The structure has three levels: the event name, a matcher group, and one or more handlers. This example runs a script before anyBashtool call:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$CLAUDE_PROJECT_DIR\"/.claude/hooks/check-bash.sh",
"timeout": 10
}
]
}
]
}
}- Write the script. Save it at
.claude/hooks/check-bash.sh, then make it executable withchmod +x. Read the input from stdin. For a Bash call, the command text is intool_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 0The script uses jq, so install it first. The docs list separate install commands for macOS, Debian and Ubuntu.
- Set the matcher. An empty string or
"*"matches everything. A plain name or a|-separated list such asEdit|Writematches exactly. Any other string is treated as a regular expression, such asmcp__memory__.*.
- Check that it is registered. Type
/hooksat the Claude Code prompt and confirm the entry appears underPreToolUse.
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
Bashmatcher will not fire for an edit. Plain matcher names are exact and case-sensitive; regex matchers use JavaScript regular-expression behavior. Open/hooksto confirm the entry appears under the event you expected. - Exit 1 does not block. Scripts that call
exit 1to refuse a command let it through and show an error notice. Useexit 2and write the reason to stderr. - `jq` is missing. A script that depends on
jqfails 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
timeoutin seconds on the handler, and keep checks to small local operations. - Stop hooks loop. A
Stophook that always blocks can keep Claude working. Readstop_hook_activefrom the hook input and allow the stop when it istrue; 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.
Related skills in this catalog
Platform Score is our assessment under a published method. It is not a safety guarantee.
Related guides
- Claude Code plugins explained: install, build, validate
- What Is everything-claude-code (ECC)? Contents and Install
- CLAUDE.md examples: four stack templates, rules, imports
- What Is Claude Code and What It Can Touch on Your Machine
- Codex vs Claude Code: Config Files and Sandbox
- Terminal AI Coding Assistants: Claude Code, Codex, and Gemini CLI
Terms this guide uses
This item is a worked example in the AI glossary for:
Sources
- https://code.claude.com/docs/en/hooks (accessed 2026-11-23)
- https://code.claude.com/docs/en/hooks-guide (accessed 2026-11-23)
This page is not offered to search engines yet: The content file does not mark this page indexable yet.