Skip to content

ProperPath

Menu

Free resources · Rules and hooks

Claude Code stop hook: give it a way out before it traps Claude

A Claude Code stop hook can send Claude back to check its work, or trap it in a loop with no answer. What our test of three hooks showed, and a five-minute hand test that proves yours lets go.

Built by ProperPath

What is a Claude Code stop hook?

A Stop hook is a command Claude Code runs each time Claude is about to finish a reply, not only when a whole task is done. Like all Claude Code hooks, it is a command you set up that runs on its own.

If it says "block", the reply does not end there: Claude reads the reason your hook gave and keeps working. People use it to make Claude run the tests, or look over its work, before it says done. The one thing it needs is a way out, and the five-minute test below proves yours has one.

Why does my stop hook loop?

It loops because it blocks on something Claude can never meet. Each block sends Claude into another turn. If the hook says no every time, Claude replies, gets sent back, and you get no answer at all.

The sentence your hook gives Claude is the block reason. It has to name something Claude can do. "Review your reply once more" is not enough if the hook blocks anyway, nor is a fix Claude cannot carry out.

What happened when we tested three stop hooks?

The hook with no way out never produced an answer; the two with a way out always did. We asked Claude Haiku 5.5, in Claude Code 2.1.296, ten developer questions three times per hook, headless (run from a script, no chat window) with no tools and a 12-turn limit: 30 sessions per hook.

The tested hooks were short Python scripts with fixed block reasons that blocked with a JSON decision; they checked nothing real, such as tests. The script below is a separate, simpler hook. Other limits: one model, no tools, one question per session.

How does the hook know Claude already tried?

It reads a field called stop_hook_active. Each time the hook runs, Claude Code sends it JSON on its standard input, the text a program reads when you pipe something into it. That JSON holds stop_hook_active, which is true when Claude is already continuing because a Stop hook blocked it.

The way out: when stop_hook_active is true, let Claude finish. Your hook can still send Claude back, just not forever.

Doesn't Claude Code stop the loop on its own?

Only as a backstop. After 8 blocks in a row, Claude Code overrides the next block and ends the turn without your answer. The count starts over each time Claude uses a tool, so in a session that edits files and runs commands, the loop can run well past eight blocks. In our view it is no substitute for a hook that knows when to let go.

How do I build a stop hook with a way out?

Prove the way out before the hook goes near a real session. You need python3; run python3 --version to see that you have it.

mkdir stop-hook-test
cd stop-hook-test
import json
import sys

hook_input = json.load(sys.stdin)

# Claude is already continuing because a Stop hook blocked it: let it finish.
if hook_input.get("stop_hook_active"):
    sys.exit(0)

print("Before you finish, add one line that names what you checked and how.", file=sys.stderr)
sys.exit(2)
echo '{"hook_event_name": "Stop", "stop_hook_active": false, "last_assistant_message": "Done."}' | python3 stop-check.py; echo "exit code: $?"
echo '{"hook_event_name": "Stop", "stop_hook_active": true, "last_assistant_message": "Done. I checked the tests."}' | python3 stop-check.py; echo "exit code: $?"

It checks nothing real yet; it proves the way out.

Then run the same two lines against a Stop hook of your own, from your project folder, where its paths work: replace python3 stop-check.py with the command your Stop hook runs in .claude/settings.json. If your hook reads other fields from its input, add them to both lines.

Where is the full answer?

Check that your hook lets go

The first line, with stop_hook_active false, prints the block reason and then exit code: 2. The second line, with stop_hook_active true, prints nothing but exit code: 0. If the second line prints anything, or any exit code but 0, your hook ignores stop_hook_active: add the check. If your hook blocks until the reply holds some text, put that text in last_assistant_message on the second line and run it again; it should then print only exit code: 0. Fix that before you add the hook to your settings.