The AI agent is stuck in a loop, making the same change over and over
Any AI coding tool Last checked 2026-10-14
The agent is working from a stale picture of your code. It makes an edit, re-reads something that contradicts it, and "fixes" it back. Stop the run — do not let it keep going — then give it one verifiable goal and a command that tells it when it is done.
What you saw
The agent edits a file, runs something, edits it back, runs it again. The diff oscillates between two states. Token spend climbs and nothing improves.
Variations: it calls the same tool with the same arguments repeatedly; it re-reads the same three files forever; it says "let me try a different approach" five times and tries the same approach.
What it actually means
Loops almost always come from a missing or ambiguous success condition. The agent has no way to know it has finished, so it keeps acting.
The other common cause is a contradiction in its context: two files, or a file and your instruction, disagree. It satisfies one, notices the other is now wrong, and flips.
Context compaction or truncation mid-task makes this much worse — the agent loses the memory of having already tried something, so it tries it again in good faith.
A third flavour is a tool that keeps failing the same way. If an error is not surfaced in a form the model can act on, it will retry it as if it were transient.
The fix
Interrupt it now. Every extra turn adds contradictory history that makes recovery harder and costs money.
Revert to the last clean state rather than negotiating with the mess.
git stash
# or
git checkout -- .
Start a fresh session. The looping conversation is now poisoned context — carrying it forward reproduces the loop.
Re-state the task with a machine-checkable finish line:
Goal: `npm test` passes with zero failures.
Change only src/auth/session.ts.
Run the tests after each change and show me the output.
If the same approach fails twice, stop and explain what you tried.
If it loops again, do the diagnosis yourself: run the failing command, read the real error, and hand the agent that error plus one file. Loops are usually an agent fighting a problem it cannot see.
How to stop it happening again
Always give a verifiable exit condition — a test, a build, a command whose exit code means done. "Make it better" has no stopping point.
Add a standing rule: if the same approach fails twice, stop and report instead of retrying.
Scope each run to one file or one small set. Broad write permission across a repo is where oscillation lives.
Keep your plan in a file the agent updates as it goes, so a compaction event does not erase the record of what has already been tried.
Watch the first two or three turns of any agent run. Loops are obvious early and expensive late.
Related questions
Should I just let it keep going?
No. A loop is not progress that needs more time; it is a missing stopping condition. Each turn costs tokens and adds contradictory history.
Why does restarting the session fix it?
Because the loop lives in the conversation history. A fresh window with a clear goal removes the contradiction that was driving the oscillation.
It keeps calling the same tool with the same arguments. Same thing?
Same root cause, tool-level. The call is failing in a way the model cannot interpret, so it retries. Run the call yourself, read the actual error, and give it that text.