Measuring LLM Inference Costs and Tiered Model Routing in Practice
This article shows how to measure real LLM inference costs from your own usage logs instead of relying on published price lists. It explains how to fit a linear model that separates input, output, and cache-hit consumption rates, how to convert a credit quota into usable tokens for each model tier, and how to route tasks across flagship, reasoning, light, and entry tiers. You will also find sampling caveats, a routing example, and common cost pitfalls such as cache invalidation.
1. Background: Why “Cheap” Often Isn’t Cheap
Once a team connects LLM capabilities to production systems, the most common complaint is not output quality but the bill. The same feature runs comfortably one day and exhausts the quota the next. Switching to a different model produces no visible quality gain, yet consumption multiplies.
The reason is that mainstream inference services usually bill across three dimensions priced separately: input, output, and cache hits. The per-unit price across those three tiers can differ by one to two orders of magnitude. Looking only at the “price per million tokens” column leads to the wrong conclusion.
Real workloads make this worse, because token structure is highly uneven. Long-document summarization and codebase comprehension tasks have huge inputs and tiny outputs; generative writing and structured extraction are the opposite. Without quantifying your own workload structure first, any unit-price comparison lacks a frame of reference.
So the core idea here is: ignore any official price list, run a regression fit over a period of real call records, back out the actual consumption rate of each tier under the credit/quota system, and build a cost model and routing strategy on top of that.
2. Method: Backing Out Unit Consumption Rates from Real Records
2.1 Build a Linear Model
Let the credit consumption of a call be C, input tokens $T{in}$, output tokens $T{out}$, and cache-hit tokens $T_{cache}$:
C = a * T_in + b * T_out + c * T_cache + e
Here a, b, and c are the unit consumption rates for input, output, and cache hits, and e is the error term. As long as you collect enough call records where the three variables are not fully collinear, ordinary least squares recovers the coefficients.
2.2 Sampling Caveats
- Samples must cover different input/output ratios, otherwise the coefficients are not identifiable (if every sample has a fixed input/output ratio, a and b become strongly correlated).
- Records must include timestamps and quota snapshots, so periodic settlement errors are not mistaken for model consumption.
- Drop samples from failed retries and interrupted streams; their billing basis often differs from successful requests.
A sketch of the collection and fitting flow:
import numpy as np# rows: (tokens_in, tokens_out, tokens_cached, credits_used)rows = load_usage_log()X = np.array([[r[0], r[1], r[2]] for r in rows], dtype=float)y = np.array([r[3] for r in rows], dtype=float)coef, *_ = np.linalg.lstsq(X, y, rcond=None)a, b, c = coefpred = X @ coefss_res = ((y - pred) ** 2).sum()ss_tot = ((y - y.mean()) ** 2).sum()r2 = 1 - ss_res / ss_totprint(f"input={a:.6f} output={b:.6f} cache={c:.6f} R2={r2:.6f}")
With good enough sampling, R² approaches 1 and residuals approach 0, which means the credit system prices tokens strictly linearly. This matters: cost can be modeled precisely rather than guessed after the fact.
2.3 Typical Magnitudes from the Fit
In my own measurements, consumption rates across tiers show roughly this gradient (in credits per million tokens; these are personal fitting results, not official data):
- Flagship: input around 80,000, output around 400,000;
- Mid-tier reasoning: input around 32,000, output around 112,000;
- Light execution: input around 4,000, output around 8,000, cache hit around 80;
- Fast entry: input and output both around 2,000.
Two conclusions are worth remembering: the flagship output rate can be about 200x the entry tier’s, and the cache-hit price is usually only a few percent of the normal input price, making it the biggest cost lever for repeated long-context calls.
3. Cost Estimation: Converting Credits into Usable Tokens
Let the credit quota be Q (for example 60,000 per 5 hours and 600,000 per week), with input share p and output share 1-p. Usable total tokens are approximately:
Tokens_total ≈ Q / (p * a + (1 - p) * b)
With p = 0.8 and cache ignored, the magnitudes above give roughly the following per 5 hours:
- Flagship: about 417,000 tokens;
- Mid-tier reasoning: about 1.25 million tokens;
- Light execution: about 12.5 million tokens;
- Entry: about 30 million tokens.
The same quota spans nearly two orders of magnitude across tiers. This explains a common pattern: teams are not “overusing” anything, they are leaving tasks on the expensive tier that could have been pushed down to a cheaper one.
Including cache extends the formula to:
Tokens_total ≈ Q / (p_in * a + p_cache * c + p_out * b)
In scenarios with very long repeated prefixes (a fixed system prompt, long code context, multi-turn Q&A over the same document), p_cache often accounts for more than half of the input, cutting overall cost by several times.
4. Tiered Routing: Expensive Models Think, Cheap Models Execute
Given that cost gradient, tier models by “thinking density” rather than by “how convenient they are to call”:
- Planning and review: task decomposition, approach selection, final code or copy review. Low volume, high value per call — use the flagship tier.
- Complex reasoning: multi-constraint logical reasoning, hard defect localization, long tool-call chain decisions — use the mid-tier reasoning model.
- Main execution: routine code generation, templated writing, structured extraction — use the light execution tier.
- Batch processing: classification and labeling, format conversion, summarization, bulk rewriting — use the entry tier and enable cache whenever possible.
In practice, add a routing layer that decouples “difficulty assessment” from “model selection”:
def route(task):if task.risk == "high" or task.needs_plan:return "tier_flagship"if task.reasoning_depth >= 3:return "tier_reasoning"if task.batch and task.prompt_prefix_cached:return "tier_entry"return "tier_light"
Supporting engineering practices:
- Provide a unified abstraction layer that hides call differences between tiers, making canary releases and rollbacks easy.
- Instrument the full path, recording input/output/cache tokens and credit consumption to form a data source for continuous fitting.
- Set budget guardrails with quotas and circuit breakers per session, per user, and per feature.
- Use output quality spot-check results to adjust routing thresholds in reverse, avoiding rework costs from cutting cost too aggressively.
5. Common Pitfalls and Risks
- Looking at unit price but not structure: the input/output ratio is the primary driver of cost.
- Ignoring cache invalidation: inserting a timestamp or random ID into the prefix voids the entire cache.
- Treating fitted results as official rules: the coefficients above are back-derived from personal measurements, and rules and quota policies can change at any time, so make fitting a recurring job rather than a one-time conclusion.
- Overlooking hidden costs: retries, over-long context truncation, and manual review all show up on the final bill.
6. Summary
Linearize the credit system, quantify the workload structure, and make model routing tiered. Once those three steps are done, inference cost stops being something you learn at month end and becomes an engineering metric you can predict in real time. Expensive models think, cheap models execute; combined with caching and prefix-stability optimization, this typically increases usable tokens by an order of magnitude without sacrificing quality on critical tasks.
