Put a budget on an agent

DevOps, Hands On20 minutesFreeSteps checked 23 November 2026By Cloud Hands On

In this task you'll work out what an agent run costs and stop it before it spends too much.

Models charge per token (roughly four characters of English) for input (everything sent: instructions, tool descriptions, the whole conversation so far) and output (what it writes). An agent resends the growing conversation on every step, so input tokens rise with each tool call. Ten steps can cost far more than ten times one step.

Control it four ways: a budget per run that stops the loop, fewer steps (better tool descriptions), shorter context (trim or summarise old tool results), and the right model (a small, cheap model for easy jobs, a larger one only where it earns its price). Prices change, so keep them in one place and check the provider's pricing page.

Example. Fernhill Cloud's alert helper used a large model for every alert. Measuring showed 80% were simple 'is it still firing' checks. Routing those to a small model cut the monthly bill by about two thirds with no drop in the evaluation pass rate.

Before you start

You need: Python 3.10 or newer (on Windows, use py where the steps say python). No AI account: the practice model runs on your computer. oncall.py from the final project. Time: 20 minutes.

Where to look

  1. oncall.py
  2. PRICE
  3. run(budget_gbp=...)

Steps

  1. Read PRICE and the budget check in oncall.py (see Commands). The prices are Example figures in GBP per million tokens.
  2. Run the cost comparison from Commands: the same request on practice-small and practice-large. The large model costs about 16 times more for the same steps.
  3. Run with budget_gbp=0.0001: the agent stops after the first step with 'budget used up'.
  4. In trace.jsonl, follow input_tokens step by step and see it grow as tool results pile up.

Commands

PRICE = {"practice-small": (0.15, 0.60), "practice-large": (2.50, 10.00)}   # Example prices in GBP per million tokens

# In run(), after each model call:
pin, pout = PRICE[model.name]; u = reply["usage"]
trace.cost += (u["input_tokens"] * pin + u["output_tokens"] * pout) / 1_000_000
if trace.cost > budget_gbp: return f"Stopped: budget of GBP {budget_gbp} used up."

# Compare two models on the same request
python -c "import oncall; from practice_model import PracticeModel; q='shop-api is slow. Check its status and logs, then search the runbooks for the fix.'; a=oncall.Trace(); oncall.run(q, model=PracticeModel('practice-large'), trace=a, budget_gbp=1); b=oncall.Trace(); oncall.run(q, trace=b); print(a.cost, b.cost)"

# A budget that stops early
python -c "import oncall; print(oncall.run('shop-api is slow. Check its status and logs, then search the runbooks for the fix.', budget_gbp=0.0001))"

Watch out for

Cost
Free here. With a real provider, set a spending limit or budget alert on the account as well as in your code.
Security
A budget also limits damage: an agent stuck in a loop, or steered into one by injected text, can't run up a bill.
Common mistakes
Setting no limit, measuring only output tokens, and sending whole log files when the last 50 lines would do.

Clean up

Keep the agents folder for the next tasks in this section; delete it at the end.

Words to know

Token
The unit models charge by, about four characters of English.
Input tokens
Everything sent to the model on a call.
Budget
The most a run may spend before it stops.
Model routing
Sending each job to the cheapest model that does it well.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going