Measuring LLM API Cost and Tiered Model Routing in Practice

    This article shows how to reverse-engineer the real cost of LLM API calls from your own usage data using multiple linear regression, presents measured cost tiers across four model classes, and explains how to build a tiered routing strategy that assigns expensive models to reasoning and cheaper models to execution. It also covers quota-to-token conversion, engineering guardrails, and common cost estimation pitfalls.

    1. Why Affordability Matters More Than Capability

    In day-to-day development of intelligent coding tools and AI agents, call volume rarely grows linearly. It expands exponentially with task chain length. A request that looks simple may involve a dozen or more model interactions across planning, retrieval, coding, testing, and review. When the per-call price varies by two orders of magnitude across model tiers, cost stops being a finance problem and becomes an architecture problem: it determines where in the pipeline you can place an agent, how many rounds of self-reflection you can allow, and whether batch processing over the full dataset is feasible.

    Many teams either estimate costs by guesswork or simply multiply the published price by an expected call count. Both approaches tend to break down in production, for three reasons. First, billing usually spans input, output, and cache hits, and the unit prices for these three can differ dramatically. Second, the real input-to-output ratio often defies intuition; in long-context scenarios, input frequently accounts for more than eighty percent of tokens. Third, quotas typically reset on rolling time windows rather than as a simple monthly total.

    2. Method: Deriving Unit Cost From Real Usage

    A more reliable approach is to treat quota consumption as the dependent variable and the input, output, and cache-hit token counts as independent variables, then run a multiple linear regression. As long as you can collect several call records of varying shapes, you can solve for the implied unit price of each model tier.

    The general implementation logic looks like this (it expresses only the common method and involves no specific API):

    1. # rows: each record contains quota consumed y within the window and three token counts
    2. # columns of X are [input, output, cache hit, constant]
    3. beta, *_ = np.linalg.lstsq(X, y, rcond=None)
    4. pred = X @ beta
    5. r2 = 1 - ((y - pred) ** 2).sum() / ((y - y.mean()) ** 2).sum()
    6. print(beta, r2, (y - pred).max())

    To judge whether the fit is trustworthy, look at three metrics: whether the coefficient of determination is close to 1, whether the residuals are close to 0, and whether the maximum absolute error falls within the minimum billing granularity of a single call. Only when all three hold does the reverse-engineered unit price carry engineering value.

    3. Measured Results: The Cost Gradient Across Tiers

    Fitting the method above against actual usage records yields the implied consumption rates for four typical tiers (in “quota per million tokens”; these are values reverse-engineered from personal measurements, not official pricing):

    Tier Input Output Cache hit
    Flagship reasoning ~80,000 ~400,000 Not tracked separately
    Balanced reasoning ~32,000 ~112,000 Not tracked separately
    Fast execution ~4,000 ~8,000 ~80
    Lightweight batch ~2,000 ~2,000 Same as input

    Two conclusions stand out. First, there is a clear multiplier between output and input: the flagship tier’s output price is about five times its input price, which means “letting the model write one more explanation” is far more expensive than “letting it read one more chunk of context.” Second, the marginal cost of a cache hit is extremely low; for the fast execution tier, a hit costs only about two percent of the normal input price, while the lightweight tier prices input and output the same, giving it the most predictable behavior. The flagship tier’s output cost is roughly two hundred times that of the lightweight tier, and this gradient is the foundation for every routing strategy that follows.

    4. How Many Tokens a Quota Buys: Conversion and Calibrated Intuition

    Assume the quota resets on both a “five-hour window” and a “weekly window” on a rolling basis, and that the workload is roughly eighty percent input, twenty percent output, with cache not counted for now. This converts to the usable token scale per window for each tier: about 400,000-plus for flagship, about 1.2 million for balanced, about 12 million-plus for fast execution, and about 30 million for lightweight.

    The intuition these numbers convey is that the effective throughput gap between the most and least expensive tiers approaches two orders of magnitude. So using the flagship tier for a task like “traversing every file” is like doing a carpet search with a high-powered telescope.

    5. Tiered Routing: Let Expensive Models Think and Cheap Models Execute

    The core strategy is to assign tiers by cognitive load, not by project importance.

    • Flagship reasoning: handles task decomposition, solution selection, and final code review; characterized by few calls and high value per call.
    • Balanced reasoning: handles complex cross-file reasoning, tricky defect localization, and refactoring design.
    • Fast execution: carries the bulk of coding, unit test generation, and script writing—high-frequency tasks whose marginal cost can be pushed lower with cache reuse.
    • Lightweight batch: handles deterministic tasks such as batch formatting, comment completion, log categorization, and simple classification.

    A workable skeleton for a routing middleware:

    1. def route(task):
    2. if task.needs_global_plan or task.is_final_review:
    3. return "flagship"
    4. if task.cross_file_reasoning:
    5. return "balanced"
    6. if task.deterministic and task.batch_size > 50:
    7. return "lite"
    8. return "fast"

    Three engineering constraints come with this. First, instrument every call to record the three token counts and the quota change, continuously recalibrating unit prices. Second, keep reusable system prompts and repository context up front and stable to raise the cache hit rate. Third, set tier-downgrade and circuit-breaker thresholds so that once more than half the window’s quota is consumed, non-critical tasks automatically sink to lower-cost tiers.

    6. Common Pitfalls and Risks

    Do not extrapolate long-term cost from a single window’s sample; the reset boundaries of rolling windows introduce noticeable bias. Do not treat reverse-engineered unit prices as official billing rules; they hold only under your own load distribution. Do not ignore the output multiplier; verbose chains of thought and repeated explanations are invisible amplifiers. Do not assume caching is a default benefit; it only takes effect when the prefix is stable and reuse is sufficient.

    In summary, the key to cost governance is not finding “the cheapest model” but building a measurable cost model and designing tiered routing and downgrade mechanisms around it. Note that all figures here are personal reverse-engineered measurements; the rules may change as platform policies adjust, so re-fit and validate against your own instrumentation data before putting this into practice.