key-scoped-usage-alerting · EN · 2026-10-22

Setting Up Spend Alerts That Actually Fire Before the Balance Is Gone

A practical guide to building spend alerts for an LLM API account: derive per-day thresholds from measured call volume, convert token usage into USDC cost using the platform's transparent pricing rule, and run a lightweight poller that checks usage programmatically instead of relying on dashboard visits.

Why balance-based alerts fire too late

Most API accounts only warn you when the remaining balance crosses a fixed number. That threshold says nothing about when the money is going. A key that burns through its budget in an afternoon and one that lasts a month can both trip the same "low balance" warning — the first one just trips it after the damage is done.

A spend alert should describe a rate, not a level. The useful question is: "Is today's burn faster than what I planned for?" Answering it requires two things — a threshold derived from your real usage, and a poller that reads usage on a schedule.

Step 1: Measure baseline call volume

Before setting any number, collect a few days (ideally at least a week) of raw usage. What you want per day:

  • Number of requests, split by model if you use more than one.
  • Input and output tokens per model, since they are priced differently.
  • Which workloads are routine (background jobs, batch embedding, cron summarisation) and which are bursty (interactive users, agent runs).

If your platform exposes a usage endpoint, pull this programmatically and store it. If it only shows usage in a dashboard, export or copy the daily totals into a small table. The point is to get numbers you can compute with, not impressions.

Step 2: Turn tokens into cost the way the platform does

The platform bills at the official model price multiplied by 1.3, so your cost model is simple:

  • For each model, take official input and output prices per token.
  • Multiply token counts by those prices.
  • Multiply the resulting subtotal by 1.3 to get what you actually pay in USDC.

Do this per model and per day. Aggregating by model matters because a cheap model can dominate request count while an expensive one dominates spend — a single blended number hides which lever to pull.

If you contribute keys to the platform, remember the inverse rule: credited usage pays official price × 1.1, or × 1.2 for premium, back to the contributor in USDC. That is a separate ledger from your spend and should not be mixed into the alert threshold.

Step 3: Derive per-day thresholds

With a week of daily cost figures you can set thresholds that are sensitive without being noisy. A workable pattern:

  • Soft threshold — roughly your typical daily spend plus a modest margin. Crossing it means "a human should glance at the graph today," not "stop everything."
  • Hard threshold — a multiple of the typical day that should only be reached during genuine incidents, such as a retry loop, a runaway agent, or a leaked key.
  • Balance floor — a separate check on remaining USDC that accounts for your historical daily burn, so you know how many days of runway are left rather than just a dollar figure.

The soft threshold should be derived from your median day, not someone else's numbers. If your usage varies heavily by weekday, keep separate weekday and weekend thresholds. Recompute the baseline on a schedule (monthly is usually enough) because model mix and prompt sizes drift.

Step 4: Build the poller

The poller is a small scheduled job — a cron entry, a serverless timer, or a loop in your existing worker — that does four things each run:

  1. Fetch current usage and balance from the platform's API using your existing key.
  2. Convert the delta since the last run into USDC cost with the ×1.3 rule.
  3. Accumulate today's total and compare it to the soft and hard thresholds; compare the balance to the floor.
  4. Emit a notification only on state changes — first crossing of soft, first crossing of hard, recovery back under threshold — so you are not spammed every minute.

Keep the poller's own state (last reading, today's accumulator, which alerts have fired) in a small persistent store. Polling more often than your usage granularity allows just adds jitter; once every few minutes is typically plenty for cost monitoring.

Step 5: Make alerts actionable

An alert that says "spend is high" is hard to act on. Include in the message:

  • Today's spend so far and the threshold it crossed.
  • The top model by cost today, and its share of the total.
  • The balance and estimated days of runway at the current rate.
  • A link or command to the provider's per-key usage, so you can find the culprit key.

Also alert on the absence of data. If the poller fails to fetch usage for several consecutive runs, that is itself an incident — the monitoring has gone blind.

Step 6: Test it before you need it

Trigger a synthetic crossing — temporarily set the soft threshold below current spend, or compute a fake reading in a test harness — and confirm the notification path works end to end. Then reset. An alerting pipeline that has never fired is an untested pipeline.

Finally, review thresholds after any real incident. If a runaway loop cost more than your hard threshold before anyone noticed, the poll interval or the hard threshold was wrong for your risk tolerance, and the next iteration should reflect that.