Why AI Agents Get Stuck in Loops (and Quit Too Early)

AI agents get stuck in loops, and quit too early, for the same reason: they’re bad at deciding how many times to do something. How many files to touch, how long to keep searching, when to stop. A study of transformers learning Collatz arithmetic found the same thing in miniature: 99.7% accuracy, and in over 90% of the failures, the model did the right calculation and miscounted the loop.
Why do LLMs struggle with math?
There’s a common narrative: “LLMs are bad at math.” People point to failures on arithmetic and conclude that language models lack numerical reasoning.
The odd part is that LLMs can handle hard mathematical reasoning. In July 2025 a Gemini Deep Think model scored 35 out of 42 at the International Mathematical Olympiad, gold-medal standard, writing its proofs in natural language within the 4.5-hour time limit.
The real problem is control flow
A November 2025 paper by François Charton and Ashvni Narayanan puts the failure somewhere more specific. They trained small transformers to compute long Collatz steps (a calculation with two loops whose length depends on the input) and concluded that the difficulty “lies in figuring the control structure of the computation,” meaning how many times each loop has to run. In plain terms: transformers break down on math when the problem needs control flow.
Control flow means:
- Loops that run an arbitrary number of times
- Branches that depend on runtime values
- Recursion with variable depth
- State that accumulates across steps
Traditional computers are perfect at this. A loop executes exactly as many times as needed, whether that’s 3 times or 3 million.
Transformers don’t work this way. Every token goes through the same fixed number of layers, and there’s no native loop construct. Every “iteration” has to be somehow encoded in the forward pass. Reasoning models get partway around this by thinking out loud, since each generated token is another pass through the network. But then the model is deciding how long to keep thinking, which looks to me like the same counting problem one level up.
Why do AI agents get stuck in loops?
AI agents (Claude Code, Cursor, and other agentic systems) are essentially LLMs trying to do control flow.
Think about what an agent does:
- Look at the current state
- Decide what action to take
- Execute the action
- Check if the goal is achieved
- If not, go to step 1
That’s a loop, which is exactly what transformers struggle with.
That’s why you see agents:
- Going in circles (loop that doesn’t terminate)
- Missing exit conditions (loop runs too long)
- Giving up too early (loop doesn’t run enough)
- Losing track of progress (state management across iterations)
Two of our own bots once argued on a pull request for 21 hours, 147 review runs, because nothing in either system had a way to say stop.
The 99.7% problem
In that paper, the best models (writing numbers in base 24, 16 or 32) reached 99.7% accuracy. That’s a high number for arithmetic, and the authors call it “a surprising result given the complexity of the task.”
The interesting part is where the mistakes went. The base-24 model made 243 mistakes on a test set of 100,000, and they piled up on the inputs that needed the longest loops. When the first loop ran 10 times, the model got 43% of them right. At 11 or more, it got none.
Show the data
| Inputs | Correct answers |
|---|---|
| All 100,000 test inputs | 99.7% |
| First loop runs 10 times | 43% |
| First loop runs 11 times or more | 0% |
And the mistakes weren’t random. The authors write that “in over 90% of failures, the model performs the correct calculation, but wrongly estimates loop lengths.” Hallucination, the failure everyone worries about, “almost never happens.”
The model can do the reasoning. What it can’t reliably determine is how much reasoning to do.
Which tasks agents handle well
When working with AI agents, keep in mind:
Tasks that work well:
- Fixed-step procedures
- Pattern matching
- Transformation with known structure
- Analysis with bounded scope
Tasks that struggle:
- “Keep going until done”
- Variable-depth recursion
- Finding something that might not exist
- Complex state management across many steps
How to stop an AI agent from looping
There are ways to work around the control flow problem:
Bounded iterations. Instead of “fix all bugs,” try “check for and fix up to 3 bugs.” Anthropic’s own guide to building effective agents describes agents as “LLMs using tools based on environmental feedback in a loop” and recommends “stopping conditions (such as a maximum number of iterations).”
Explicit state management. Give the agent a scratchpad and ask it to track progress explicitly.
Checkpoints. Break long tasks into phases with human verification between them.
Structured output. Ask for explicit loop tracking: “iteration 1 of N, current state: X, next action: Y.”
Early termination signals. Give clear criteria for when to stop.
We learned this one in production. Our company brain created 1,042 tasks in four months, one per action item, and a prose rule telling it to bundle them lasted two days. The fix was to take the counting away from the model and give it to a script. The model still extracts the action items and writes the titles. The script decides how many tasks exist.
The architectural limitation
Better prompts won’t fix this, and I doubt more training will either. The limitation comes from how transformers are built.
Some research directions that might help:
- Looped transformers that can iterate internally (a 2023 paper showed a looped 13-layer transformer can be programmed to run general-purpose programs)
- Hybrid architectures with explicit control flow primitives
- Tool use for algorithmic tasks (let the LLM decide WHAT to do, let code handle HOW MANY times)
For now the limitation is real, and knowing about it makes agent failures easier to predict.
Update, October 2026: the harness now owns the loop
Since I wrote this, Claude Code has started doing what the takeaway below recommends. Its best practices page starts from the same observation, “Claude stops when the work looks done,” and then lists ways to take the stopping decision away from the model doing the work:
- A
/goalcondition. A separate evaluator re-checks it after every turn, and Claude keeps working until the goal resolves. If Claude stalls, Claude Code eventually stops the run with the goal still set, so the loop has an exit too. - A Stop hook. A script you write runs your check and blocks the turn from ending until it passes, with a cap on how many times in a row it can block.
Both hand “are we done?” to something other than the model doing the work: a second model in one case, plain code in the other. That’s the same advice as below, now built into the tool.
The practical takeaway
When an AI agent fails, check whether the task needed it to decide how many times to do something: how many files to touch, how long to keep searching, when to stop. If it did, you’re fighting the architecture. Restructure the task, add a human checkpoint, or give the iteration to a tool.
Transformers weren’t built for loops, so don’t hand them the loop. Let code or a human own the iteration. That goes for review loops too. When I ran a panel of experts over our sales campaign, it kept finding problems for four rounds, and it took the reviewers telling me to stop before I did.
If you’re designing agents and want to find the loops before they find you, I do Claude Code coaching on your own code.


