How do I make Claude Code hooks fail closed with `onFailure: "block"`?
Claude Code 2.1.295 adds onFailure: "block" for command and HTTP hooks: a hook that can't start, times out, or exits oddly blocks the action instead of running.
From Claude Code 2.1.295, command and HTTP hooks accept onFailure: "block". With it, a hook that can't start, times out, or exits with an unexpected code blocks the action instead of letting it through. That wording comes from the CHANGELOG. As of 2026-10-22, the official hooks reference documents onFailure too, in a section called "Block the action when a hook fails", with an example.
Why you need it: hooks fail open by default
The hooks reference is explicit about the default (checked 2026-10-22):
- For most events, exit code 2 is the only exit code that blocks on its own. Exit code 1 without valid JSON is a non-blocking error and the action proceeds.
- A hook that can't start (missing or non-executable script, shell exit 127) lands in the same non-blocking bucket. The docs warn that "a mistyped path in
settings.jsonmeans the hook never runs." - On
PreToolUse, a timed-out command hook lets the tool call continue. Default timeout forcommandandhttphooks is 600 seconds on most events.
So a policy hook that crashes, hangs, or was never installed correctly lets everything through. onFailure: "block" flips that for command and HTTP hooks.
What 2.1.295 added
Added
onFailure: "block"for command and HTTP hooks: a hook that can't start, times out, or exits with an unexpected code blocks the action instead of letting it through
Scope from that bullet: type: "command" and type: "http" only. The hooks reference lists the field only for those two types too, so don't assume it works on prompt, agent, or mcp_tool hooks.
How to set it
onFailure is a per-hook field, next to type and timeout. The hooks reference lists it in the command and HTTP hook field tables and places it the same way in its own example:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "$CLAUDE_PROJECT_DIR/.claude/hooks/guard-bash.sh",
"timeout": 30,
"onFailure": "block"
}
]
}
]
}
}
Then:
- Upgrade to ≥ 2.1.295 (
claude --version). - Keep your intentional block on
exit 2.onFailurecovers the unexpected failures; it doesn't replace your policy logic. - Set a short, explicit
timeout. With fail-closed, a hung hook now blocks, so 600 s of waiting is a long stall. - Test the failure path: point
commandat a path that doesn't exist and confirm the tool call is blocked, not run with a non-blocking notice. Per the hooks reference, the error includesfailed; blocking because onFailure is "block", ortimed outafter a timeout.
For org-wide enforcement, ship the hook from managed settings so a repo can't drop it.
The Claude moves worth stealing, by email
Workflows tested on real jobs, sent only when they are worth it. Free.
What the docs say now
| Claim | Source | Status 2026-10-22 |
|---|---|---|
onFailure: "block" exists, command + HTTP | CHANGELOG 2.1.295, hooks reference | Documented |
| Default is fail-open (exit 1, can't start, PreToolUse timeout) | Hooks reference | Documented |
| Field sits on each hook entry | Hooks reference field tables and example | Documented |
Other value: "continue", the default for HTTP hooks | Hooks reference | Documented |
| What counts as a failure: can't start, exit code other than 0 or 2, HTTP error or non-2xx, timeout, invalid JSON output | Hooks reference | Documented |
A failure does what exit 2 does on that event; on PermissionRequest it denies the request | Hooks reference | Documented |
No effect on Stop, SubagentStop, TaskCompleted, TeammateIdle, or async/asyncRewake command hooks | Hooks reference | Documented |
2.1.296: managed deny now ends the turn
Checked 2026-10-11. Claude Code 2.1.296 fixed this (CHANGELOG, verbatim):
Fixed managed-settings
PreToolUsehooks that deny a tool call with"continue": false, and managedprompthooks that block one, refusing the call but not ending the turn
- Before 2.1.296: a managed
PreToolUsehook returning"continue": false, or a managedprompthook blocking, refused the tool call but Claude kept going in the same turn. - From 2.1.296: the same deny also ends the turn, as
"continue": falseintends.
This is about managed-settings hooks; the bullet says nothing about user or project hooks. onFailure: "block" (2.1.295) is a separate feature. If you rely on managed hooks to stop Claude cold, run ≥ 2.1.296.
Pitfalls
- Exit 1 as policy. Still non-blocking unless the hook fails in a way
onFailurecatches. Useexit 2for "deny." - Long timeouts + fail-closed. Every slow run becomes a block after the full wait.
- Async hooks and Stop-style events. The hooks reference says
onFailurehas no effect on command hooks withasyncorasyncRewake, or onStop,SubagentStop,TaskCompleted, andTeammateIdlehooks. - Exit 1 with an "allow" JSON. With
"block"set, any exit code other than 0 or 2 is a failure, even if the hook printedpermissionDecision: "allow". Exit 0 to return a JSON decision. - Mods. 2.1.295 also fixed a mod hook receiving deeply nested tool input cut short with no error. Upgrade if you guard with mods.
FAQ
Does onFailure: "block" change what exit 2 does?
No. Exit 2 already blocks on blockable events. onFailure covers can't-start, timeout, and unexpected exit codes.
Which version do I need?
2.1.295 or later for onFailure; 2.1.296 or later if you need managed "continue": false denies to end the turn. See our 2.1.295 notes.
Is this related to retry settings?
No. Retry waits on 429/529 are separate; see the overloaded retry guide.
Sources (checked 2026-10-22)
- Claude Code CHANGELOG.md: 2.1.295
onFailurebullet and mod-hook fix; 2.1.296 managedPreToolUse/promptturn-ending fix. The CHANGELOG doesn't date versions, so we can't give a release day. - Hooks reference: exit-code behavior, can't-start bucket, timeouts and defaults, async hooks.
onFailurerows in the command and HTTP hook field tables, and the "Block the action when a hook fails" section.
