LLM Inference Cost Modeling and Plan Selection: A Practical Guide

    Comparing per-token prices alone almost always leads to wrong decisions when choosing an LLM inference plan. This article shows how to replace price comparison with a reusable cost model based on cache hit rate, input/output ratio, peak-hour structure, and billing granularity. It covers engineering practices for improving prefix cache hits, modeling peak-hour capacity constraints, converting subscription plans into effective per-token prices, and shifting cost control left with budget guards and capacity planning. A checklist of common pitfalls helps readers validate model selection with their own traffic tests and billing regression instead of price labels.

    1. Why Comparing Unit Prices Almost Always Leads to the Wrong Answer

    The most common decision-making mistake in LLM application deployment is comparing only two numbers: the cached input price and the standard input price. This approach compresses a continuously varying workload into a single static scalar, and the conclusion often breaks down under real traffic. Actual bills are determined by four variables working together:

    • Cache hit rate: whether the stable prefix can be reused;
    • Input-to-output ratio: output tokens usually cost significantly more than input tokens;
    • Time-of-day structure: some services impose additional constraints during peak hours;
    • Billing granularity: pay-as-you-go and subscription plans use different conversion bases.

    Cost evaluation should therefore shift from “comparing unit prices” to “building a model.”

    2. Building a Reusable Cost Model

    Let T be the total number of tokens in a given time window, h the cache hit rate, a the input share, b the output share, p_c the cached input price, p_i the standard input price, and p_o the output price. Then:

    1. Cost = T * [ h * p_c + (1 - h) * a * p_i + b * p_o ]

    Using a set of illustrative values (in CNY per million tokens, p_c=0.115, p_i=4, p_o=16) to check two extreme workloads:

    1. def cost_per_100m(h, a=0.015, b=0.005, pc=0.115, pi=4.0, po=16.0):
    2. return 100 * (h * pc + (1 - h) * a * pi + b * po)
    3. print(cost_per_100m(0.98)) # about 12.6
    4. print(cost_per_100m(0.95)) # about 14.9

    The conclusion is clear: when the hit rate is extremely high, the cached input price is amplified into the dominant term, and the option with the lower cache price wins. When the hit rate falls back to around 95%, the uncached standard input begins to contribute significantly to cost, and the option with a higher cache price but cheaper standard input becomes the better deal. The crossover point is determined by the price structure, not by “who has the lower unit price.”

    3. Cache Hit Rate: The Only Engineering Variable Worth Investing In

    Hit rate is not an operations metric but an architecture metric. Practical measures include:

    1. Prefix stabilization: put the system prompt, tool descriptions, and few-shot examples at the very front in a fixed order, and never concatenate timestamps or random IDs.
    2. Session stickiness: route the same session to the same instance as much as possible to reduce cache eviction.
    3. Layered prompts: extract high-frequency, unchanging content into a separate prefix block and place low-frequency content after it.
    4. Observability: bring cache_read_tokens and cache_write_tokens into monitoring, alerting, and logging, and build dashboards by business line.

    It is worth noting that cache writes themselves may also be billed. Frequently rewriting the prefix can produce the counterintuitive result of “hit rate goes up but the bill does not go down.”

    4. Modeling Peak-Hour Constraints Correctly

    Peak hours should be treated as “reduced available capacity,” not “higher prices.” Let k be the share of peak duration and d the available throughput discount during peak hours. The effective weekly capacity is approximately:

    1. Capacity ≈ Base * [ (1 - k) + k * d ]

    Four peak hours per day correspond to k≈0.024, or about 20 hours over five working days per week. If peak coverage spans the entire working-day daytime, k rises above 0.3, and the capacity difference can reach several times. In engineering practice, batch jobs, offline evaluation, and log backfills should be scheduled into off-peak windows, leaving interactive requests for peak hours.

    5. Converting Pay-as-You-Go and Subscription Plans to an Equivalent Basis

    The real value of a plan lies in its “effective unit price”:

    1. effective_price = monthly plan fee / monthly redeemable token volume
    2. conversion ratio = effective_price / pay-as-you-go list price

    When the conversion ratio falls in the 50%–70% range, the plan is equivalent to a 30%–50% discount, but it also introduces constraints such as quota expiration, peak-hour throttling, and overage billed at the original rate. The decision rule can be simplified as:

    • Stable and predictable workload → subscription plan;
    • Clearly spiky workload with large cache-hit fluctuations → pay-as-you-go;
    • Mixed scenarios → plan as the baseline plus pay-as-you-go as the fallback, with overage accounted for separately.

    6. Shifting Cost Left: Budget Guardrails and Capacity Planning

    It is recommended to implement budget guardrails at the gateway layer. Example logic:

    1. class BudgetGuard:
    2. def __init__(self, monthly_cap, unit_price):
    3. self.cap, self.price, self.spent = monthly_cap, unit_price, 0.0
    4. def allow(self, tokens):
    5. if self.spent + tokens * self.price > self.cap:
    6. return False # degrade to a smaller model or queue
    7. self.spent += tokens * self.price
    8. return True

    Combined with tag-based cost allocation (by business line, environment, and tenant), this makes it possible to spot abnormal growth before the bill arrives. For capacity planning, feed three scenarios (conservative, baseline, optimistic) into the model separately to output the token budget and the scaling timeline.

    7. Common Pitfalls Checklist

    • Using averages instead of distributions: long-tail requests determine peak capacity;
    • Ignoring a rising output share: agent-style workloads are output-heavy and have a completely different cost structure;
    • Overlooking cache write amplification;
    • Treating peak-hour constraints as a pricing problem;
    • Plan quotas reset by calendar month, so cross-month workload migration causes waste.

    8. Conclusion

    The correct order for cost optimization is: first quantify the workload structure, then optimize hit rate and time-of-day scheduling, and only then compare commercial plans. General-purpose capabilities from Baidu AI Cloud (such as monitoring and alerting, logging, and elastic scaling on container platforms) can be used to support the guardrails and dashboards described above, but model selection itself should still come back to stress testing with your own traffic and billing regression validation—making decisions with data rather than unit-price labels.