Claude Code stuck in a loop? Stop it and get back on track
On this page
  1. How to tell it is looping
  2. Step 1: interrupt with Esc
  3. Step 2: rewind to before the loop
  4. Step 3: say what changed
  5. Step 4: make the job smaller
  6. Stop it happening again
  7. Where Joxo fits
  8. Frequently asked questions
  9. How do I stop Claude Code mid-task?
  10. How do I undo what Claude Code just did?
  11. Why does Claude Code keep making the same mistake?
  12. Try it with your team

Blog / claude code

Claude Code stuck in a loop? Stop it and get back on track

Interrupt with Esc, rewind to before the loop started, say what changed, and narrow the task. How to spot a loop early and stop it coming back.

When Claude Code goes round in circles (the same test fails, it tries a fix, the fix fails, it tries the first fix again), more turns rarely help. Press Esc to stop it, rewind to before the loop began, tell it what you now know, and give it a smaller job. That takes a minute and saves the next twenty.

Here is how to spot a loop early, the four steps to break it, and what to set up so it happens less.

How to tell it is looping#

A loop is not the same as slow, careful work. Watch for these:

  • The same command fails again with the same error, after an edit that didn't touch the cause.
  • It reads the same file again with nothing changed in between.
  • Fixes undo each other. It changes a line, then changes it back two attempts later.
  • The plan keeps getting rewritten, but each version tries the same thing.
  • It widens the blast radius. A one-line bug turns into edits across five files, or it starts disabling the failing test.

One of these is normal. Two or three in a row means the agent has lost the thread, usually because its context is full of failed attempts that now steer it toward more of the same.

Step 1: interrupt with Esc#

Press Esc once. Claude Code stops the current response or tool call and keeps the work done so far. You are back at the prompt, and nothing has been thrown away.

Don't type "stop" while it is still running and hope it notices; that message is queued and sent after the current step. Esc stops it now.

Step 2: rewind to before the loop#

With the prompt input empty, press Esc twice (or run /rewind). The rewind menu lists every prompt you sent in the session. Pick the one just before things went wrong, then choose:

OptionUse it when
Restore code and conversationThe attempts made things worse. Start again from the clean point
Restore conversationThe code is fine, but the failed attempts are crowding the context
Restore codeThe reasoning was right, the edits were wrong
Summarize from hereYou want to keep what it learned, in a few lines instead of many

Rewinding removes the failed attempts from what the agent sees, which is the main reason it was stuck. One limit: rewind only restores files Claude edited with its own editing tools. A file changed only by a shell command (a code generator, a formatter, sed) isn't tracked, so use git diff and git restore for those. Commit before a risky step and you never have to think about it.

If the whole session has gone stale, /clear starts a new conversation in the same folder. You can get the old one back with /resume.

Step 3: say what changed#

The agent loops because, from where it sits, the next attempt still looks reasonable. Give it something new to work with:

  • The real error, copied in full, not "it still fails".
  • What you ruled out: "It isn't the cache, I cleared it", "The env var is set, I checked with echo".
  • One concrete hint if you have one: the file, the function, the version that broke.
  • A rule for this attempt: "Don't change the test", "Read the error before editing", "If the same check fails twice, stop and tell me why".

A short message with one new fact beats a long one that repeats the task.

Step 4: make the job smaller#

Loops are most common on jobs with several moving parts. Split them:

  1. Ask for a diagnosis first, with no edits. Switch to plan mode with Shift+Tab (it cycles through the modes) and ask: "Why does this test fail? Don't change anything yet."
  2. Agree on the cause, then ask for the one change that fixes it.
  3. Run the check yourself if the output is long or flaky, and paste only the part that matters.

If it is still stuck after a clean restart and a smaller job, the problem may not be solvable from inside the repository: a missing service, a broken dependency release, a flaky test. That is worth knowing too.

Stop it happening again#

  • Put the rules in CLAUDE.md: how to run one test file, which tests are known to be flaky, "stop after two failed attempts at the same fix and report". Rules in CLAUDE.md are loaded into every session.
  • Keep a fast single-test command in that file. Agents loop more when the only check takes five minutes and prints 3,000 lines.
  • Commit at each green step. A known good point makes both rewind and git restore cheap.
  • Cap unattended runs. In scripts and scheduled jobs, claude -p --max-turns 20 "…" ends the run when it reaches the limit, with Error: Reached max turns (20), instead of letting it go on all night.

Where Joxo fits#

A loop you can't see costs the most: the agent keeps going while you are away from your desk. Joxo shows each agent's state on your phone and desktop, and when an agent stops to ask you something, that question reaches you wherever you are, so you can answer or tell it to change course. Setup is one line pasted into your agent. If your agent keeps hitting the same permission questions instead, see stop Claude Code asking for permission.

Frequently asked questions#

How do I stop Claude Code mid-task?#

Press Esc once. It stops the current response or tool call right away and keeps the work done so far. Press Esc twice on an empty prompt to open the rewind menu.

How do I undo what Claude Code just did?#

Run /rewind (or press Esc twice on an empty prompt), pick the prompt before the change and choose Restore code. Files changed only by shell commands aren't covered, so check git status and restore those with git restore.

Why does Claude Code keep making the same mistake?#

Its context fills with the failed attempts, and they steer the next try. Rewind past them, add one fact it didn't have, and ask for a diagnosis before any edit.