How to stop Claude Code agents from looping and burning your quota
The expensive part of an unattended agent is not the work. It is the work that goes nowhere.
Four signs it is stuck
- The same failing command is retried again and again.
- A sweep keeps re-checking a file that has not changed.
- Two checks in a row show the same state, the same figure, the same output.
- It blames a tool or an API limit and tries the same call a third time.
Four stop rules to write down before you start
- No progress twice in a row: stop and rethink. Do not raise the limit.
- The same error twice: change the cause (a tool in a loop, an API limit) before retrying.
- Budget exceeded: the turns, minutes or tokens you gave in the brief are the limit.
- A conflict or a locked file: do not force it. Report it.
When a result comes back, allow at most three follow-up rounds, each with a reason. On the third, close it as applied or discarded. Nothing stays in limbo.
Brief it like a colleague
Write four things before launching a subagent or a repeating task: the goal and why, what it may touch, a budget, and the stops above.
A sentence is not a limit
Instructions in a prompt or in CLAUDE.md are context; the model usually follows them but nothing enforces them. For hard limits in scripts and automation, Claude Code's command line has --max-turns (limits agentic turns) and --max-budget-usd (stops once estimated spend reaches the amount), both for print mode (claude -p) only. Anthropic's CLI reference has the current details; the spend figure is a client-side estimate and can differ from your bill.
The four rules and the brief fit in one plain-text skill you can read in thirty seconds. It is free and open source (MIT): Loop-Stop on GitHub. The full story is in our dev.to article.
If you want the rest of what we use (cross-checking a "done" by another route, a quota budget before long jobs, a daily hand-off report, file-based memory between sessions), there is a paid pack of seven skills in the shop. It is optional; the free one stands on its own. We have not measured savings on other projects and promise none.
See also: Work toward a goal with /goal · Run Claude on a schedule with /loop.