Loading lesson…
Coding agents can spiral: same edit, same test, same failure, forever. Learn to spot agent loops early, the patterns that cause them, and the interventions that actually break the cycle.
You ask Claude Code to fix a failing test. It edits a file. The test still fails. It edits the file again, almost the same way. The test still fails. Forty minutes later, your token bill is ugly, the test is still red, and the file is a worse version of where it started. You hit an agent loop.
| Loop type | Symptom | Trigger |
|---|---|---|
| Edit-and-retry | Same file edited 5+ times in a row | Test failure the model misreads as a code problem when it's an env problem |
| Tool ping-pong | Same two tools called in alternation | No success criterion — the model has nothing to stop on |
| Search spiral | Endless `Grep` then `Read`, never editing | Vague task that never resolves into action |
| Apology loop | "You're right, let me try again" with the same code | Model trusts your last message more than the file state |
INTERVENTION 1 — Force a diagnosis "Stop. Do not edit anything. In one paragraph, tell me: what do you think is broken, what evidence supports that, and what would prove you wrong?" INTERVENTION 2 — Reset the loop "Discard everything you've tried in this session. Start from `git status`. Tell me what's changed and what's not." INTERVENTION 3 — Hand back the keys "You're stuck. Stop tool use. List the three most likely root causes and what I — the human — should check on each."Each intervention forces the model out of action mode and into reasoning mode.Most loops happen because the task has no exit condition. "Make this work" is a loop machine. "Make `pytest tests/test_auth.py::test_login` pass, then stop" is not. Give the agent a specific test, a specific command, or a specific file diff that defines done.
# Claude Code prompt with a hard exit condition "Fix the failing test in tests/test_auth.py. Done is defined as: 1. `pytest tests/test_auth.py -x` exits 0. 2. No other test in the suite changes status. 3. The diff is under 30 lines. If you cannot meet all three within 5 attempts, stop and report."A bounded contract. The agent has somewhere to land and somewhere to fail gracefully.An agent loop is not a model failure. It is a missing exit condition.
— An infra engineer
The big idea: agents loop when they have no clear definition of done. Spot the patterns within three turns, intervene with a forced diagnosis or git reset, and design tasks with bounded success criteria. Stopping a loop early is worth more than letting any agent run a marathon.
10 questions · take it digitally for instant feedback at tendril.neural-forge.io/learn/quiz/end-coding-debug-agent-loops-creators
What is the main idea of "When Agent Loops Go Wrong — Detecting and Breaking Them"?
Which concept is most central to "When Agent Loops Go Wrong — Detecting and Breaking Them"?
Which use of AI fits this topic best?
Which limitation should you watch for in this topic?
What should a careful learner remember about "Loops cost real money"?
You want to use AI after this lesson. What is the safest next step?
How should AI output about agent loop be treated?
Name one way to verify an AI answer about agent loop.
Which action would help you apply "When Agent Loops Go Wrong — Detecting and Breaking Them" responsibly?
Which choice is a bad use of AI for this lesson?