Skip to content
mentoor

Topic 5 of 5 · 5 min read

Budget alerts and hard spend caps

Goal: Set alert thresholds and a hard spend cap for a feature from its baseline usage and justify each number.

An alert tells a person that spend or latency left its normal range; a hard cap makes the system stop on its own. You need both, because the worst bills come from loops that run faster than anyone reads email.

Check yourself · 1 of 3

What is the main difference between an alert and a hard cap?

Choose one answer

Why it matters

An agent that retries on an error, a prompt that suddenly doubles in size or a user who scripts your endpoint can burn a month of budget in an afternoon. A monthly invoice is far too late a signal.

What to set

  1. Meter by feature and user. Every call carries a feature name and a user or tenant id, so spend can be summed per feature and per user.
  2. Learn the baseline. Take a few weeks of daily spend and p95 latency. Note the normal range, including weekday and weekend differences.
  3. Alert on deviation, in two tiers. A warning at about 1.5 times the normal daily spend, and a page at about 3 times. Alert on p95 latency exceeding the budget for a sustained window, such as 15 minutes, to avoid noise from single spikes.
  4. Add per-user limits. A daily token or cost limit per user stops one account from using the pool.
  5. Add hard caps. A maximum number of steps and a maximum cost per agent run, and a daily spend cap per feature. At the cap, the system stops calling the model and returns a clear message or a cheaper fallback.
  6. Set the provider-side limit (a monthly spend limit in the console) as a last line behind your own caps.

Before and after

Weak: an agent loop has "retry until the tool succeeds". A broken tool means unlimited calls.

Better:

const MAX_STEPS = 8;
const MAX_RUN_COST_USD = 0.25;

let cost = 0;
for (let step = 0; step < MAX_STEPS; step++) {
  const res = await callModel(messages);
  cost += priceOf(res.usage);
  if (cost > MAX_RUN_COST_USD) {
    throw new BudgetExceeded({ step, cost });
  }
  if (res.done) return res;
}
throw new BudgetExceeded({ reason: "max steps" });

A stuck run now stops after eight steps or 25 cents, whichever comes first, and the error is logged with the run id.

What goes wrong

  • Alerts with no owner. Name who gets paged and what they do.
  • Thresholds set once and never updated as usage grows, which leads to constant false alarms that people mute.
  • A hard cap with no message. Users should see why the feature stopped and what to do.
  • Caps that only count successful calls. Failed and retried calls spend money too.

Examples

Thresholds from a baseline

A summariser spends 20 to 30 dollars on weekdays and 8 to 12 dollars on weekends.

  • Warning at 45 dollars on a weekday (about 1.5 times the top of the range).
  • Page at 90 dollars (about 3 times).
  • Daily hard cap at 150 dollars: past it, requests get the cached summary or a polite message.
  • Per-user daily limit of 2 dollars, since the heaviest honest user last month spent 0.80.

Each number has a reason in the runbook, so the next person knows what to change when usage grows.

Agent loop caught by the run cap

At 02:10 a vendor API starts returning 500 errors. The agent keeps retrying with a growing context. With the run cap of 8 steps and 25 cents, each run stops at about 25 cents and logs BudgetExceeded.

The warning alert fires at 02:40 because failed runs are piling up. Without the cap, the same night would have cost the full daily budget many times over before anyone woke up.

Key points

  • Tag every model call with a feature and a user or tenant so that spend can be attributed and limited.
  • Derive alert thresholds from observed daily baselines, with a warning tier and a higher paging tier.
  • A hard cap stops the system itself: a maximum steps and cost per agent run, plus a daily cap per feature.
  • Alert on p95 latency sustained over a window, not on single slow requests.
  • Cap messages must tell the user what happened, and every alert needs a named owner and a first action.

Common mistakes

  • Relying on the provider's monthly invoice or a single monthly limit as the only control.
  • Setting a cap on cost but not on steps, so a cheap-per-step loop runs forever.
  • Paging on every slow request instead of a sustained p95 breach, which trains people to ignore alerts.
  • Leaving thresholds untouched after growth so they either never fire or always fire.

Check yourself · 2 of 3

A feature normally spends 20 to 30 dollars a day. Which warning threshold is best justified?

Choose one answer

Check yourself · 3 of 3

An agent retries a failing tool until it succeeds. Which change best prevents a runaway bill?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic