Context Caching for LLM Inference: How Cached Input Pricing Cuts Costs
Context caching lets large language model APIs reuse computation for shared prompt prefixes. This article explains why providers can charge less for cached input tokens, where the savings come from in multi-turn chat, retrieval-augmented generation, and few-shot prompting, and how developers can improve cache reuse through prompt design, monitoring, and request routing.
A cloud provider recently announced a pricing change for a high-performance model service on its Model as a Service (MaaS) platform in a specific region. The price of input tokens served from cache was reduced to 20% of its previous rate, while all other rates remained unchanged. The change illustrates how LLM API pricing can reflect the different costs of computing a prompt from scratch and reusing work from an earlier request.
1. How Context Caching Works in LLM Inference
Most large language models use a decoder-only Transformer architecture and generate text autoregressively, one token at a time. Each new token attends to earlier tokens. Without a cache, the model would repeatedly compute the key and value representations of those earlier tokens.
A key-value cache, or KV cache, avoids this work. During generation, the inference engine stores previously computed key and value tensors in GPU memory. At each subsequent step, it reuses those tensors rather than recomputing them. The new token still needs to attend to the earlier tokens; what the cache saves is the work of rebuilding their keys and values.
Context caching, also called prefix caching, extends this reuse across requests. API calls often start with the same system instructions, tool descriptions, or conversation history. The platform can retain the KV state for a frequently reused prefix. When a new request starts with a matching prefix, the engine can reuse that state and skip processing the matching input tokens from scratch. This is a cache hit.
2. Why Cached and Uncached Input Have Different Prices
Traditional LLM API pricing typically distinguishes between input and output tokens. That split is easy to understand, but it does not account for differences in how input tokens are processed. Two hardware constraints help explain the difference: GPU compute capacity and memory bandwidth.
- Uncached input: The model processes the input through its attention and feed-forward layers and computes the corresponding keys and values. This initial processing places a substantial demand on GPU compute.
- Cached input: The inference engine reads previously computed KV state from memory rather than running the matching prefix through the model again. The reuse step depends primarily on memory bandwidth. Processing the uncached suffix and generating output still require computation.
Reducing the cached-input rate to 20% of its previous price brings this billing category closer to the underlying resource cost. It does not mean the entire request costs 80% less: the savings depend on how many input tokens hit the cache and how much of the bill comes from uncached input and output.
3. Where Context Caching Can Reduce Costs
Multi-Turn Chat
Customer support bots and AI assistants typically send earlier messages back to the model with each new turn. If the conversation history remains an unchanged prefix, the shared portion can hit the cache while the newly appended content is processed at the uncached rate. As the conversation grows, more of the input may qualify for the cached rate, reducing the cost of resending a long history.
Retrieval-Augmented Generation (RAG)
A RAG request may contain lengthy system instructions and retrieved documents. If a user asks several questions about the same long document, the application can place the document before the question and keep that prefix unchanged. After the initial request, follow-up questions can reuse the document’s cached state when the prefix matches, reducing input costs without omitting the document from the request.
Few-Shot Prompting
Applications often include fixed examples to guide a model toward a particular output format. Putting those examples before the variable question allows them to form part of the cached prefix. When that prefix hits the cache, the examples are billed at the cached rate and the changing portion is billed at the standard input rate.
4. How to Improve Cache Reuse
Put Stable Content First
Place content that stays the same across requests—such as role definitions, instructions, and few-shot examples—at the beginning of the prompt. Put variable content, such as the user’s current query, at the end. A changing value in the middle of an otherwise stable prompt can stop the prefix match at that point.
# Put reusable instructions and examples before the variable query.system_prompt = "You are a technical documentation translator. Translate the English input into accurate Chinese."few_shot_examples = "\n\nExample 1: ..."user_query = "Please translate this sentence."messages = [{"role": "system", "content": system_prompt + few_shot_examples},{"role": "user", "content": user_query}]
Monitor the Share of Input Tokens Served from Cache
If the API reports token usage in its response, track the ratio of cached tokens to total input tokens—for example, cached_tokens / total_input_tokens where those fields are available. This shows how much of each request’s input benefits from cached pricing. If the ratio remains low, check for changing content placed before or within the intended shared prefix, then restructure the prompt as needed.
Route Requests with Shared Prefixes Together
For high-concurrency deployments that control inference routing or cache placement, group requests that share a prefix and send them to the same inference instance or cache node. For example, presales and after-sales support may use different sets of system instructions. Assigning them to separate cache pools can reduce competition for cache space and help frequently reused prefixes stay resident.
5. What Cached Input Pricing Means for Developers
Separate pricing for cached input reflects a meaningful difference in inference work: reading existing KV state costs less than processing the same prefix again. To benefit from that distinction, keep reusable content at the front of requests and measure how many input tokens actually qualify for the cached rate. As inference engines evolve, MaaS providers may introduce more granular pricing that gives developers additional ways to manage inference costs.
