OpenRouter ran six models, three times each, on a billing task built to fail. Without a guard, 4 of 18 runs retried all the way to a 40-step cap. With doom-loop detection on, 16 of 18 stopped after two real tool calls, and total spend fell from $0.29 to $0.05.
The figures come from OpenRouter's official tutorial of November 12, 2026, using its Agent SDK (@openrouter/agent 0.11.0).
Why do agents loop?
An agent loop only ends when the model stops calling tools and answers. When a tool keeps failing, the error goes back into the conversation, the prompt says finish the task, and retrying looks like the right step. Nothing counts attempts unless you add a counter.
Later turns also cost more: every turn resends the whole conversation, failures included. In one logged run, input grew from 131 tokens to 3,262 and cost per turn nearly quadrupled. With Mistral Large 4 and only a 40-step cap, the model made the identical call 41 times for $0.0309.
How much can it burn?
The tutorial gives no measured all-night bill and no per-model prices, so we don't invent one. Its warning that one night can reach thousands of dollars is a scenario: a pricier model plus many sessions at once. What was measured: 41 calls for $0.0309 without a guard; 2 calls for $0.0034 with detection. Results also vary: Grok 4.7 made 41, 12 and 41 calls on the same input, so one clean test proves nothing.
The 4 guards
1. Doom-loop detection. Set doomLoop: true in callModel. It fingerprints tool name plus arguments. Default ladder: observe at 2 repeats, block at 3, stop at 6.
2. loopKey. Models dodge detection by tweaking arguments (INV-2291 vs inv-2291). Normalise them, e.g. loopKey: ({ invoice_id }) => invoice_id.replace(/\D/g, ''). With it, 5 of 6 runs stopped after two real calls.
3. stopWhen caps. Recommended: stopWhen: [stepCountIs(15), maxCost(0.01), () => failuresInARow >= 5]. Normal Mistral Large 4 runs took 4 to 6 turns and $0.0018 to $0.0053, so this leaves room without allowing a 40-turn run.
4. API key credit limit. Set limit (USD) and limit_reset (daily, weekly, monthly) on the key. OpenRouter checks it before the request reaches a provider and returns 402 when exceeded. For BYOK, set include_byok_in_limit: true. The tutorial suggests no dollar amount.
No code? Do this
You can't change the first three inside an off-the-shelf product, but you can cap the API key's spend in OpenRouter or your provider's console, and avoid sharing one uncapped key across many agents.
Our take
The key limit is the most useful and easiest guard. The other three live in your process and can be missed or buggy; the key limit is checked server-side. A 3-cent demo isn't scary. Having no overall cap is.