Reliability and Cost Engineering › Cost Budgets and Forecasting
Language AI HandbookAll chapters Estimate typical spend and the cost of the expensive requests separately. Longer model inputs and outputs can move per-request spend even before you count retries and tool calls.
For one generation in the simple two-price model below, multiply the number of billed input tokens and output tokens by their respective prices per token, and .
The sum is measured in dollars. In a provider bill, use billed token classes and the actual model rates, and include tool charges or cache-write fees when they apply. For a logical request with multiple attempts, sum the charges over the attempts. The rates below are illustrative.
PythonIn [19]Expand
# Illustrative prices (dollars per token). Replace with a provider's
# current published rates; do not hardcode stale prices in policy code.
P_IN = 3.00 / 1_000_000
P_OUT = 15.00 / 1_000_000
rng = np.random.default_rng(21)
N = 50_000
# Input tokens: fairly tight around 800, but sometimes a long RAG context.
n_in = np.where(
rng.random(N) < 0.05,
rng.lognormal(np.log(6000), 0.4, N),
rng.lognormal(np.log(800), 0.3, N),
)
# Output tokens: lognormal; a small fraction run very long (reasoning).
n_out = np.where(
rng.random(N) < 0.03,
rng.lognormal(np.log(2000), 0.5, N),
rng.lognormal(np.log(250), 0.4, N),
)
n_in = np.rint(n_in).astype(int)
n_out = np.rint(n_out).astype(int)
cost = P_IN * n_in + P_OUT * n_out
mean_cost = cost.mean()ConsoleOut [20]Expand
mean cost/request: $0.0083 p50 cost/request: $0.0065 p99 cost/request: $0.0451 p99.9 cost/request: $0.0816 1M req/mo @ mean: $8,338 1M req/mo @ p99 each: $45,141 (stress / mean 5.4x) share of total cost from top 1% requests: 7.32%
Multiplying the mean by one million forecasts spend if volume and the cost mix match this example. Pricing all one million requests at the per-request p99 is a stress scenario. It is neither a 99% upper bound nor the p99 of monthly spend. For a budget at a stated risk level, model volume, request mix, and shared high-cost periods across entire billing windows.
A spend burn rate compares the fraction of a budget used with the fraction of the window elapsed. At day 3 of a 30-day window, spending 20% of the budget gives . At that constant pace the budget runs out on day 15. The admission example below uses a separate one-day scope to demonstrate a soft warning and a hard limit on modeled reservations.
PythonIn [21]Expand
from decimal import Decimal
class BudgetGate:
"""Sequential gate for nonnegative estimates in decimal dollars."""
def __init__(self, soft_cap_usd, hard_cap_usd):
self.soft = Decimal(str(soft_cap_usd))
self.hard = Decimal(str(hard_cap_usd))
if not self.soft.is_finite() or not self.hard.is_finite():
raise ValueError("Caps must be finite.")
if not 0 <= self.soft <= self.hard:
raise ValueError("Caps must satisfy 0 <= soft <= hard.")
self.reserved = Decimal("0")
def check_and_reserve(self, est_cost):
estimate = Decimal(str(est_cost))
if not estimate.is_finite() or estimate < 0:
raise ValueError("Reserve a finite nonnegative estimate.")
projected = self.reserved + estimate
if projected > self.hard:
return "reject"
self.reserved = projected
return "allow_warn" if projected > self.soft else "allow"
def burn_rate(spent, elapsed_days, total_days, budget):
"""Projected budget multiple at the observed constant pace."""
projected = spent / elapsed_days * total_days
return projected / budget
gate = BudgetGate(soft_cap_usd="800.00", hard_cap_usd="1000.00")
estimated_request_cost = Decimal(str(mean_cost))
decisions = {"allow": 0, "allow_warn": 0, "reject": 0}
for _ in range(120_000):
decision = gate.check_and_reserve(estimated_request_cost)
decisions[decision] += 1ConsoleOut [22]Expand
daily decisions: {'allow': 95946, 'allow_warn': 23986, 'reject': 68}
reserved in modeled day: $1000.00 (hard cap $1000.00)
day 3 monthly burn rate: 2.00x (>1 means you will exhaust before the window closes)
pre-flight on near-full scope: rejectThe modeled gate warns above the soft cap and rejects estimates that would exceed the hard cap. It uses decimal dollars so that reserving $0.10 and then $0.20 against a $0.30 cap reaches the limit exactly. The second example offers a $0.10 reservation with only $0.05 remaining and rejects it. An actual hard cap requires atomic reservations across callers, a conservative bound on the cost of each admitted request, and reconciliation against usage. An estimate based on the sample mean can understate realized charges.
Monitor spend against the forecast during the window. If demand or the workload mix changes, recalculate the forecast and decide whether to cap output, reduce scope, or shift to a cheaper route.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!