LLM API Cost Estimation and Architecture Selection: A Practical Guide

    This article presents a practical, product-agnostic framework for estimating large language model API costs and making architecture selection decisions. It breaks down billing into input, output, and cache-read components, shows how to calculate per-request and monthly costs, and explains why a lower unit price does not always mean a lower total cost. The article also covers architecture-level efficiency factors such as MoE and hybrid attention, along with actionable cost-reduction techniques including prefix caching, tiered routing, batching, output constraints, and observability. Readers will learn how to turn per-token pricing into a meaningful cost-per-task metric for production planning.

    1. Background: Why “Cheap” Does Not Mean “Cost-Effective”

    When integrating large language model capabilities into a business system, developers often run into a billing problem before an accuracy problem. A prototype call that looks negligible can quickly expose cost issues once it reaches production and is amplified by hundreds or thousands of high-frequency requests. Many teams simply compare unit prices and pick the cheapest option, but a real bill is determined by input length, output length, cache hit rate, peak concurrency, and retry strategy. Unit price is only one variable.

    A more reasonable approach is to treat model calls as an engineering budget: first build an itemized pricing model, then estimate per-request and monthly costs, and finally decide whether paying a bit more is worth it based on architectural characteristics. Using a general-purpose LLM service from a cloud provider running a limited-time 50% discount as the technical background, this article abstracts a measurement and selection method that is independent of any specific product.

    2. Cost Model: Breaking the Bill into Three Calculable Components

    Billing for mainstream LLM services typically revolves around three types of tokens, priced per million tokens:

    • Input cost: tokens used by the context and prompt;
    • Output cost: tokens generated by the model;
    • Cache-read cost: tokens recomputed after a prefix cache hit, priced significantly lower than normal input.

    Let the input unit price be $p{in}$, the output unit price be $p{out}$, and the cache-read unit price be $p{cache}$. For a single request with input tokens $T{in}$, output tokens $T{out}$, and cache-hit tokens $T{cache}$, the per-request cost can be written as:

    1. cost_once = (T_in - T_cache)/1e6 * p_in
    2. + T_cache/1e6 * p_cache
    3. + T_out/1e6 * p_out

    Take the “50% off for two weeks” scenario as an example, where the relationship between the original and discounted prices is $p’ = 0.5p$. Plugging in a medium-sized code repository analysis task with roughly 500K input tokens and 20K output tokens, with no cache hit:

    1. Before discount: 0.5*0.8 + 0.02*2.8 = 0.456 CNY/request
    2. After discount: 0.5*0.4 + 0.02*1.4 = 0.228 CNY/request

    That is a 50% saving per request. If monthly volume ranges from hundreds to thousands of calls, the monthly difference is on the order of a few hundred CNY. Three engineering details deserve emphasis here:

    1. Discounts are time-limited. When budgeting, you must distinguish “promotional-period cost” from “normal cost” and avoid using the discounted price for long-term capacity planning;
    2. Output tokens are usually more expensive than input tokens, so controlling generation length is often more effective than compressing the context;
    3. Cache reads only produce savings when the prompt prefix is stable and reusable; dynamically concatenated system prompts waste this benefit.

    3. Architecture Perspective: Why “More Expensive” Can Be Cheaper

    Comparing unit prices alone is meaningless—if generation quality falls short, no one will use the model even for free. Take the Mixture-of-Experts (MoE) architecture as an example: it uses a gating network to activate only part of the expert subnetworks, so the number of parameters involved in a single forward pass is far smaller than the total parameter count, trading compute budget for larger model capacity. Combined with a hybrid design of sparse attention and linear attention, the compute and memory overhead for long contexts no longer grows quadratically with sequence length but approaches linear growth.

    In engineering terms: for long-context, batch-analysis tasks, architectural efficiency directly translates into “fewer tokens and fewer retries needed to complete the same task,” which is the second source of cost. When evaluating, it is better to focus on three general metrics rather than vendor marketing claims:

    • Long-context stability: whether key information recall degrades with inputs above 32K;
    • Instruction-following rate: the pass rate of structured output (such as JSON) on the first attempt;
    • Retry rate and timeout rate: directly affecting effective cost and latency.

    4. Actionable Cost-Reduction Techniques

    1. Prefix cache reuse: place fixed content such as system prompts, tool descriptions, and code conventions at the very front, keeping it byte-level stable so cache reads take effect.
    2. Tiered routing: send simple classification and formatting tasks to smaller models or a rules engine, and invoke the LLM only for complex reasoning, forming a cost gradient.
    3. Batching and asynchronization: combine non-real-time tasks into batch submissions and use a message queue to smooth peaks, reducing peak compute usage.
    4. Output constraints: use explicit limits on word count, fields, and examples to constrain generation length and avoid wasted output from “lengthy explanations.”
    5. Observability: use logging services and monitoring alerts to record input/output tokens, cache hit rate, and failed retries for each request, forming a cost dashboard by business dimension.

    An illustrative call wrapper is shown below:

    1. def estimate_cost(t_in, t_out, t_cache, p_in, p_out, p_cache):
    2. return ((t_in - t_cache) * p_in + t_cache * p_cache + t_out * p_out) / 1e6
    3. # Estimation example with a fixed prefix hitting the cache (unit: CNY)
    4. print(estimate_cost(500_000, 20_000, 300_000, 0.4, 1.4, 0.115))

    5. Conclusion

    Cost management for LLM calls is essentially about restoring “unit price” to “cost per task.” First break down itemized pricing, then apply three correction factors—cache hit rate, output length, and retry rate—and finally use architectural efficiency to explain why a more expensive model may actually be cheaper. Limited-time discounts are only a short-term bonus; truly sustainable cost reduction comes from cache reuse, tiered routing, and an observability system. If an enterprise is already using a general-purpose LLM service on Baidu AI Cloud, it can apply the same formulas and engineering techniques to build its own technical ledger.