The invoice API keeps timing out, but the Agent retries the same invoice over and over; you see call counts climbing while it never voluntarily stops. OpenRouter’s tests show that such loops not only repeatedly consume tool calls, but also carry an ever-growing conversation history into each round, making every call more expensive. It has added four safeguards to the Agent SDK: detect and block repeated calls, normalize parameters, cap the number of steps and cost per run, and use API key limits to enforce a ceiling on total spending. The goal is to stop the loop before the bill gets out of control.

These four safeguards are like four layers of insurance on an automated payment robot: the first spots when it is repeatedly pressing the same button, the second prevents it from slipping through by changing only one identifier, the third limits how many operations it can perform and how much it can spend in a run, and the last caps the total account spending at the platform level. The analogy highlights their different roles: the first two identify repetition, the third and fourth limit spending, and the platform limit provides the final backstop if the code fails.

Why Do Agents Become More Expensive?

Every retry sends the previous error back to the model, making the input longer and each round more costly. Agents become more expensive not merely because they run extra steps after a tool fails.

OpenRouter’s tutorial points to a loop as the cause: the tool continues to return errors, but the task instructions the model receives still require completion, so it treats another tool call as the next step. Once the error is written back into the conversation, the Agent does not automatically count its attempts unless developers add a separate stopping rule.

How One Failure Leads to the Next Round
Tool call fails
→
Error is written to the conversation
→
Model requests the tool again
→
Full history is sent again

Each round includes the previous failure information. The model can see that the service is still unavailable but may continue retrying; the added bill covers not only the current tool operation, but also the increasingly long contextual input.

In one round-by-round demo, OpenRouter measured input growing from 131 tokens to 3,262 tokens—about 24 times longer. Yet the per-round price of the same demo approached only four times its original level, so the increase cannot be read as a direct, proportional rise on a per-token basis.

Another demo configured persistent timeouts for an invoice lookup tool. The loop continued until the run ended, executing the same tool call 41 times at a cost of 0.0309 dollars. That bill stayed small only because the run had a step limit.

Without one, the OpenRouter Agent SDK—a development toolkit that lets models call tools and take consecutive actions—would keep running until the model stopped requesting tools. When a tool keeps failing, an Agent has no natural endpoint.

Blocked on the Third Identical Call

The model receives the previous tool result, then asks for the same tool with the same parameters again—that is the loop this mechanism blocks.

OpenRouter calls this “Unlucky Loop Detection”(identifying repeated tool calls and stopping the run). The default rules work in three stages: after 2 identical calls in succession, the system observes; on the 3rd, it blocks; and after 6 consecutive occurrences, it terminates the entire run. The observation stage does not immediately interrupt the model, the blocking stage stops the current call, and the termination stage ends the task.

What counts as “identical”? The tool name must match, the parameters must be equivalent, and the model must have received the previous execution result before it can make the same request again. If the model generates consecutive requests before seeing a result, they will not accumulate in this way.

Most loops do not reach the third occurrence. OpenRouter ran three tests on each of six current models, completing 18 tests in total; with detection enabled, 16 ended after only two actual tool calls.

Overall call volume also fell: with detection disabled, the 18 runs made 314 tool calls at a cost of 0.29 dollars; with it enabled, the total dropped to 64 calls and the cost fell to 0.05 dollars. These are the vendor’s own test results. No third-party retesting data is currently available, so they cannot be directly extrapolated to other models or different tasks.

OpenRouter: Announcements (RSS) official image 1
Official image 1 · Source: OpenRouter: Announcements (RSS) · Data definitions follow the original source

Can Slightly Changed Parameters Defeat Loop Detection?

Yes. With literal parameter comparisons, the model only needs to make a slight rewrite to create a “different request.” A more reliable approach is to normalize parameters first, then determine whether they belong to the same call.

Some models alter tool parameters before making what appears to be a different request. If the detector compares them literally, it may treat these requests as new calls and allow the loop to continue. The detector should compare what the parameters target, not how they are written.

loopKey(a marker that normalizes calls to the same identity) is designed for this scenario. Developers can specify parameter differences that should be ignored; the system normalizes them according to its rules before identifying repeated calls. Even if the model slightly adjusts a parameter, it has difficulty escaping detection by changing its wording, provided developers configure the relevant differences to be ignored in loopKey.

Normalization must also take the round in which the call occurs into account. Five identical calls within the same round count as one repetition, not five separate pieces of work. Detection focuses on repeated behavior within a round rather than simply accumulating call counts.

OpenRouter: Announcements (RSS) official image 2
Official image 2 · Source: OpenRouter: Announcements (RSS) · Data definitions follow the original source

The Third Safeguard: Braking a Single Run

A single run needs both a step limit and a cost limit: the former controls how far the loop can go, while the latter caps how much the run can spend.

OpenRouter’s Agent SDK—a development package for building multi-turn tool calls—puts stopping conditions in stopWhen(ending the current run when a condition is met), which can handle the step and cost conditions together. stepCountIs(stopping based on the number of run steps) controls how far the loop can go, while maxCost(stopping based on cumulative cost) caps how much the run can spend. The two limits contain different forms of runaway behavior: many calls that are individually cheap, or a relatively small number of calls that become expensive because of the model, tools, and context.

The step limit is not an exact “tool call counter.” When stepCountIs(40) reaches its limit, additional tool calls previously issued by the model may still execute once. Developers therefore cannot assume that “stopping” means no subsequent requests will occur; tool-side systems must still be designed to account for one possible extra call.

If stopWhen is not set at all, the Agent SDK will not guess when there is “enough” on developers’ behalf. It will keep running until the model stops requesting tools. Persistent tool failures, repeated model retries, or continuously changing parameters can all delay the end condition.

Stopping Checks During a Run
Agent requests a tool call
→
Check step limit
→
Check cumulative cost
→
Stop when either condition is met

These limits apply to each run and cover loops triggered by different causes, but they do not constrain total spending across all runs combined. The current OpenRouter Agent SDK version is 0.11.0, so developers need to verify their dependency version and configure the step and cost conditions separately rather than relying on only one safeguard.

Who Caps the Total Bill When the Code Fails?

When an Agent’s internal cost-control logic fails, OpenRouter still enforces a limit based on the API key’s total spending. Once a request exceeds the limit, the service fails it and returns 402; the over-limit request fails immediately, capping the bill there.

The first 3 safeguards all run inside the Agent: repeated-call detection identifies anomalies, parameter normalization identifies disguised repetitions, and step and cost limits constrain a single run. If the Agent code contains an error, these checks may stop functioning, but spending by requests using the same API key still accumulates.

Key limits are enforced on the server, so developers do not need to rely on the Agent voluntarily stopping or assume that every piece of local code works correctly. A code failure can affect how the Agent calls tools, but it cannot lift a spending ceiling already enforced on the server.

Enforcement Location What It Constrains Result After a Code Failure
Inside the Agent Repeated calls, steps and cost for a single run The relevant checks may not run
OpenRouter server Cumulative spending by API key Over-limit requests still fail

The first 3 safeguards govern the execution of a run, key limits govern the account’s total bill, and a single-run cost limit cannot serve as a backstop for total spending.

Limit-triggered feedback can also help identify the cause. A request that exceeds the key limit returns 402(indicating that the request was rejected due to a spending limit), and the limit_source field in the response is openrouter_key_limit. This field indicates that the rejection came from the key limit rather than the Agent’s internal duplicate detection or single-run limit, helping developers determine why spending was capped.

Back to the opening scene: when the invoice API times out again and the Agent is preparing to make another retry, the four safeguards will appear before the bill spirals out of control.

If the Agent must complete an invoice lookup before it can end the task, which safeguard should trigger first?

This article is based on the original OpenRouter: Announcements (RSS) post (2026-11-12). Figures published by the vendor, including benchmark scores and reductions, reflect official results and, unless otherwise noted, have not yet been independently retested by a third party.