All blogs

AI

How Modern LLMs Scale: KV Caching, Multi-Server Routing, and DeepSeek’s 10x Cost Advantage

A deep dive into how KV caching, prefix-aware routing, and distributed disk offloading eliminate redundant LLM compute plus the five architectural pillars (MoE, FP8/FP4, MLA, MTP, and multi-tier caching) behind DeepSeek’s low inference costs.

AxStudios

Diagram illustrating LLM KV caching across GPU VRAM, host RAM, and distributed NVMe storage alongside cache-aware multi-server routing.

If you have ever wondered how large language models generate thousands of words without recalculating an entire conversation from scratch—or why APIs like DeepSeek cost a fraction of traditional frontier models the answer lies beneath the surface of transformer inference.

Everything comes down to two core engineering challenges:

  1. Avoiding repeated math during and across requests using KV caching and cache-aware routing.
  2. Compressing memory and compute so massive models run cheaply on real hardware.

Here is a practical breakdown of how it all works under the hood.

Part 1: What a KV Cache Actually Is (And Isn’t)

A KV cache (Key-Value cache) is not a traditional web cache that stores raw text responses. Instead, it is temporary working memory in GPU VRAM that stores pre-computed mathematical matrices (Key and Value vectors) for every token the model has processed so far.

How a Single Request Uses the KV Cache

  1. Prefill Phase: When you send a prompt, the model processes all input tokens, calculates their Key and Value tensors across every neural network layer, and saves those numbers in GPU VRAM. 2.Decode Phase: When generating word #1, the model calculates Query, Key, and Value vectors only for that new token and looks up all previous Key and Value vectors from VRAM. When generating word #2, it appends word #1 to the cache and repeats.

Without a KV cache, generating a 1,000-word reply would force the GPU to re-read and recalculate the entire prompt 1,000 separate times. With a KV cache, per-token attention work drops from quadratic to linear.

Part 2: Cross-Request Caching, Security, and Multi-Server Routing

In basic inference, the KV cache is wiped the instant your request finishes. Prefix Caching (or Prompt Caching) changes that by leaving computed KV tensors in memory so future requests can reuse them.

Why Only the Exact Prefix Matches

  • Reuse works strictly byte-for-byte from Token #1 onward. The instant a single character, space, or timestamp changes, the cache stops matching and the
  • GPU computes everything from that point forward fresh. Best Practice: Always structure prompts from most static to most dynamic

[1. Global System Instructions]      <-- IDENTICAL for all users (Cache Hit)
[2. Tool / Function Definitions]     <-- IDENTICAL for all users (Cache Hit)
[3. Shared Reference Documents]      <-- IDENTICAL for all users (Cache Hit)

[4. User-Specific Profile / RAG]     <-- UNIQUE to User A (Computed Fresh)
[5. User A's Chat History]           <-- UNIQUE to User A (Computed Fresh)
[6. User A's Current Message]        <-- UNIQUE to User A (Computed Fresh)

Why User A Can Never Leak Into User B

  • The API is stateless: Nothing is injected into your prompt from the cache

  • Self-contained matching: A cache hit only skips math for tokens that the current request itself already sent in its payload. Because User B never sends User A’s messages, User B can never touch User A’s conversation cache.

-Tenant isolation: Providers isolate caches at the account or organization level—meaning multiple backend servers or multiple API keys under the same account safely share the same static prefix cache, while separate customer accounts remain completely isolated

How Requests Hit the Cache Across Different Servers

KV tensors live inside a specific GPU’s VRAM, and shipping gigabytes of tensors over the network on every request is slow. Modern inference clusters solve multi-server traffic in two ways:

  • Cache-Aware (Prefix-Aware) Routing: A smart router hashes the first chunk of your prompt and routes your request to the exact GPU worker already holding that prefix in VRAM, unless that server's queue is too backed up.
  • Multi-Tier Offloading and Shared Storage: When GPU VRAM fills up or traffic lands on a different server, advanced architectures save those computed KV tensors to secondary memory tiers:

1.Tier 1 — Local GPU VRAM: Instant access (~microseconds). Primary fast path when the router lands on the warm GPU. 2.Tier 2 — Host CPU RAM: Fast access (~milliseconds). Short-term spillover when local VRAM fills up. 3.Tier 3 — Distributed NVMe Disk: Tens to hundreds of milliseconds. Cross-server persistence lasting hours or days over high-speed datacenter networking (used heavily by DeepSeek).

Fallback — Full Recomputation: Seconds. Triggered only on a cache miss when the prefix changes or expires.

Even when pulling from Tier 3 shared disk, loading pre-computed KV tensors over high-speed datacenter networking is still 15x to 40x faster—and up to 90% cheaper—than pushing a massive prompt through the LLM layers again.

Part 3: The 5 Architectural Pillars Behind DeepSeek’s Low Cost

Compressing and caching prompts is only half the story. DeepSeek’s API is dramatically cheaper than traditional frontier models because five compounding optimizations attack every bottleneck in AI serving simultaneously.

Mixture-of-Experts (MoE) — "Only Wake Up the Specialists"

Instead of forcing every parameter in a massive neural network to calculate every single word, an MoE model divides its layers into hundreds of specialized sub-networks (experts). A lightweight router selects only the top few experts (such as 8 out of 256) per token.

Impact: A 671B-parameter model stores massive knowledge in memory while burning the GPU compute of only a ~37B-parameter model per word.

2.FP8 and FP4 Precision — "Shrink Every Number"

Traditional models store weights and KV tensors as 16-bit floating-point numbers (BF16). FP8 (8-bit) and FP4 (4-bit) represent numbers with fewer decimal bits.

Impact: Cuts VRAM footprint by 50% to 75%, while modern GPUs (such as NVIDIA Hopper and Blackwell) multiply FP8 and FP4 numbers 2x to 4x faster. Block-wise scaling and Quantization-Aware Training preserve roughly 98% to 99.5%+ of full-precision accuracy on large MoE models.

3.Multi-Head Latent Attention (MLA) — "Zip-Compress the KV Cache"

Standard attention stores bulky Key and Value matrices for every attention head, making KV caches too large to store on disk. MLA mathematically compresses Key and Value tensors down into a tiny latent vector before saving them, decompressing them on the fly during attention.

Impact: Shrinks KV cache memory by 90%+, making multi-tier disk offloading fast and affordable.

4.Multi-Token Prediction (MTP) — "Draft Multiple Words at Once"

Standard LLMs generate strictly one token at a time, reloading model weights from VRAM for every single word. MTP trains lightweight prediction heads into the model to draft 2 to 4 future tokens simultaneously and verify them in one forward pass.

Impact: Boosts output generation speed by 1.5x to 2x without sacrificing output quality.

Multi-Tier Disk Offloading — "Never Recompute Repeated Prefixes"

Because MLA and FP8 make the KV cache tiny, DeepSeek persists computed KV tensors onto shared distributed NVMe storage for hours or days.

Key Takeaways for Developers

  1. Put static context at the top: Keep system prompts, tool schemas, and shared knowledge identical down to the last character before inserting any user-specific data. 2.Don't worry about cross-user leaks: Prefix caching is a stateless mathematical shortcut on the tokens your request already contains—never a shared database. 3.Compounding efficiency wins: Cheap, fast LLM inference comes from pairing MoE (less compute), FP8/FP4 (smaller numbers), MLA (compressed KV memory), MTP (parallel drafting), and Multi-Tier Caching (zero wasted prefill work).