LLM API Cost Estimation and Model Selection: A Practical Guide

    This guide explains why the lowest per-token price often leads to budget overruns and how to evaluate LLM API costs accurately. It introduces a weighted effective price model based on cache-hit reads, uncached input, and output tokens, shows how to read pricing tiers instead of rankings, and presents a four-step selection process using a unit cost per successful task. It also covers engineering practices for cost control and common pitfalls to avoid.

    1. Why the Lowest Unit Price Often Gives the Wrong Answer

    When enterprises deploy LLM applications, exceeding the budget is one of the most common surprises. The cause is usually not a high unit price, but a pricing structure simplified into a single number per million tokens. Real billing involves at least three types of actions: reading repeated context that hits the cache, new input that misses the cache, and output generated by the model. The unit prices of these three can differ by several times, and their proportions depend heavily on the business workload.

    So the right starting point for cost estimation is not a price list, but quantifying your own traffic structure: how much of the traffic is repeated prefixes, how much new input each turn adds, and how output length is distributed. Only after you have these ratios does the unit price become meaningful.

    2. An Effective Unit Price Model: Compressing Three Prices into One Comparable Number

    Let the cache-hit read price be P_read, the uncached input price be P_in, and the output price be P_out, with corresponding shares w_r, w_i, and w_o (summing to 1):

    1. Effective unit price = w_r * P_read + w_i * P_in + w_o * P_out
    2. Unit: CNY per million tokens

    For long-context, fixed system prompt, multi-turn conversation workloads, a common set of assumptions is 80% cache-hit reads, 10% uncached input, and 10% output, so the weights become 0.8 / 0.1 / 0.1. The value of this model is threefold: it is reproducible, auditable, and recalculable as the business changes. The weights are not constants but configuration items that should be re-fitted weekly.

    It is important to note that cache hits do not happen automatically. They usually require the prefix to be byte-for-byte stable. If timestamps, random IDs, or dynamic retrieval results are placed before the system prompt, the hit rate can drop sharply. In engineering terms, put stable content first and volatile content last.

    3. How to Read the Leaderboard: Tiers, Not Rankings

    Sorting by effective unit price from low to high produces a reference leaderboard that roughly falls into four tiers (the values are illustrative ranges used to explain the tiering logic):

    Tier Effective unit price range (CNY per million tokens) Typical use cases
    Very low 0.3 ~ 1.0 Batch extraction, labeling, deduplication, offline cleaning
    Low-medium 1.0 ~ 5.0 Customer support assistance, code completion, general Q&A
    Medium-high 5.0 ~ 15.0 Complex reasoning, long-document understanding, agent orchestration
    High Above 15.0 Difficult reasoning, refined writing, strongly constrained tasks

    Two corrections must be flagged separately on the leaderboard. One is time-of-day pricing: the same model can have different unit prices during peak and off-peak hours, with a ratio of up to two times. The other is tiered pricing for long context: once the input exceeds a certain threshold, the unit price jumps to a higher tier. Ignoring these two factors will distort the ranking.

    4. From Leaderboard to Decision: A Four-Step Selection Method

    1. Build a baseline set. Sample 200–500 real requests, annotate the expected outputs, and form a regression evaluation set.
    2. Measure the three-way split empirically. Use real traffic to compute w_r / w_i / w_o rather than guessing.
    3. Calculate the effective unit price and sort, keeping only candidates with acceptable cost (usually 3–5).
    4. Within the candidate pool, compare quality pass rate and P95 latency, and make the final decision using the unit cost per successful task:
    1. Unit cost per success = Effective unit price / Task success rate

    This step is critical: a cheap model with only a 70% success rate may cost more in reality than a model twice as expensive, because failed requests trigger retries and manual fallback.

    5. Engineering Practices for Cost Governance

    • Prefix stabilization: place the system prompt, tool definitions, and examples at the front and manage them with version control to improve the hit rate.
    • Semantic caching: build a vector cache for high-frequency similar questions, served by general-purpose vector retrieval, with a similarity threshold and expiration policy.
    • Context trimming: use a sliding window plus summary compression to control the growth of uncached input.
    • Output constraints: use structured output and maximum length limits to curb output expansion, since output is usually the most expensive part.
    • Tiered routing: send simple requests to lower-tier models and escalate complex ones, with fallback based on confidence thresholds.
    • Observability: log the three token counts, cache status, and cost for each request in a logging service, and configure monitoring alerts and budget thresholds; batch processing and asynchronous queues can further smooth peaks, while object storage can hold evaluation sets and call details.

    6. Common Pitfalls

    The first is looking only at the output price while ignoring scenarios where input dominates. The second is treating the cache as enabled by default without prefix governance. The third is using average latency rather than percentiles for capacity planning. The fourth is not re-fitting the weights for a long time, so the cost model breaks after the business changes. The fifth is ignoring the hidden cost of failed retries.

    7. Conclusion

    The essence of cost optimization is mapping the billing structure into engineering constraints. First quantify the traffic structure, then compare horizontally using the weighted effective unit price, and finally close the loop with the unit cost per successful task. This keeps the budget under control without sacrificing quality. It is advisable to put the weights, the leaderboard, and the evaluation set under version control and review them weekly to form a sustainable cost loop.