Decoder-Only LLM Cache Optimization: Techniques for Lower Inference Costs
This article explains how caching works in decoder-only LLM architectures and why KV cache often becomes the dominant inference cost. It clarifies that an encoding stage does not imply a dual-tower encoder-decoder design, breaks cache overhead into capacity, lifetime, and access costs, and covers practical optimizations such as prefix caching, content-addressed reuse, tiered storage, TTLs, batching, chunked prefill, and cache-aware routing. A deployment checklist and conclusions help readers make better architecture, capacity, and cost decisions.
Cache Optimization in Decoder-Only Architectures
1. Background: Why Caching Becomes the Main Inference Cost
Most mainstream large language model implementations today use a decoder-only autoregressive structure. The defining behavior is simple: given a prompt, the model generates content token by token, and every new token requires the hidden states of all previous tokens to be included in the attention computation again.
In a naive implementation, this repeated computation has two direct consequences:
- Redundant computation: Computing token N largely repeats the projections and intermediate attention results already produced for the previous N-1 tokens.
- Memory and bandwidth pressure: To reuse intermediate results, the system needs a key-value cache (KV Cache). Its size grows linearly with sequence length, batch size, and layer count, and it quickly becomes a memory bottleneck.
This leads to a seemingly contradictory situation in practice: caching starts as a cost-saving technique but ends up as the most expensive overhead. When people say a model’s cache is “cheap,” what they really mean is: how do you reduce the cache’s size, lifetime, and access cost without changing the decoder architecture?
2. A Common Misunderstanding: An Encoding Design Is Not a Dual-Tower Structure
When technical reports mention an “encoder” or an “encoding stage,” it is easy to assume the model has abandoned the mainstream decoder paradigm in favor of a dual-tower, encoder-decoder split. That is usually not the case:
- The report typically describes a single model body, not two separate parameter towers.
- The so-called “encoding” refers to a reusable representation precomputation on the input side, whose output is written to the cache and read repeatedly by later decoding stages.
- Decoding remains autoregressive and produces output token by token.
The following pseudocode shows this “single model, two stages” relationship:
# Stage 1: precompute the long prefix once and store it in the cacheprefix_state = model.encode(prefix_tokens) # computed only oncecache.put(prefix_state, ttl=SESSION_TTL)# Stage 2: autoregressive decoding, reusing the cache at every stepfor step in range(max_new_tokens):logits = model.decode_step(last_token, cache) # each step only reads the cachelast_token = sample(logits)
The key point is that precomputation and decoding share the same parameters, so there is no engineering burden of aligning two models. The cache is simply a materialization of the same model’s internal state.
3. Three Dimensions of Cache Cost
To talk about “cheap,” you first have to break the cost apart. Cache overhead usually consists of three parts:
| Dimension | Meaning | Influencing factors |
|---|---|---|
| Capacity cost | How much space the cache occupies | Layers, heads, sequence length, batch size, precision |
| Lifetime cost | How long the cache stays alive | Session duration, eviction policy, timeout settings |
| Access cost | How expensive a single read or write is | Storage medium, network hops, serialization format |
Many optimizations focus only on the first item, but the second and third often drive the bill: the cache is kept too long and read from too far away.
4. General Optimization (1): Prefix Caching and Representation Reuse
The most basic and effective approach is to precompute and reuse the stable, unchanged prefix separately: system prompts, tool descriptions, long documents, and few-shot examples.
Practical points:
- Prefix normalization: Put fixed content first and variable content last to maximize the reusable boundary.
- Content addressing: Use a content hash rather than a session ID as the key, which naturally supports cross-session sharing.
- Tiered caching: Keep hot data in memory, warm data on fast local storage, and cold data in object storage.
- Explicit expiration: Set a reasonable TTL for the cache to avoid the capacity blowup caused by “permanent caching.”
Illustrative code:
key = hash(system_prompt + tool_schema + doc_chunk)state = cache.get(key)if state is None:state = model.encode(prefix_tokens)cache.set(key, state, ttl=3600)
5. General Optimization (2): Batching and Scheduling
The other half of cheap caching comes from scheduling. With the same cache, different batching strategies produce noticeably different unit costs:
- Continuous batching: New requests can join at any time without waiting for the whole batch to finish, improving throughput.
- Chunked prefill: Split long prompts into chunks to avoid the long-tail latency caused by a single large prefill.
- Priority queues: Serve interactive requests first and let offline jobs yield.
- Cache-aware routing: Route requests with the same prefix to the same instance to improve hit rate.
This kind of scheduling capability is usually provided by general cloud components such as container platforms, message queues, logging services, and monitoring and alerting, rather than depending on a single proprietary interface.
6. Engineering Deployment Checklist
- Measure first: Track cache hit rate, average lifetime, and read amplification separately to locate the real bottleneck.
- Compress precision: Evaluate lower-precision caching within an acceptable quality loss; capacity often drops by a multiple.
- Separate hot and cold: Decide which content deserves to stay resident and which should be evicted immediately.
- Isolation and compliance: In multi-tenant scenarios, isolate caches to avoid data boundary risks from cross-tenant hits.
- Load-test and validate: Replay real traffic to verify hit rate and latency instead of relying only on theoretical values.
7. Conclusion
Cache optimization around the decoder architecture is not about switching model paradigms. It is about turning “precompute once, reuse many times” into a system capability: use content-addressed prefix caching to cut repeated computation, use tiered storage and reasonable TTLs to control capacity, and use cache-aware scheduling to improve hit rate and throughput. Understanding these three cost layers leads to more rational decisions in architecture selection, capacity planning, and cost governance.
