LLM API Pricing: Cost Logic and Model Selection Practices

    This article breaks down the cost structure behind large language model API pricing, including asymmetric input/output billing, prefix caching, and model tiers. It provides a reusable cost estimation model, practical optimization techniques, and a four-dimension framework for evaluating providers based on real workloads rather than list prices.

    1. The Problem: API Price Lists Keep Changing

    Over the past year or two, LLM API prices have been adjusted much more frequently. A single platform may introduce flagship, balanced, and lightweight tiers within a few months, along with caching discounts, volume-based pricing, and limited-time promotions. Similar capabilities across different platforms can also differ in price by several times. For developers, the real challenge is not “which one is cheaper,” but the fact that the price list itself has become a multidimensional, dynamic, and conditional function.

    It is worth emphasizing that price is a relative concept. A given tier may be an order of magnitude cheaper than the top flagship model, yet still not be at the low end of the overall market. Some tiers advertised as “low cost” can quickly lose their advantage under long-context, high-concurrency, or cache-miss scenarios. Therefore, discussing whether something is expensive or cheap without considering the actual business workload is meaningless.

    2. Breaking Down the Cost Structure: What You Are Actually Paying For

    2.1 Asymmetric Pricing for Input and Output

    The industry generally prices input tokens and output tokens separately, with the output unit price typically several times that of input. The reason is that the output stage requires token-by-token autoregressive generation, which cannot be fully parallelized and has lower compute utilization. This directly leads to a practical principle: output length is the primary amplifier of cost.

    2.2 Order-of-Magnitude Differences from Cache Hits

    Mainstream solutions generally support prefix caching: when the prompt prefix of a request matches previous requests, that portion of the input is billed at a significantly lower price. An illustrative price structure is shown below (for general expression only, not the pricing of any specific product):

    1. Input (cache miss) : 1.00x
    2. Input (cache hit) : 0.10x ~ 0.20x
    3. Output : 3.00x ~ 6.00x

    This means that placing stable system prompts, tool definitions, and knowledge base snippets at the front and keeping them unchanged is a high-return optimization.

    2.3 Model Tiering and Selection

    The same generation of models usually offers flagship, balanced, and lightweight tiers, with input unit prices possibly following a 5 : 2.5 : 1 gradient. Most business requests do not require the reasoning depth of the flagship tier. Getting the routing strategy right is often more effective than waiting for price cuts.

    3. A Reusable Cost Estimation Model

    It is recommended to write cost as a computable engineering object rather than judging by intuition:

    1. def estimate_cost(reqs, price):
    2. total = 0.0
    3. for r in reqs:
    4. in_hit = r["cached_input_tokens"]
    5. in_miss = r["input_tokens"] - in_hit
    6. total += (in_miss * price["in"]
    7. + in_hit * price["cached_in"]
    8. + r["output_tokens"] * price["out"]) / 1_000_000
    9. return total

    The key is to collect cached_input_tokens and output_tokens as first-class metrics. Without instrumentation for these two items, any selection comparison is an estimate rather than a measurement.

    4. Optimization Checklist

    • Prompt structure governance: place stable content first and volatile content last to maximize cache hit rate.
    • Output length constraints: use structured output, field trimming, and summarization instead of long-form generation.
    • Tiered routing: handle requests with the lightweight tier first, then escalate to the balanced or flagship tier when confidence is low or rules are triggered.
    • Batching and asynchronization: use asynchronous interfaces for offline tasks, trading throughput for unit price.
    • Semantic caching: directly reuse historical results for high-frequency repeated questions, reducing token consumption at the source.

    A simplified routing example:

    1. def route(task):
    2. if task["max_output_tokens"] > 2000 or task["needs_deep_reasoning"]:
    3. return "flagship"
    4. if task["simple_classification"]:
    5. return "light"
    6. return "balanced"

    5. Selection Framework: Four-Dimensional Evaluation

    1. Effective cost: measured against your own real workload, not by comparing list prices.
    2. Capability match: whether the task requires general capabilities such as long context, tool calling, or multimodality.
    3. Engineering stability: rate limiting policies, timeout retries, availability zone failover, and fallback plans.
    4. Portability: abstract a unified calling layer to avoid tight coupling between business code and a single interface form.

    A unified wrapper layer can significantly reduce switching costs:

    1. class LLMClient:
    2. def chat(self, messages, **kw): raise NotImplementedError
    3. def stream(self, messages, **kw): raise NotImplementedError

    6. Common Pitfalls

    • Looking only at input unit price while ignoring the output multiplier;
    • Assuming caching will always hit without monitoring hit rates;
    • Treating limited-time discounts as a long-term cost baseline;
    • Using the flagship tier across the board without a fallback path;
    • Discovering a lack of token-level observability only after cost overruns.

    7. Conclusion

    Rewriting the price list is the norm; engineering capability is the variable. By making token consumption, cache hit rate, tiered routing, and fallback strategies observable and adjustable infrastructure, you can keep costs under control amid price fluctuations. If you already use relevant general capabilities from Baidu AI Cloud (such as log services, monitoring and alerting, or object storage) to carry the calling pipeline, you can connect the above metrics to your existing observability system to form a continuous optimization loop.