How to Calculate LLM Token Costs: Fitting and Practicing Tiered Routing

    Large language model usage is billed by tokens or deducted from a credit quota, and the same task can consume vastly different amounts of quota across models. This article explains how to break down token costs into input, output, and cached-token components, how to reverse-engineer effective consumption rates from usage logs using least-squares fitting, how to convert a quota into an estimate of achievable workload, and how to apply tiered model routing—using high-capability models for planning and review, and cheaper models for bulk execution—to control costs.

    1. Why Token Costs Need to Be Calculated Carefully

    LLM calls are billed per token or deducted from a credit quota. Under a quota system, the same task can consume dozens of times more quota on one model than on another. If a team only focuses on model capability without accounting for unit consumption, it is easy to waste quota on simple work while leaving insufficient reasoning capacity for the steps that matter most.

    Cost accounting should answer three questions: how much each type of call actually costs, which steps justify using a high-capability model, and how much work can be completed within a given quota.

    2. What Variables Make Up the Cost

    The consumption of a single call typically consists of three parts:

    • Input tokens: the prompt, context, conversation history, and retrieved materials.
    • Output tokens: the content generated by the model. In most billing rules, the output unit price is higher than the input price.
    • Cached tokens: when the request prefix matches a previous request, some platforms price this portion of the input at a lower rate.

    The cost of a single call can be written in linear form:

    Cost per call = input tokens × input rate + output tokens × output rate + cached tokens × cache rate

    Here, the rate is the quota deducted per token. Rates differ across models, and the rate for the same model may change after a pricing adjustment. Therefore, rates should be managed as configurable parameters rather than hard-coded into business logic.

    The following example uses hypothetical values solely to demonstrate the calculation:

    1. # Unit: credits per token; values are hypothetical
    2. RATES = {
    3. "tier_a": {"input": 0.0010, "output": 0.0050, "cache": 0.00003},
    4. "tier_b": {"input": 0.0100, "output": 0.0300, "cache": 0.00050},
    5. "tier_c": {"input": 0.0005, "output": 0.0010, "cache": 0.00001},
    6. }
    7. def estimate_cost(tier, input_tokens, output_tokens, cache_tokens=0):
    8. r = RATES[tier]
    9. return (input_tokens * r["input"]
    10. + output_tokens * r["output"]
    11. + cache_tokens * r["cache"])

    3. Reverse-Engineering Rates from Usage Records

    Most platforms do not publish complete billing details, but bills or call logs usually record the token counts and the quota deducted for each request. Once these records are aggregated, least-squares regression can be used to back out the consumption rate for each token type.

    The steps are as follows:

    1. Export call records for a period of time. Each record should at least include input tokens, output tokens, cached tokens, and the quota deducted for that call.
    2. Group records by model and build a regression equation for each group: y = a·x_in + b·x_out + c·x_cache.
    3. Solve for the coefficients a, b, and c, which are the estimated consumption rates.
    4. Compute the coefficient of determination R² and the residual for each record to check fit quality.

    The pseudocode is as follows:

    1. X = [[in_1, out_1, cache_1], [in_2, out_2, cache_2], ...]
    2. y = [credit_1, credit_2, ...]
    3. a, b, c = least_squares(X, y)
    4. residuals = [y_i - (a * in_i + b * out_i + c * cache_i) for each record i]
    5. r2 = 1 - sum(res ** 2 for res in residuals) / sum((y_i - mean(y)) ** 2 for y_i in y)

    Several caveats apply when using this method:

    • An R² close to 1 only means the linear model fits this batch of records well; it does not mean the fitted values match the official price list.
    • If input and output always grow in the same proportion across samples, the two coefficients cannot be distinguished and the fit becomes unstable. Samples need to cover different scenarios: long input with short output, short input with long output, with and without cache hits.
    • Examine the distribution of residuals. If residuals cluster in certain time periods or request types, it usually indicates unmodeled billing factors, such as a fixed per-request charge or time-varying coefficients.
    • When pricing rules change, old coefficients become invalid, so the fit should be recalculated regularly.

    4. Converting Rates into Actionable Workload Estimates

    Teams care more about how much work a quota can support. Let Q be the quota for a period, r the share of input tokens, 1−r the share of output tokens, and a and b the input and output consumption rates (in credits per token). The total tokens that can be processed in that period are approximately:

    T ≈ Q / (r·a + (1−r)·b)

    Assuming 80% input and 20% output per period, the processable volume under the same quota differs noticeably across models. The difference comes mainly from the output rate, so tasks with long outputs are more cost-sensitive.

    Two assumptions in this formula should be replaced with real data. First, the input/output ratio r should be derived from log statistics rather than set by intuition. Second, when the cache hit ratio is high, the effective input rate drops, so the cache term must be included separately. If quota is settled per model or per period, each pool must be accounted for individually rather than summed directly.

    5. Tiered Routing by Task Complexity

    When unit prices differ significantly, the most effective approach is to let different stages use models of different capability tiers. A common division of labor is:

    • Planning and review: task decomposition, solution evaluation, and final result checking. These steps determine direction and carry a high cost of error, so they suit the most capable—and most expensive—model, but are called infrequently.
    • Complex reasoning: multi-step derivation, constraint solving, or deep analysis of subproblems. Suitable for mid-to-high-tier models.
    • Main execution: code modification, text generation, data transformation, and other routine work. High call volume with stability requirements; suitable for cost-effective mid-tier models.
    • Bulk simple tasks: classification, extraction, formatting, and other work with fixed patterns. Suitable for the cheapest lightweight models, combined with rule-based validation.

    The core principle can be summarized as: use high-priced capability for decisions, and low-priced capability for mass execution.

    An example routing configuration:

    1. ROUTES = {
    2. "plan": "tier_b",
    3. "review": "tier_b",
    4. "reason": "tier_b",
    5. "execute": "tier_a",
    6. "batch": "tier_c",
    7. }
    8. def route(task_type):
    9. return ROUTES.get(task_type, "tier_a")

    The routing table also needs an accompanying escalation mechanism. If a lightweight model’s output fails validation, the request is retried on the next higher tier, and each escalation is recorded. A persistently high escalation rate indicates that the task type is routed to a tier that is too low for its actual complexity, and the routing configuration should be adjusted accordingly.