Reverse-Engineering LLM Token Costs and Tiered Routing in Practice
A practical guide to reconstructing per-token costs from opaque prepaid balances, fitting a linear usage model, converting a budget into throughput, and routing work across model tiers without treating demo prices as real list prices.
1. Why you need your own cost model
After LLM capabilities are wired into a product, teams often face an awkward gap: the bill is one aggregate number, while the business wants to know how much each completed task cost. When the platform meters usage through credits, quotas, or prepaid resource packs, that opacity gets worse. You can see the balance falling, but not whether the drop came from input length, output length, or cache hits.
A self-built cost model is useful for three reasons:
- Attribution. Break cost down to a single request, a business line, or a model tier.
- Forecasting. Given a budget, estimate how much call volume the system can carry and use that for capacity planning.
- Optimization. Find cost sinks—usually very long context and verbose output—and drive changes to prompts and routing.
2. Data collection: the prerequisite for reverse inference
Fit quality depends entirely on instrumentation. Each call should write at least one structured record. Suggested fields:
request_id unique request idts timestamp (milliseconds)model_tier model tier id (flagship / standard / lightweight)in_tokens input token countout_tokens output token count (include chain-of-thought; split it out if the platform meters it separately)cache_tokens cache-hit token countretry_cnt retry countstatus success / rate-limited / timeout
Three details that are easy to miss:
- Record raw usage, not an estimate. Guessing tokens by dividing character counts by a rule of thumb injects noise straight into the regression.
- Count retries on their own. Failed retries often consume real quota. Mixing them into successful samples pollutes the fit.
- Tag multimodal calls and tool calls separately. Images and audio are often billed in different units. Mixing them with plain text breaks the linear relationship.
3. Reverse inference: turn the bill into a set of linear equations
On most platforms, the cost of one call is linear in each token class. You can model it as:
C = a · T_in + b · T_out + c · T_cache
Here a, b, and c are the unknown unit costs (credits or currency per million tokens), and C is the quota consumed in that time window. Slice a day or a week into hours and you get N equations and 3 unknowns. Solve them by least squares:
solve min ‖ X·θ − C ‖²X = [[T_in, T_out, T_cache], ...] # N×3 usage matrixθ = [a, b, c]ᵀ # unknown unit costs
In engineering terms there are three steps: build the usage matrix, build the consumption vector, and call any available linear least-squares solver. Judge whether the fit is trustworthy with two checks:
- R² should be close to 1, which supports the linear assumption.
- Residuals should be near zero and show no systematic bias. If residuals grow monotonically with context length, there is tiered pricing or a long-text surcharge, and you need piecewise features.
The following figures are illustrative assumptions for demonstrating the method. They are not the price list of any real product:
| Tier | Input unit price | Output unit price | Cache-hit unit price |
|---|---|---|---|
| A (flagship reasoning) | 80 | 400 | 80 |
| B (standard reasoning) | 32 | 112 | 32 |
| C (general execution) | 4 | 8 | 0.08 |
| D (lightweight batch) | 2 | 2 | 2 |
Units are credits per million tokens. Two patterns show up. Output cost is typically 3–5× input cost, because generation cannot be parallelized the same way and compute utilization is lower. Cache-hit price is usually only a few percent of the normal input price, which is the most direct cost lever for long-context workloads. In the table, tier A output cost is 200× tier D. That gap is the reason to schedule by tier.
4. From unit price to throughput under a budget
Let the period budget be B, the share of input tokens be p, and the share of output tokens be 1−p. The weighted average cost per token is:
w = p·a + (1−p)·btotal tokens the budget can carry T = B / w
Using the table above, with p = 0.8 and cache ignored for now:
- Tier A:
w = 0.8×80 + 0.2×400 = 144. About 600,000 credits can carry roughly 417,000 tokens. - Tier B:
w = 48, about 1.25 million tokens. - Tier C:
w = 4.8, about 12.5 million tokens. - Tier D:
w = 2, about 30 million tokens.
Under the same budget, throughput differs by two orders of magnitude. This conversion is more useful for decisions than unit price alone: it turns “is it expensive?” into “is it enough?”. If cache hit rate rises to 60%, tier C’s effective input cost drops sharply and throughput can rise further. That is why it usually pays to pin stable prompt prefixes before you split work across model tiers.
5. Tiered routing: expensive models think, cheaper models execute
Cost optimization is not about squeezing the unit price. It is about spending expensive reasoning only where it is actually needed. A routing policy you can ship:
function route(task):if task.type in [batch extraction, format conversion, classification and labeling]:return tier_D # very short input and output; optimize for throughputif task.need_tool_loop and task.steps_est < 5:return tier_C # main execution path; best cost-performanceif task.need_multi_hop_reasoning:return tier_B # complex reasoningif task.need_planning or task.is_final_review:return tier_A # planning and final review; high unit price is acceptablereturn tier_C
Engineering constraints that go with it:
- Budget guardrails. Set a period quota per tier. Near the threshold, downgrade automatically and alert, so simple tasks do not exhaust the flagship tier.
- Observability. Send token usage, cache hit rate, and per-task cost to logging and alerting, and aggregate by business line.
- Cache first. Put system prompts, tool definitions, and long documents that stay stable at the front of the prompt, and keep them byte-for-byte identical, to maximize cache hits.
- Constrain output. Use structured output and a maximum length to shrink
T_out. This is usually the fastest cost cut. - Quality regression. Downgrades are not free. Keep shadow traffic for A/B checks and watch task success rate and human rework rate, so you do not save money by losing quality.
6. Iteration and caveats
The cost model is not a one-time deliverable. Metering rules, cache policy, and context windows can all change. Refit about once a week and monitor R² and residuals. A systematic drift in residuals means the pricing model has changed and you need to collect features again.
All unit prices and throughput numbers in this article are assumptions chosen to demonstrate the method. Real numbers must come from a fit against your own platform bills and instrumentation. Cloud providers differ widely in billing units, cache rules, and tiered pricing; copying someone else’s conclusions is an easy way to misjudge capacity. If you use a model service such as Baidu AI Cloud, treat the official billing documentation as the source of truth and validate it locally with the method above.
