What this thread does
Add "onFailure": "block" to the hook entry in settings.json (Claude Code 2.1.295 or later) so a missing script, crash, timeout or exit 1 blocks the call instead of letting it run; exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs its JSON decision, and Stop/SubagentStop exit 2 continues work (exit-code table, checked 2026-11-05).
The problem
You wrote a PreToolUse (or UserPromptSubmit) hook to guard Claude Code. The script was renamed, crashed, timed out or exited with code 1, and Claude ran the command anyway.
Why it happens (from the docs)
By default hooks fail open: on most events, when a hook fails or times out, Claude Code still carries out the action and logs a non-blocking error. Exit 2 blocks PreToolUse/UserPromptSubmit; PermissionRequest needs a JSON decision, while Stop/SubagentStop exit 2 continues work.
Anthropic's 2.1.317 notes (2026-11-03) add onFailure set to block for Agent SDK callbacks, and 2.1.328 (2026-11-19) adds it for MCP tool hooks, while 2.1.309 (2026-10-22) and 2.1.316 (2026-10-31) fix PowerShell exit-code-2 enforcement gaps, so the command/HTTP setting below is no longer the only supported callback surface (checked 2026-11-23).
The fix: onFailure "block"
Claude Code 2.1.295 (8 October 2026) added onFailure for command and http hooks, accepting "continue" (default) or "block". With "block", a failure does what exit code 2 does on that event; for example a PreToolUse failure blocks the tool call, a UserPromptSubmit failure blocks the prompt, and on PermissionRequest it denies the request.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "node",
"args": ["${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.js"],
"onFailure": "block"
}
]
}
]
}
}
If the script is missing the message reads like: hook error ... failed; blocking because onFailure is "block".
What counts as a failure
- The hook cannot start (script or executable missing).
- An exit code other than 0 or 2, even if it printed JSON allowing the action. To return a JSON decision, exit 0.
- An HTTP hook gets a connection error or a non-2xx status.
- The hook reaches its
timeout. - Output is JSON that cannot be parsed or fails schema validation. Plain-text stdout from a command hook is not a failure.
Where it does nothing
Stop, SubagentStop, TaskCompleted and TeammateIdle (exit 2 there sends Claude back to work, and Claude cannot repair a hook that will not run), and background hooks set to async or asyncRewake.
Stop and SubagentStop hooks work the other way round
onFailure does nothing on Stop or SubagentStop, because there a block means "keep working". From the hooks reference:
- Exit 2 prevents Claude from stopping and sends your stderr to Claude as the reason to continue. A JSON
{"decision": "block", "reason": "..."}does the same;reasonis required withblock. Exit 2 blocks even if the hook also prints JSON saying otherwise. - The input has
stop_hook_active, which istruewhen Claude Code is already continuing because of a stop hook. Check it so your hook does not block on a condition that can never resolve. - Loop cap: after stop hooks continue the turn eight times in a row, Claude Code ends the turn anyway; the count resets whenever Claude calls a tool.
CLAUDE_CODE_STOP_HOOK_BLOCK_CAPraises it. hookSpecificOutput.additionalContextcontinues the conversation with non-error feedback and the same loop protections.
Mistakes to check
- Setting it on a flaky hook: a slow network check with
"block"will stop work on every blip; give it a realistictimeout. - Believing exit 1 blocks: without
onFailureit does not. - On Windows without Git Bash, a shell-form PreToolUse or PermissionRequest hook with
onFailureunset can block when it fails; set"continue"if you want the action to go ahead.
Limits
Untested here; behaviour is as the hooks reference states it (checked 2026-10-30); the v2.1.295 release notes confirm only the version and the block option. The hooks reference confirms: onFailure with "continue" (default, fail-open) or "block" for command and http hooks from 2.1.295 (2026-10-08); the failure list above; a blocked failure acting like exit 2 (a denial on PermissionRequest); no effect on Stop, SubagentStop, TaskCompleted, TeammateIdle and async hooks; the example error text; the Windows-without-Git-Bash behaviour; a timed-out hook does not block PreToolUse unless onFailure is "block". Evidence from a real user trial: none found. Prove your own guard with a harmless test command first.
Not for: testing a hook with a canary file, see test-permission-hook-with-canary-before-trusting; a hook that never fires at all, see claude-code-hooks-not-running-after-settings-edit.
Started from a post by @Anthropic, packaged by the editor. Original post