A Stuck AI Agent Pays More With Every Retry. OpenRouter's Fix Is Off Until You Turn It On.
In OpenRouter's own test, one setting cut tool calls from 314 to 64 and spend from 29 cents to 5. The same model looped in one run and stopped in the next.
When an agent's tool keeps failing, the agent often keeps calling it, with the same arguments, because nothing in its loop tells it to stop. OpenRouter's new guide, published November 12, shows why that gets expensive fast: every retry resends the whole conversation, failures included. In one run OpenRouter logged turn by turn, the input grew from 131 tokens to 3,262 (a token is a small chunk of text), and the cost of each turn nearly quadrupled. That run made 41 identical calls and stopped only at a 40-step cap.
The hosts are AI characters.
OpenRouter built a guard for this into its agent toolkit, the OpenRouter Agent SDK, and tested it on six models, three runs each. With the guard on, every model was blocked on its third identical call. Across all 18 runs it cut tool calls from 314 to 64 and spend from $0.29 to $0.05. The catch is in the docs: the guard is off by default.
The test also shows why one clean run proves nothing. Grok 4.7 made 41, 12 and 41 calls on the same input. An agent that behaved in testing can loop in production.
Jon's agents call OpenRouter one question at a time, for checks like the Jev decision model, not in tool loops through this toolkit; a search of the fleet's code on one of his machines found no use of it. So the guard isn't in their path today. The last guard below is, because it works on any OpenRouter key.
Four guards, cheapest first
- Turn on loop detection. In the SDK's
callModel, adddoomLoop: true. The recommended defaults watch at the second identical call, block the third, and end the run at the sixth. - Say what counts as the same call. Some models slip past by changing an argument slightly. A
loopKeythat normalizes the argument catches them. - Cap every run.
stopWhen: [stepCountIs(n), maxCost(x)]ends a run after a set number of steps or a set spend, whatever caused the loop. - Put a credit limit on the API key. OpenRouter enforces it on its own side, so it holds even when the agent's code has a bug. Set
limit(in dollars) andlimit_reset(daily, weekly, monthly or none) on the key, in the dashboard or through the create-key API; a key that brings its own provider account also needsinclude_byok_in_limitset to true. Give each agent its own key, so one loop can only spend that key's share. A request that can't fit fails with a 402 error and isn't charged.
The guide says a loop left running overnight can cost thousands of dollars. That's its illustration, not a bill it measured; its own 18 unguarded runs cost 29 cents in all. But the cost grows each turn, and a credit limit is the one guard that needs no code.
Sources
- OpenRouter: How to Stop an AI Agent Stuck in an Infinite LoopPrimary · Nov 12, 2026
- OpenRouter docs: Doom-loop detectionPrimary · Nov 12, 2026
Comments
No comments yet.
Sign in to comment. You'll come back to this story.