Why your AI agent loops forever — and how to stop it

First thing to understand: your agent isn't stuck. A hung process goes quiet. Yours is working away merrily — it's just doing the same thing for the twentieth time. Opening the same file, sending the same request, "verifying" what it already did.
That changes how you debug. You're not looking for the place where everything stopped. You're looking for the reason the agent can't tell the job is done.
There are usually three. In order of frequency.
Cause 1: the agent can't see the result of its own action
The most common one. The agent called a tool, the tool ran — and what came back was nothing: undefined, an empty string, null. To the model that reads as "nothing happened." The logical response is to try again.
That's the whole point of the agent loop: think → act → look at the result → think again. Remove step three and the loop closes on itself.
How to check. Log what goes back to the model, not what your function returns:
const result = await runTool(name, args);
console.log("→ going to the model:", JSON.stringify(result));
See undefined, {} or an empty string and you've found it. A frequent culprit: the function is async and the result was returned without await.
How to fix. Every tool returns text that asserts something. Not silence, but "Created config.json, 412 bytes." Even when there was nothing to do: "No records found, the table is empty" is an answer. "Nothing" isn't.
Cause 2: no definition of done, and no step limit
You asked: "Improve the code in this file." The agent improved it. Then asks itself: done? Unclear — "improve" has no end. So it improves it again. And again.
Same with state-shaped tasks: "make the tests pass." If a test fails for a reason the agent can't remove, it will keep fixing it forever — a different way each time.
How to check. Read your own task and answer in one sentence: what has to be on screen for this to count as finished? If you can't, neither can the agent.
How to fix.
- Give it a visible finish line. Not "improve," but "extract the repeated code into a function and show me the final file." A file exists — the work ends.
- Set a step limit. 10–15 covers almost everything. Hitting the limit isn't a failure; it's the safety catch doing its job.
- Split the task. Three small ones with clear endings beat one big one.
Cause 3: errors arrive with no detail
A tool failed and the model got back "Error" — or worse, a silent "success." Both lead into a loop: in the first case there's nothing to reason from, so it retries; in the second it's convinced everything went fine and wonders where the result is.
A familiar example: a tool call goes out with the wrong key, the server answers 401, and your wrapper swallows the error and returns an empty list. The agent sees "empty" and simply tries again.
How to check. Find the try/catch blocks in your tool code and look at the catch. If it says return null or a bare "error" — there it is.
How to fix. Return the error as text, like any other result:
catch (e) {
return `Error: ${e.message}. Check the API key and retry.`;
}
The model reads that and changes tactic — asks for a key, or takes another route. An error in the context is more useful than silence.
The emergency brake — install it first
Before fixing causes, put the limiters in. They're cheap and they save your token bill:
- Step limit — a hard cap on iterations.
- Repeat detector — remember the last 2–3 calls with their arguments. Third identical call in a row: stop and tell the agent, "You're repeating the same call and it isn't helping. Change approach."
- Budget cap — a token ceiling per task.
- A log of every step — what it called, with what, what came back. Without it you're guessing.
A different story is when the agent isn't looping but has forgotten what it already did: a long task outgrew the context window and the start of the conversation fell out. The symptom looks similar, the cure isn't — see why your bot forgets context.
Is this a bug in the model?
Almost never. Nine times out of ten it's the plumbing: a tool returned nothing, an error got lost, a task had no definition of done. The model is reacting honestly to what it can see.
Would a smarter model help?
A little: a stronger model notices sooner that it's going in circles. But if the result never reaches it, there's nothing to notice — the loop comes back, just more expensively.
Why did the bill grow so fast?
Every lap carries the whole conversation so far, and it keeps growing. Twenty laps isn't twenty requests at the price of the first — it's twenty increasingly long and expensive ones. Which is why the step limit goes in before debugging, not after.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





