Lesson 71.3 · 3 of 6 · 4 min read · Reviewed 24 Nov 2026
Budgets, quotas and alerts
By the end, you can
- Set budgets, quotas and cost alerts.
- Stop runaway spend early.
Runaway cost is usually a bug, not growth: a loop, a retry storm, a context that never shrinks.
Limits at every level
| Level | Example limit | On hit |
|---|---|---|
| Per call | max_tokens | Response stops at the cap |
| Per agent run | 25 steps, 200k tokens total | Stop and report |
| Per user per day | Quota on requests or tokens | Friendly limit message |
| Per tenant per month | Contract amount | Alert account team, then throttle |
| Per feature | Budget with forecast | Alert owner |
| Organisation | Spend limit in the provider console | Hard stop |
Alerts on rate
A monthly total tells you about last month. Chart cost per hour by feature and alert when it is, say, three times the usual for that hour. A loop shows up within minutes.
Decide actions in advance
Write down what happens at each threshold so on-call staff don’t have to invent it at 3 a.m.:
- 80% of budget: notify owner.
- 100%: switch feature to a cheaper route or reduce limits.
- 150%: disable non-critical feature with a kill switch (72.3).
Workspaces and keys
Use separate keys or workspaces per environment and feature, with their own limits, so a test script can’t spend the production budget.
Key points
- Set spending limits at several levels: per request (max tokens, max steps), per user or tenant, per feature, and organisation wide.
- Alert on cost rate, not only on monthly totals, so a runaway loop is caught in minutes.
- Decide in advance what happens at each limit: warn, slow down, degrade or stop.
Catch a runaway loop with a rate alert
Stop a looping agent with a step cap, catch a cost spike within the hour and plan each budget action in advance.
You will have: A script with a step cap, an hourly rate alert and a budget_action() that maps spend to a planned action.
Steps
- Run the starter: the loop runs 1,000 steps and nothing alerts.
- Stop the run at MAX_STEPS and report it.
- Alert on any hour where spend is more than 3 times the usual for that hour.
- Map 80%, 100% and 150% of budget to notify, cheaper route and kill switch.
Done when
- The run stops at step 25.
- The alert fires at 13:00, the first bad hour.
- 8,450 of 10,000 credits notifies the owner; 15,500 flips the kill switch.
Open in the Playground No API key needed.
Starter code (Python)
# Three guards against runaway spend: a step cap per run, an alert on cost rate, and planned budget actions.
MAX_STEPS = 25
def buggy_agent_step(step):
return "call_tool" # a loop bug: the agent never decides it is finished
def run_agent(max_steps):
steps = 0
while True:
steps += 1
if buggy_agent_step(steps) == "finish":
return f"finished after {steps} steps"
# TODO 1: stop and report when steps reaches max_steps
if steps >= 1000: # the Playground's own safety net, not yours
return f"ran {steps} steps before anything stopped it"
print(run_agent(MAX_STEPS))
# Credits spent per hour by one feature, against the usual spend for that hour.
USUAL = [4, 3, 3, 3, 4, 6, 10, 18, 25, 28, 30, 30, 29, 28, 27, 26, 24, 20, 16, 12, 9, 7, 5, 4]
TODAY = [4, 3, 3, 4, 4, 6, 11, 17, 26, 27, 31, 29, 30, 95, 210, 230, 220, 21, 15, 12, 8, 7, 5, 4]
def rate_alerts(usual, today, factor=3):
# TODO 2: return the hours where today's spend is more than factor times the usual spend for that hour
return []
alerts = rate_alerts(USUAL, TODAY)
print("ALERT hours:", ", ".join(f"{h:02d}:00" for h in alerts) if alerts else "none")
if alerts:
print(f"first alert at {alerts[0]:02d}:00, the monthly total would only show it weeks later")
def budget_action(spent, budget):
used = spent / budget
# TODO 3: 80% notify the owner, 100% switch to the cheaper route and lower limits, 150% kill switch
return "none"
for spent in [5000, 8450, 10200, 15500]:
print(f"spent {spent} of 10000 credits -> {budget_action(spent, 10000)}")
Show a solution
# Three guards against runaway spend: a step cap per run, an alert on cost rate, and planned budget actions.
MAX_STEPS = 25
def buggy_agent_step(step):
return "call_tool" # a loop bug: the agent never decides it is finished
def run_agent(max_steps):
steps = 0
while True:
steps += 1
if buggy_agent_step(steps) == "finish":
return f"finished after {steps} steps"
if steps >= max_steps:
return f"run stopped at step {steps}: step cap reached, reported to the owner"
print(run_agent(MAX_STEPS))
# Credits spent per hour by one feature, against the usual spend for that hour.
USUAL = [4, 3, 3, 3, 4, 6, 10, 18, 25, 28, 30, 30, 29, 28, 27, 26, 24, 20, 16, 12, 9, 7, 5, 4]
TODAY = [4, 3, 3, 4, 4, 6, 11, 17, 26, 27, 31, 29, 30, 95, 210, 230, 220, 21, 15, 12, 8, 7, 5, 4]
def rate_alerts(usual, today, factor=3):
return [h for h, (u, t) in enumerate(zip(usual, today)) if t > factor * u]
alerts = rate_alerts(USUAL, TODAY)
print("ALERT hours:", ", ".join(f"{h:02d}:00" for h in alerts) if alerts else "none")
if alerts:
print(f"first alert at {alerts[0]:02d}:00, the monthly total would only show it weeks later")
def budget_action(spent, budget):
used = spent / budget
if used >= 1.5:
return "kill switch: disable the feature"
if used >= 1.0:
return "switch to the cheaper route and lower limits"
if used >= 0.8:
return "notify the owner"
return "none"
for spent in [5000, 8450, 10200, 15500]:
print(f"spent {spent} of 10000 credits -> {budget_action(spent, 10000)}")
Do it Stop a runaway agent in the Playground in the Playground. No API key needed.