Short answer
Work out the cost of each model call from its token usage, keep a running total for the run, and check the total before the next call starts. When the next call would go over your budget, end the run with an error you can catch and alert on.
A normal program costs the same every time you run it. An agent does not. How much it spends depends on how many steps it decides to take, and the model makes that decision. You need a budget that belongs to the run and ends it when it is used up.
Why agent cost grows faster than you expect
- Every step re-sends the history. The model reads the whole conversation again on each step. Step 30 costs much more than step 3, so the total grows faster than the number of steps.
- Big tool results. One call that returns a long web page or a large file puts all of it into every later step.
- Retries. A failing call that the agent retries ten times is billed ten times.
- Loops. A loop is just all of the above with no end.
- A bigger model than you planned for. A switch from a small model to a large one can multiply the price of every step.
The fix: a budget that lives outside the agent
Count the cost of every model call and refuse the next call once the run has used its budget. The key point is to check before the call, using a worst-case estimate, so the run stops under the limit and not just past it.
class BudgetExceeded(Exception):
pass
class Budget:
def __init__(self, max_usd):
self.max_usd = max_usd
self.spent = 0.0
def check_before(self, estimated_usd):
# Stop BEFORE the call that would take you over.
if self.spent + estimated_usd > self.max_usd:
raise BudgetExceeded(
f"next call would take spend to ${self.spent + estimated_usd:.3f} "
f"(limit ${self.max_usd:.2f})"
)
def record(self, input_tokens, output_tokens, in_per_m, out_per_m):
# Prices are per million tokens. Copy yours from your provider's price page.
self.spent += input_tokens / 1e6 * in_per_m + output_tokens / 1e6 * out_per_mUse it around each model call:
budget = Budget(max_usd=2.00)
while not done:
budget.check_before(estimated_usd=0.05) # your worst-case guess for one call
reply = call_model(messages)
budget.record(
reply.usage.input_tokens, reply.usage.output_tokens,
in_per_m=3.00, out_per_m=15.00, # example prices, use your own
)We ran this on 20 November 2026 with a limit of $0.05 and calls costing about $0.006 each. It made 8 calls, stopped with a clear error, and had spent $0.048. It never went over.
What this simple version does not cover
- Prices go stale. The price table is yours to keep up to date. A new model or a price change makes the numbers wrong without any error.
- Estimates can be wrong. If your worst-case guess is too low, a run can still creep over. If it is too high, runs stop early.
- Parallel calls. Several calls started at once can all pass the check before any of them is recorded.
- Streaming. A long streamed answer costs money while it is still arriving. A check between calls cannot stop it halfway.
- Several agents. A budget per agent does not limit what a team of agents spends together.
Doing it with SteerPlane
SteerPlane keeps a running total for each run and ends the run when it passes max_cost_usd. Install it with pip install steerplane:
from steerplane import SteerPlane
from steerplane.exceptions import CostLimitExceeded
sp = SteerPlane(agent_id="research_agent")
try:
with sp.run(max_cost_usd=0.05, max_steps=100) as run:
for i in range(1, 101):
run.log_step(f"read_page_{i}", tokens=1200, cost=0.008)
except CostLimitExceeded as e:
print(f"Stopped: {e}")On version 1.1.3 this ends the run at step 7 with a cost-limit error you can catch and alert on.
One thing to know. The total is checked after each step is logged. In our test the limit was $0.05 and the run stopped at $0.056, one step past it. Set the limit a little below the amount you can really afford to lose.
You report the cost of each step, or send your model calls through the SteerPlane gateway so it works the cost out for you. Both are covered on the setup page.
A checklist before you ship an agent
- Every run has its own cost limit, not just an account-wide cap at the provider.
- The limit is a small multiple of what a normal run costs.
- Hitting the limit raises an error you can alert on.
- You have a step limit as well, in case cost is reported wrongly.
- You have run an agent that is meant to overspend and watched it stop.