Log in·Sign up
BestRelay

Back to the Journal

Agent Loops Need Spending Limits Before Better Prompts

An agent that repeatedly revises a plan, retries a failing tool, or rereads a growing conversation can spend money without producing useful progress. The safeguard is not a stronger instruction to be economical. It is a control layer that can refuse the next request. Build that layer before giving an agent unattended access to paid models.

Step one assigns every run a spending owner

Start with one identifiable run, one accountable owner, and one explicit budget. Attach the run identifier to model requests, tool activity, retries, and background jobs. Without that common identifier, a single task can look like several unrelated charges.

Choose limits for money, model calls, elapsed time, and concurrent work. Treat these as independent constraints: a cheap but endless loop still wastes resources. Define a successful outcome and a stopping condition before execution, such as completing one reviewed change or exhausting three unsuccessful repair attempts.

Step two reserves money before dispatching requests

Estimate an upper bound for each proposed call using its input size, maximum output allowance, and applicable rates. Reserve that amount against the run budget before sending the request. If insufficient budget remains, stop or request approval rather than hoping the response will be short.

Make budget checks atomic. Parallel workers must not each observe the same remaining balance and spend it independently. Include outstanding reservations when deciding whether another call can start. After receiving usage data, replace the reservation with the recorded cost. Treat missing usage as unresolved spending, not as zero.

Step three prices context and caches separately

Track ordinary input, generated output, cache reads, and cache writes according to the provider's billing categories. Do not assume cached tokens are free or add overlapping categories twice. Long histories can make successive turns expensive even when each answer is brief.

BestRelay bills pay-as-you-go from provider-reported token counts, with cache reads and writes priced separately. That makes usage reconciliation important: an application's estimate is a dispatch guard, not the final bill. Preserve each response's usage details and apply the rates effective for that request.

Prune irrelevant tool output, bound retrieved material, and summarize older context when appropriate. A cache may reduce repeated-input costs, but it does not replace a run budget.

Step four limits retries outside the model

Enforce iteration and retry limits in the orchestrator, not merely in the prompt. Separate transport retries from task-level attempts. A network timeout and a failed test are different events, but both can trigger additional spending.

Allow retries only for defined conditions, use bounded backoff, and stop repeated failures. A timeout does not prove that an upstream request never ran. Retrying may incur another charge, and interrupted streams may still have generated billable tokens. Keep uncertain requests visible in the ledger until reconciled.

Step five stops repetition without useful progress

Record compact fingerprints of tool calls, errors, and proposed actions. If the agent repeats the same failed operation without relevant new evidence, pause it. Also detect oscillation, such as repeatedly undoing and restoring the same change.

Require human approval before expanding the budget or starting a large parallel batch. On cancellation, prevent new dispatches and signal active workers to stop. Do not assume cancellation instantly eliminates charges for work already underway.

Step six verifies the complete request route

Test your controls through the actual client you will use. BestRelay works with Claude Code, Codex, Gemini CLI, and any OpenAI SDK. Its quality-first relay forwards each request to the official model named in it, without substitution, across Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok, and more. Exact routing preserves model choice; it does not bound agent spending.

Create a BestRelay account to test a tightly budgeted run. Simulate repeated tool failures, missing usage data, and simultaneous workers. Verify that each case blocks further dispatch when its limit is reached.

Step seven reconciles charges before scaling autonomy

Compare run-level estimates with provider-reported usage and billing records. Investigate discrepancies before increasing concurrency. Keep referral income separate from execution costs: BestRelay members earn a commission when invited people top up, but that income should never relax a run's limits. Scale autonomy only after the stopping rules survive failure tests.

Want to try the models discussed here? Sign up