Measuring LLM Inference Costs and Implementing Tiered Scheduling
This article shows how to build a quantifiable cost model for aggregated LLM API platforms without relying on official price lists. By applying linear regression to real call logs—input tokens, output tokens, cached tokens, and credit consumption over a time window—you can reverse-engineer the unit consumption rate of each model tier. It presents the fitting method with sample code, typical measured rates across four tiers, credit-to-token conversion estimates, a practical tiered routing strategy where expensive models handle reasoning and cheaper models handle execution, and the leverage of prompt caching. A final checklist covers instrumentation, calibration, routing, and periodic re-fitting to keep budgets accurate as billing rules change.
1. The Real Problem: Credit Anxiety Comes From Missing a Cost Model
When using an aggregated LLM API platform, many teams run into the same situation: credits drain far faster than expected, yet no one can explain where they went. The cause is usually not that “the models are too expensive,” but the absence of a quantifiable cost model. Without knowing how many credits each million tokens consumes, you cannot set budgets, build routing logic, or plan capacity.
The core method in this article does not rely on the official price list. Instead, it uses real call logs collected over a period of time—input tokens, output tokens, cache-hit tokens, and credit consumption at the start and end of each interval—to run a linear fit. This reverse-engineers the unit consumption rate of each model tier, which then drives the scheduling strategy.
One caveat up front: the reverse-engineered results are based on personal measurements. They only hold for a specific time window and a specific plan’s rules, and must be re-calibrated once those rules change.
2. The Fitting Method: Turning Bills Into Solvable Equations
Let credit consumption over a given time window be $C$, input tokens be $T{in}$, output tokens be $T{out}$, and cache-hit tokens be $T_{hit}$. The relationship is linear:
C = a * T_in + b * T_out + c * T_hit + e
Here, a, b, and c are the unit consumption rates (credits per 1M tokens) for input, output, and cache hits respectively, and e is the residual. As long as you collect enough samples (20 or more is recommended, with variables that are not highly collinear), you can solve for the coefficients using least squares.
The typical magnitudes obtained from measurement are as follows (credits per 1M tokens):
| Tier | Input | Output | Cache Hit |
|---|---|---|---|
| Flagship reasoning | ~80,000 | ~400,000 | Not tracked separately |
| Mid-to-high general | ~32,000 | ~112,000 | Not tracked separately |
| High-speed execution | ~4,000 | ~8,000 | ~80 |
| Lightweight batch | ~2,000 | ~2,000 | Not tracked separately |
The goodness of fit R² is close to 1.0 and the residual approaches 0, which shows that credit consumption is indeed linearly determined by token structure. Two key conclusions: the output cost of the flagship tier is about 200 times that of the lightweight tier; and after a cache hit, the high-speed execution tier costs only about 2% of the normal input cost.
Sample code for collection and fitting:
import numpy as np# rows: [T_in, T_out, T_hit, C]rows = np.array([[820000, 160000, 0, 129600],[410000, 80000, 0, 64800],# ... more real samples], dtype=float)X = rows[:, :3]y = rows[:, 3]coef, *_ = np.linalg.lstsq(X, y, rcond=None)pred = X @ coefr2 = 1 - ((y - pred) ** 2).sum() / ((y - y.mean()) ** 2).sum()print(coef, r2)
3. Credit Conversion: Actual Output Across Tiers
Based on “60,000 credits per 5 hours, 600,000 credits per week,” assuming input accounts for 80%, output for 20%, and ignoring cache for now, the total tokens that can be supported works out to:
- Flagship reasoning: ~417,000 tokens per 5 hours
- Mid-to-high general: ~1.25 million tokens per 5 hours
- High-speed execution: ~12.5 million tokens per 5 hours
- Lightweight batch: ~30 million tokens per 5 hours
The gap between the first and last is about 72 times. This means that if every task is handed to the flagship tier, the credits only support one medium-scale codebase understanding session. With sensible tiering, however, the same credits can cover a complete loop of “planning + full execution + batch validation.”
4. Tiered Scheduling Strategy: Use Expensive Models to Think
Based on the table above, a practical role division is:
- Flagship reasoning: requirement breakdown, solution selection, final code review;
- Mid-to-high general: complex logical reasoning, tricky defect localization;
- High-speed execution: main coding, refactoring, test completion;
- Lightweight batch: batch formatting, comment generation, simple classification.
The principle in one sentence: expensive models think, cheap models do. In engineering, this can be implemented with rule-based routing plus complexity scoring:
def route(task):if task.needs_arch_decision or task.is_final_review:return "flagship"if task.complexity >= 0.6:return "mid"if task.batch and task.simple:return "lite"return "fast"
5. The Leverage of Caching and Context
A cache hit can cost as little as 2% of the input price, so a “stable prefix” is the second-largest cost-reduction lever: place the system prompt, project conventions, and interface contracts at the very front of the context and keep them byte-level stable, while putting volatile content at the end; avoid repeatedly rewriting unchanging long background within a session. On the engineering side, this also needs logging services and monitoring alerts, with dashboards built by model tier, task type, and cache-hit rate, and the coefficients re-fitted every week to prevent budget distortion caused by rule changes.
6. Implementation Checklist and Risk Notes
- Instrument first: log the token structure and a credit snapshot for every call, otherwise fitting is impossible;
- Calibrate next: samples must cover different task shapes to avoid collinearity;
- Route afterward: tier by complexity, and set a per-task credit cap and circuit breaker;
- Review regularly: a sudden increase in fitting residuals usually means the billing rules or caching strategy have changed.
Conclusion: cost differences are not an uncontrollable black box, but a linear system that can be made explicit through data fitting. Once you build the cost model, tiered scheduling and cache reuse have a basis, and the effective output under limited credits can be systematically amplified.
