LLM API Cost Calculation: Why You Shouldn't Ignore Cache Hit Rate

    This article breaks down how prompt caching affects the real cost of Large Language Model APIs. It explains the math behind cache-based input pricing, the inference architecture needed to sustain high hit rates, and practical prompt design habits that help developers maximize savings. Readers will learn a three-dimensional framework for evaluating API costs based on list price, underlying architecture, and measured cache performance.

    Introduction: The Overlooked Cost Iceberg

    When integrating large language models into enterprise applications, cost evaluation is often one of the core metrics for technology selection. However, many architects and developers habitually look up the “standard pricing” on each vendor’s official website and multiply it by the estimated token throughput. This approach has a massive blind spot: it ignores the most critical variable in inference billing logic—the cache hit rate.

    In large-scale conversational AI, long-document analysis, or multi-turn agent tasks, the context often contains a great deal of repetition. If the underlying inference platform has efficient cache reuse capabilities, a difference in cache hit rate from 0% to 90% will directly create a 3x to 5x gap in input-side costs. This article strips away specific product attributes and provides an in-depth analysis of the caching billing mechanism for LLM APIs from a general technical perspective, while exploring how to achieve extreme cost compression through underlying architecture optimization.

    1. The Math Behind Cache Billing Logic

    To understand why the actual price can drop to 25% of the list price, we first need to establish a general billing model. Mainstream LLM inference services typically split input (prompt) billing into two parts: the cache-hit portion and the cache-miss portion.

    Assume a general multimodal LLM has a standard input price of 4 RMB per million tokens, and the cached portion enjoys a discount of roughly 17.5% of the standard rate, priced at 0.7 RMB per million tokens.

    When the cache hit rate reaches 90%, the actual input cost for 1 million tokens is calculated as follows:

    • Cache-hit portion (90%): 0.9 × 0.7 RMB = 0.63 RMB
    • Cache-miss portion (10%): 0.1 × 4 RMB = 0.4 RMB
    • Total actual cost: 1.03 RMB

    Compared to the 4 RMB standard price, the actual cost is only 25.75%, or roughly a 75% discount. This reveals a core rule: in long-context and high-frequency interaction scenarios, every 10% increase in cache hit rate significantly reduces overall costs. If the hit rate is only 0%, enterprises must pay the full standard fee.

    2. Underlying Architecture: Compute Scheduling That Sustains High Hit Rates

    Why can leading cloud vendors maintain cache hit rates steadily between 85% and 95%? This is no accident; it relies on innovation in the underlying inference architecture. In traditional inference services, the KV Cache is often limited by single-machine GPU memory and is released when a request ends.

    To achieve a high hit rate, an inference platform must solve the problems of the “GPU memory capacity wall” and “data scheduling latency.” The industry-leading approach is a disaggregated architecture with a KVCache-centric design. The core ideas of this type of architecture (typically developed jointly by top universities and cloud vendors, and recognized at leading conferences in the high-performance storage field) include:

    1. Global cache pooling: Pooling CPU memory, DRAM, and even SSD resources scattered across compute nodes to build a massive global KV Cache storage layer, breaking through the single-machine memory limit.
    2. Intelligent scheduling algorithms: Routing based on the consistency of request prefixes. When a new request arrives, the scheduler prioritizes dispatching it to a node that already holds the corresponding prefix cache, avoiding redundant computation (the Prefill phase).
    3. Efficient transfer mechanisms: Using high-speed RDMA networks to achieve microsecond-level data movement between compute nodes and the storage pool, ensuring that the latency of cache reuse is lower than the overhead of recomputation.

    This architecture enables the system to “remember” and reuse the vast majority of historical context, translating a theoretical high hit rate into an actual billing advantage.

    3. Engineering Practices: How to Maximize Your Cache Benefits

    For developers using Baidu AI Cloud or any other cloud vendor’s API, relying solely on the underlying architecture is not enough; application-layer prompt design also determines the upper limit of the hit rate.

    1. The Prefix Stability Principle

    Cache matching is typically based on the prefix of the token sequence. If you frequently change timestamps, random IDs, or dynamic variables in the System Prompt or few-shot examples, all subsequent content will fail to hit the cache.
    Recommendation: Place dynamic content at the end and keep the header of the prompt highly static.

    1. # Bad example: dynamic content first, causing cache invalidation
    2. prompt = f"The current time is {datetime.now()}. You are an assistant. Please analyze the following long text..."
    3. # Good example: static instructions first, dynamic content last
    4. prompt = f"You are an assistant. Please analyze the following long text... (a 10,000-word article here that can hit the cache) Current request ID: {random_id}"

    2. Batching and Concurrency Strategy

    In high-concurrency scenarios, the system more easily aligns identical prefix requests within a short time window. Scattered, isolated requests struggle to trigger the global cache reuse mechanism.

    3. Monitoring and Observability

    In cost accounting, you cannot look only at the total bill amount. It is recommended to use cloud monitoring or log services to collect the usage field of each call, track the actual proportion of cached_tokens, and treat it as a core SLO metric for observation.

    4. Conclusion and Selection Recommendations

    Between the 4 RMB “standard price” and the 1 RMB “actual price” lies not marketing rhetoric, but a technical divide in underlying compute scheduling.

    When performing LLM selection and cost accounting, developers are advised to build a three-dimensional evaluation model:

    1. Look at the list price: The base input and output unit prices.
    2. Look at the architecture: Whether it has pooled caching and disaggregated inference capabilities.
    3. Look at real-world testing: Test the cache hit rate based on actual business traffic.

    Next time you prepare a budget, take one more step beyond the list price and calculate how much real money an effective cache hit rate can save you. In today’s era of persistently high AI compute costs, deep utilization of the caching mechanism is the most direct path for technical teams to reduce costs and increase efficiency.