Guides · Updated October 23, 2026

Claude Code stuck in a loop: how to break it and stop it repeating the same command

Claude Code runs the same failing command again, gets the same error, and runs it again. Or it flips a file back and forth between two versions. Every one of those turns re-sends the whole conversation, so a loop costs more the longer it goes. This guide covers how to stop one right now, how to undo what it did, how to restart without walking back into it, and how to make Claude Code refuse the next repeat on its own.

What the loop usually looks like

The common shape: nothing changes between attempts, so the next attempt can’t go differently. A healthy fix-and-rerun cycle (run the test, edit the code, run the test) is not a loop, and you don’t want to stop that.

Stop it now

From Claude Code’s interactive mode reference:

Then, before typing anything, read the last few tool calls. If the same command produced the same error three times, the next attempt needs new information (the real error, a file it hasn’t read, a decision from you), not another run.

Undo what the loop changed

Press Esc twice with an empty prompt, or run /rewind, to open the rewind menu (checkpointing docs). Pick the prompt from before the loop started, then restore code, conversation, or both. Two limits:

That is the practical argument for committing (or at least git stash-ing) before you hand Claude a long debugging task.

Restart without walking back into it

One more trap: if the session hits your plan’s usage limit mid-loop, Claude Code can wait and continue the interrupted task on its own after the reset (see Claude Code usage limits). A looping session will pick the loop back up and spend the next window the same way, so check what it was doing before you let it continue.

Why “stop after 3 tries” in CLAUDE.md doesn’t hold

An instruction in CLAUDE.md or the prompt is context, not a rule. The model can miss that it is on try number four, especially after compaction, and it can’t see that npm run test | tail is the same run as npm test. A limit you can rely on has to run outside the model, on every tool call, and be able to refuse it. In Claude Code that is a PreToolUse hook: it runs before each tool call and can return permissionDecision: "deny" with a reason Claude reads, or {"continue": false} to end the session. PostToolUse and PostToolUseFailure hooks see each call’s result, which is how a guard can tell “same command, same output” from “same command, new output”.

Make Claude Code refuse the repeat: LoopGuard

LoopGuard is an open-source (MIT) CLI that installs those hooks. It needs no proxy and no API key, so it works on Pro and Max as well as API billing.

npm i -g loopguard-cli          # puts `loopguard` on your PATH
loopguard init --agent claude   # adds PreToolUse, PostToolUse, PostToolUseFailure and Stop hooks
                                # to ~/.claude/settings.json (original backed up first)
loopguard status                # caps, guarded agents, last 5 trips

Restart running Claude Code sessions so they load the hooks. What it does on a loop (version 0.1.3):

It also caps spend: an estimated $10 jump within 5 minutes ends the session (loop.spiralUsd), and there are per-session and daily caps ($25 and $100 by default), priced at API list prices from the transcripts Claude Code writes, subagents included.

loopguard config set loop.maxRepeats 4      # stricter
loopguard config set loop.progressAware false   # count every identical call, progress or not
loopguard pause 15                          # bypass enforcement for 15 minutes
tail ~/.loopguard/trips.jsonl               # every block, with the reason

What it won’t catch

Related

LoopGuard on GitHub · Pricing