All Agent Engineering

Claude Code Stop hooks: continuation decisions and loop guards

Inspect stop_hook_active, block and reason, then check continuation guards with local inputs.

AINavWritten / Updated
On this page

At a glance

The goal is to avoid a Stop hook repeatedly requiring continuation as a session ends. The hooks reference was checked and original pure-function fixtures executed for input and JSON output. No hook was installed or Claude Code started. The fixture demonstrates a guard, not a complete completion checker.

Runtime and applicability

Article type
Spec file
Verified
Applies to
  • Claude Code Applies to Claude Code as documented; environment prerequisites appear in the body.

File locations

PathScopeHow it is read
settings.json 的 hooks.Stop 与本地 command Hook 脚本Continuation decision at session stop; not a replacement for tool permissions.On Stop, a command reads event JSON and synchronously returns its decision.

When to use

  • When an end-of-turn check needs bounded continuation without an unsatisfiable loop.

When not to use

  • For blocking a tool call, inspect the relevant PreToolUse event rather than relying on Stop.

Inspect stop_hook_active first

A true stop_hook_active means Claude is already continuing because of a Stop hook. The docs advise checking it or the transcript to avoid an unsatisfiable loop. The original decision first requires a Stop event and a Boolean field, then returns no continuation for true. Missing or wrong-typed input should be recorded as invalid; do not assume false and block again.

Blocking stop requires a reason

Top-level decision:"block" prevents stopping and requires reason to tell Claude why to continue. To allow stop, omit decision or exit 0 without JSON. It is not a tool-permission denial. The fixture returns block with a fixed reason when active=false and an externally supplied Boolean needFollowup=true, and an empty object when active=true. needFollowup is supplied by the caller, not an official event field or a text-based completion inference.

Async hooks cannot provide the blocking gate

async:true applies only to command hooks and lets Claude continue without waiting. Its decision, permissionDecision and continue fields have no effect because the controlled action has completed. A Stop gate must not print from a background process after the script exits or assume an async block is honored. This guide uses a synchronous pure-function model, with no actual command path configured; inspect event, timeout and exit status before deployment.

Check guard branches without certifying a session

Local checks cover active=false/true, no follow-up, missing fields and string false, verifying no block for invalid input, no repeated continuation for true, and a reason with block. They do not verify hook loading, event delivery or the runtime continuation cap. In a disposable session, inspect one bounded continuation followed by exit and retain diagnostics. Stop using the added hook on repetition or unclear input and inspect its source.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Anthropic · Hooks reference

Found an error? Tell us →