KV Cache Optimization and Efficient LLM Inference: Architecture Trends in Large Language Models

    This article explains why the KV cache has become the main memory bottleneck for long-context LLM inference. It covers how recent open-weight models address this with cross-layer KV sharing, MoE and dense design choices, and small architectures for on-device use. It also includes a memory estimation formula and practical guidance for developers choosing models and inference frameworks.

    1. Introduction: Architecture Priorities Shift in the Inference Era

    For the past few years, competition among large language models has centered on parameter count, training data scale, and benchmark scores. As reasoning models and AI agents spread, the focus is shifting. A model now needs to do more than answer correctly. It also needs to run efficiently, respond quickly, and handle long contexts.

    In reasoning and agent scenarios, models often process very long conversation histories, large volumes of tool call records, and multi-step chains of thought. As a result, memory use during inference has become the main obstacle to real-world deployment. Within that memory, the KV cache (Key-Value cache) grows linearly, or even faster, with context length. It is gradually replacing the model weights as the largest source of memory consumption in long-context settings. For this reason, how to compress the KV cache has become one of the central questions in model architecture design.

    This article follows that question. It reviews several architectural developments in recent open-weight models: cross-layer KV sharing, the divergence between mixture-of-experts (MoE) and dense architectures, and small designs for on-device deployment. It also analyzes the shared logic behind these designs.

    2. Why the KV Cache Matters

    2.1 How the KV Cache Works

    During autoregressive decoding, each time the model generates a new token, the attention mechanism must access the Key and Value vectors of all previous tokens. Without caching, every decoding step would recompute the KV states for the full history, so computation would grow quadratically with sequence length. The KV cache trades memory for computation. It stores historical KV vectors in GPU memory. Each step then only computes the KV vectors for the current token and appends them to the end of the cache.

    2.2 Quantifying Memory Usage

    The KV cache size can be estimated with a simplified formula:

    1. Cache size ≈ 2 × number of layers × sequence length × hidden dimension × bytes per element

    Consider a medium-sized model with dozens of layers and a hidden dimension in the thousands. With batched inference and a context length of tens of thousands of tokens, the KV cache can easily exceed the size of the model weights. When the context grows to hundreds of thousands or millions of tokens, a single GPU cannot hold the cache without optimization.

    2.3 Real Drivers of Longer Context

    Reasoning models need to keep complete chains of thought in context. Agent applications must retain records of multiple rounds of environment interaction. Code analysis tasks may need to load an entire repository at once. Together, these needs push context windows longer. They also turn KV cache optimization from an optional improvement into a requirement for deployment.

    3. Cross-Layer KV Sharing: Letting Later Layers Reuse Earlier States

    3.1 Core Idea

    In a standard Transformer, each layer computes and stores its own KV states. Cross-layer KV sharing starts from the observation that not every layer needs a unique KV representation. Some later layers can directly reuse the Key-Value tensors that earlier layers have already computed. The number of KV tensors to store drops from “number of layers × sequence length” to “number of sharing groups × sequence length.” Memory use can therefore fall by a substantial multiple.

    3.2 Relation to Earlier Approaches

    This idea is not new. Earlier academic work tested cross-layer attention and showed that, with a sensible sharing strategy, the loss in model quality can be kept small. Recent open-weight models are notable because they applied this technique at scale and in a systematic way within mainstream open architectures, for the first time. They also combined it with quantization, grouped-query attention (GQA), and other methods to form a complete long-context inference approach. This shows the technique works in production-grade models.

    3.3 Engineering Benefits

    • Lower memory use: KV cache size is roughly inversely proportional to the sharing ratio. Halving the cache allows nearly twice the context length in the same GPU memory.
    • Less bandwidth pressure: each decoding step reads the full KV cache. A smaller cache directly reduces memory bandwidth demand and raises decoding throughput.
    • Easier deployment: on mobile and edge devices, memory (or unified memory) is often only a few gigabytes. Cross-layer reuse is a key piece that makes long context feasible on-device.

    3.4 Trade-offs

    Cross-layer reuse has costs. The more layers that share a KV state, the less diverse the representations become. This can hurt tasks that need fine-grained position awareness or complex reasoning. In practice, common approaches include grouped sharing across ranges of layers, or keeping independent KV states in shallow layers and sharing only in deeper layers. These approaches balance quality and efficiency.

    4. MoE and Dense: Two Complementary Architectural Paths

    A clear trend in recent open-weight models is that a single model series often comes in both mixture-of-experts (MoE) and dense versions. Each version targets different needs.

    4.1 MoE: Sparse Activation for Inference Efficiency

    An MoE model replaces the feed-forward network with multiple “expert” sub-networks. Each token activates only a small subset of them. This allows a very large total parameter count while keeping the number of activated parameters per step small. The result is a significant reduction in computation per forward pass. For high-throughput, low-latency local inference and service deployments, MoE is an attractive choice.

    4.2 Dense: Easier Post-Training and Engineering

    MoE has clear costs. The routing mechanism adds training instability, and expert load balancing requires careful tuning. In post-training stages such as fine-tuning and preference alignment, MoE models generally converge less easily than dense models and are more prone to expert collapse. Teams that want a higher quality ceiling, or that fine-tune frequently for specific domains, often prefer dense architectures. Their training and post-training pipelines are more mature, and their behavior is more predictable.

    4.3 Selection Guidance

    • For large-scale online serving where inference cost matters and the engineering team is experienced, MoE versions usually offer better cost-effectiveness.
    • For heavy domain fine-tuning, reinforcement learning alignment, or teams that want to reduce tuning complexity, dense versions are a safer starting point.
    • When deploying on cloud platforms, use the elastic scaling and monitoring and alerting features of your container platform. Plan capacity for each architecture separately. For MoE, focus on expert distribution and routing hotspots. For dense models, focus on memory use and batching efficiency.

    5. Small Architectures for On-Device Use

    Beyond server-side models, small models with a few billion parameters, designed for mobile and embedded devices, also include targeted architectural optimizations:

    1. More aggressive KV sharing: on-device memory is very limited. Cross-layer KV reuse combined with low-bit KV quantization can bring peak memory during long-context inference down to an acceptable level.
    2. Combined attention variants: sliding-window attention and global attention are used in alternation.