Show notes
Budgets are a runtime control, not a report#
An AI product can be fast and reliable and still produce a bill nobody can explain. Spend control has to decide, at request time, which work may start, how much it may consume, and what happens near a limit.
Match the budget model to how your organization decides. A single monthly account limit is rarely enough; you may need platform, team, customer, project and per-workflow budgets. Keep those scopes explicit, allow a request only when it fits all of them, and say which policy blocked it without exposing another tenant's financial data.
A budget is not a rate limit. Rate limits control how fast work arrives, usually in requests or tokens per minute, and protect capacity. Budgets control how much value or money is consumed over a longer window and protect spend. A modest request rate can still create a large bill if it selects an expensive model or generates long outputs.
Accounting units, estimates and reservations#
Dollars are clearest for financial control, but tokens, image units, video seconds or tool execution time may suit product quotas better. Store the provider's raw usage and price inputs next to the normalized charge, along with model, provider, pricing version, currency and calculation timestamp, so invoices stay explainable when prices change or usage is reported late.
Before work starts, estimate the maximum likely charge from the model, input tokens, output cap, image size, video duration or tool plan. Reserve that amount atomically against every applicable budget. On completion, replace the reservation with actual usage and release the difference. Reservations stop many concurrent requests from seeing the same remaining balance and overspending it.
Estimates need guardrails. Do not trust a client-supplied max token field alone; apply a server-side ceiling. When an agent's plan expands, require a new authorization decision and reserve incrementally for long jobs. If the next step cannot be reserved, pause cleanly with an explanation instead of running until a card is declined.
A durable ledger and threshold behavior#
Each charge and reservation needs a stable operation ID, tenant, budget scope, amount, status and source request. A retried usage callback must not create a second charge. Use atomic balance or ledger transactions and reconcile against provider usage records. An in-memory counter is fine as a fast hint, but it cannot be the financial authority after a restart or during a multi-region race.
Decide what each threshold does:
- Warning: show remaining budget in the dashboard and return headers or metadata to authorized clients.
- Soft limit: route to a cheaper approved model, lower the output cap, or ask the user to confirm.
- Hard limit: reject new work with a stable error code and a retry or reset time.
Work already in flight needs its own rule: finish reserved work, stop safely, or allow a bounded overage.
Model-aware policies beat one blunt switch. A team might allow a low-cost model for experiments but require approval for a frontier model; a customer might have separate image, video and text allowances; a workflow might cap cost per successful task. Express these as data, evaluate them in one gateway layer, and log the reason for every decision.
Streaming, async jobs, retries and fallbacks#
For streaming, reserve against the output ceiling before opening the stream and settle when it closes or is interrupted. For queued image or video jobs, reserve before enqueueing, not when a worker picks the job up. Reconcile delayed webhooks by operation ID, and never release a reservation on client disconnect while the provider job keeps running.
Retries, fallbacks and hedged requests quietly multiply spend. Tie every attempt to one logical operation but account for each provider job that actually ran. Check the remaining budget before a fallback starts, and if a timeout leaves provider state uncertain, keep the reservation until the outcome is known. Idempotency prevents duplicate work; reservations stop a legitimate retry from bypassing a limit while the first attempt is unresolved.
Useful failures and enforcement during incidents#
A generic 429 or 402 leaves developers guessing. Return a stable error type, the scope that hit its limit, the reset time when it is safe to share, and a request or operation ID. Avoid revealing exact balances to untrusted callers. In interactive products, let users switch to an approved cheaper route or request an increase without losing their task.
Keep enforcement working when dependencies fail. If the pricing service is down, use a versioned local price snapshot with a fail-closed or bounded-degraded policy rather than allowing unlimited expensive requests. If the ledger is down, accept only work covered by a durable reservation or pause new work. Test partial outages, price updates, clock skew, duplicate webhooks and region partitions.
Observe spend and tune the policy#
Track estimated, reserved, actual, released, denied, overage and reconciled amounts by tenant, workflow, model family, provider and outcome, while keeping prompts, keys and sensitive outputs out of cost logs. Alert on unusual cost per successful task, fast reservation growth, fallback spikes and gaps between provider usage and your ledger.
Budgets that are too strict push teams to workarounds; budgets that are too loose are decorative. Start from historical usage, set warnings that leave time to act, and tune by workflow value. For experiments, a small hard cap plus an approved cheap model often beats blocking exploration. For production customer work, combine prepaid balance, reservations and clear overage rules. Crazyrouter, for example, uses pay-as-you-go billing behind one API key, which is the kind of gateway layer where these checks belong.
