LLM API Caching: Billing Mechanisms and Cost Optimization in Practice
This article explains how cache-aware billing works for large language model APIs and why input and output tokens are priced differently. It covers how prefix caching reduces input costs, practical ways to raise cache hit rates, methods for limiting output length, and how to track token usage to find where costs come from.
1. Introduction: Token Billing Is Now a Core Cost Variable for AI Applications
As large language models improve quickly, more applications call model inference through APIs and integrate it into their business systems. A primary engineering challenge for developers in this setup is controlling API call costs precisely.
A recent API pricing update from a leading large model provider shows this clearly. Input tokens that hit the cache cost about 1.3 CNY per million tokens. Input tokens that miss the cache cost about 13 CNY per million. Output tokens cost about 68 CNY per million. On the surface, this is just a price table update. However, it reflects engineering logic worth examining. The price of a cache hit is one-tenth the price of a cache miss, and the price of an output token is about five times the price of an input token. Understanding and using these two price gaps is key to building cost-effective AI applications.
2. Pricing Structure: Why There Are Three Price Tiers
To understand this pricing structure, you first need to understand how large model inference works. Inference has two phases: prefill and decode. Prefill processes the input tokens. Decode generates output tokens one at a time.
2.1 The cost difference between input and output
Decoding generates tokens serially. Each step requires a full forward pass through the model, and the work cannot be parallelized the way prefill can. As a result, each output token costs significantly more compute than each input token. The higher price for output reflects this asymmetry in computation.
2.2 The cost difference for cache hits
A “cache hit” usually refers to prefix caching. When multiple requests share the same long prefix, such as a system prompt, a knowledge base document, or the early turns of a multi-turn conversation, the inference engine can reuse the key-value (KV) cache already computed for that prefix. This skips the repeated prefill computation. A cache hit consumes almost no new compute, so its price can drop to one-tenth of the cache-miss price. In effect, the provider passes the compute savings to the caller as a discount, which encourages efficient call patterns.
3. Engineering Practices for Improving Cache Hit Rates
Cache hits can reduce costs by an order of magnitude, so raising the hit rate should be a core goal of application architecture. The following are widely accepted general practices.
3.1 Keep prompt prefixes stable
Prefix caching requires an exact match on the prefix. Even a one-character difference can prevent everything after it from hitting the cache. Place stable content, such as role definitions, business rules, and reference documents, at the start of the prompt. Place variable content, such as user input, timestamps, and dynamic values, at the end. A common anti-pattern is putting a timestamp or random ID at the beginning of the system prompt. This invalidates the cache for the entire prefix.
3.2 Combine and reuse long context
In scenarios such as retrieval-augmented generation (RAG) and knowledge base Q&A, the same document is often referenced by many requests. Concatenate retrieved documents in a fixed order and format, and reuse the same document set whenever possible. This raises the probability of cache hits. In multi-turn conversations, append history messages as they are instead of rewriting them as summaries. This also helps cache reuse.
3.3 Account for cache lifetime and capacity limits
Prefix caches usually have a limited time-to-live and finite capacity. Entries are evicted when these limits are exceeded. Because of this, frequent, concentrated calls are more likely to hit the cache than sparse ones. When designing call orchestration, you can group similar requests closer together in time so that cache entries do not expire from lack of access.
Cache granularity, minimum prefix length, and hit-matching rules can differ between providers. Read the relevant API documentation carefully before integrating. The principles in this article are general industry practices. They do not guarantee any specific product capability.
4. Output Costs Matter Just as Much
Output tokens cost more than 50 times as much as cache-hit input tokens. This means that asking the model to say less often saves more money than asking it to read less. Useful techniques include:
- Specify the output format and length in the prompt, for example, “Return JSON only” or “Use no more than 200 words.”
- Use structured output or function calling to prevent the model from adding unnecessary natural-language explanations.
- For long-form generation, use segmented generation and early stopping to avoid unproductive continuation.
5. Cost Measurement and Continuous Optimization
Optimization depends on measurement. Set up token usage monitoring in your application. For each request, record the input token count, the cache-hit token count, and the output token count. Aggregate these by business scenario. Logging and monitoring services offered by most cloud providers can help you quickly build a cost dashboard and identify high-consumption scenarios.
A practical estimate of the cost per request is:
Cost per request ≈ (cache-hit input tokens × cache-hit price+ uncached input tokens × input price+ output tokens × output price) / 1,000,000
Tracking these three components separately often shows where the costs concentrate. The cause might be a redundant system prompt, output length that has grown out of control, or a low cache hit rate. Each one calls for a different fix.
6. Conclusion
The three-tier pricing of cache-hit input, uncached input, and output is more than a simple pricing strategy. It reflects the real cost structure of inference computation. For developers, this means prompt engineering is not only a question of output quality. It is also a question of cost engineering. Stable prefix design, sensible context organization, and disciplined output constraints, combined with ongoing usage measurement, can reduce call costs by an order of magnitude without sacrificing quality. As AI applications move from pilots to large-scale deployment, mastering these cost optimization methods is essential for developers and architects.
