claude/for
Search
Subscribe
Claude CodeUpdated

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.json means the hook never runs."
  • On PreToolUse, a timed-out command hook lets the tool call continue. Default timeout for command and http hooks 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:

  1. Upgrade to ≥ 2.1.295 (claude --version).
  2. Keep your intentional block on exit 2. onFailure covers the unexpected failures; it doesn't replace your policy logic.
  3. Set a short, explicit timeout. With fail-closed, a hung hook now blocks, so 600 s of waiting is a long stall.
  4. Test the failure path: point command at 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 includes failed; blocking because onFailure is "block", or timed out after 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

ClaimSourceStatus 2026-10-22
onFailure: "block" exists, command + HTTPCHANGELOG 2.1.295, hooks referenceDocumented
Default is fail-open (exit 1, can't start, PreToolUse timeout)Hooks referenceDocumented
Field sits on each hook entryHooks reference field tables and exampleDocumented
Other value: "continue", the default for HTTP hooksHooks referenceDocumented
What counts as a failure: can't start, exit code other than 0 or 2, HTTP error or non-2xx, timeout, invalid JSON outputHooks referenceDocumented
A failure does what exit 2 does on that event; on PermissionRequest it denies the requestHooks referenceDocumented
No effect on Stop, SubagentStop, TaskCompleted, TeammateIdle, or async/asyncRewake command hooksHooks referenceDocumented

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 PreToolUse hooks that deny a tool call with "continue": false, and managed prompt hooks that block one, refusing the call but not ending the turn

  • Before 2.1.296: a managed PreToolUse hook returning "continue": false, or a managed prompt hook 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": false intends.

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 onFailure catches. Use exit 2 for "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 onFailure has no effect on command hooks with async or asyncRewake, or on Stop, SubagentStop, TaskCompleted, and TeammateIdle hooks.
  • Exit 1 with an "allow" JSON. With "block" set, any exit code other than 0 or 2 is a failure, even if the hook printed permissionDecision: "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 onFailure bullet and mod-hook fix; 2.1.296 managed PreToolUse/prompt turn-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. onFailure rows in the command and HTTP hook field tables, and the "Block the action when a hook fails" section.

The Claude moves worth stealing, by email

Workflows tested on real jobs, sent only when they are worth it. Free.