New Tool Radar

AI Agents & Automation

How to Set a Cost Guardrail That Caps Agent Spend per Run

Cap what one agent run may spend, so a confused agent in a retry loop cannot burn the budget in an afternoon.

Schematic illustration with three step boxes: pick the cap, enforce it in code, and test the cutoff.

A confused agent in a retry loop can fire dozens of vision API calls per minute, and without a guardrail the bill follows. A cost guardrail is a hard circuit breaker: a maximum cost per run, a maximum number of actions, and a timeout, enforced in code around the loop. The guardrail must stop the run, not just warn about it.

Before you start

An agent loop where you can count cost as it runs, and a record of what normal runs cost.

Pick the cap from normal runs

Look at the cost of the last 20 normal runs and note the highest. Set the cap at about three times that number: high enough that normal work never trips it, low enough that a runaway loop stops early.

Set the action cap and the timeout alongside the cost cap: a run that is cheap but endless is still a failure.

Enforce the cap in code, around the loop

Wrap the agent loop in a guard that checks the running cost before each step and stops the run when the cap is reached. The check belongs in code, not in the prompt: the model cannot be trusted to count its own spending.

On hitting the cap, stop the run cleanly, save the partial work, log the cost, and escalate. Do not let the run finish one more step after the cap.

Test the cutoff on a looping task

Run a task designed to loop, such as a tool that always errors, and confirm the run stops at the cap with a clear message. Confirm the cost stays under the cap plus one step.

Check the normal runs too: none of the last 20 should have tripped the new cap.

Practice check: fictional Maple Corner agent, normal runs up to $0.12, cap $0.50

The table below is fictional practice material, worked out by hand from the stated cap. It is the expected result for the practice runs, not a run of any agent.

Run Cost Cap Outcome Verdict
Normal lookup $0.07 $0.50 Completed Pass
Normal summary $0.12 $0.50 Completed Pass
Looping task $0.50 $0.50 Stopped at cap Pass
No cap, looping task $4.80 None Ran until timeout Fail – no guardrail

Normal runs cost $0.07 to $0.12, well under the $0.50 cap. The looping task stops at $0.50, while the same task without a cap costs $4.80 before the timeout ends it.

Expected result. UNTESTED: the costs in the practice table are worked out by hand, not measured by running an agent. Set your own cap at three times your highest normal run cost, enforce it in code around the loop, and test it on a looping task.

Limits

  • A guardrail that only warns is not a guardrail: it must stop the run.
  • Set the action cap and the timeout with the cost cap: a cheap endless run is still a failure.

Sources

See all