Home
ArenaGraphSignalTopics
/Large Language Model Infrastructure: Building and Deploying Production AI Systems
Chapter 3 • Module 2 9 min breakdown +15 XP Module

Calculating KV Cache Memory Growth: Capacity Planning for Concurrency

While model weights occupy a fixed amount of VRAM regardless of traffic, the Key-Value (KV) Cache is dynamic: it expands with every newly generated token and scales linearly with the number of concurrent user sessions.

For high-throughput inference clusters serving long-context workloads (such as 32k or 128k token document summarization, code generation, and agentic workflows), KV Cache memory rapidly becomes the limiting factor that caps total system concurrency.

Interactive Blueprint
Rendering diagram...

1. The Fundamental KV Cache Formula

To compute the exact bytes consumed by the KV cache for a given batch of requests:

Where:

  • : Multiplier accounting for two distinct tensors—one Key tensor () and one Value tensor ().
  • : The number of Transformer layers in the model (e.g. in Llama 3 8B, in Llama 3 70B).
  • : The number of Key-Value attention heads per layer.
  • : The hidden dimension per attention head, defined as (typically ).
  • : The total token sequence length (Prompt Tokens + Generated Tokens).
  • : The number of active concurrent request streams (Batch Size).
  • : The storage bytes per element:
    • FP16 / BF16:
    • FP8 (vLLM FP8 KV Cache):
    • INT4 (Experimental KV Cache):
Interactive Blueprint
Rendering diagram...

2. Attention Head Architectures: MHA vs MQA vs GQA

The evolution of attention architectures over recent years was driven primarily by the urgent need to compress the KV cache footprint.

Interactive Blueprint
Rendering diagram...

Quantitative Comparison: KV Cache Footprint per 1,000 Tokens

Let us calculate the KV cache memory required to store 1,000 tokens for 1 user () on an 80-layer, 8192-hidden-dimension model ():

Attention TypeKV Heads ()KV Cache Formula ()VRAM per 1k TokensMax Concurrency on 40GB KV Pool (4k Context)
Multi-Head (MHA)64 heads2.62 GB / 1k tokens3.8 concurrent users
Grouped-Query (GQA)8 heads0.328 GB / 1k tokens30.5 concurrent users
Multi-Query (MQA)1 head0.041 GB / 1k tokens244.1 concurrent users

GQA provides an concurrency increase over MHA without sacrificing model reasoning capabilities.


3. The Long Context Trap: How One Request Can Evict Fifty Users

In production serving systems with continuous batching (e.g. vLLM), all active requests share a common pre-allocated physical VRAM block pool.

Consider a cluster with an available KV Cache pool of 32 GB:

  • Scenario A (Standard Chat Queries): 50 concurrent users each maintaining a 2,000-token context window ().
  • Scenario B (Long Context Ingestion): A single enterprise user submits a 64,000-token document for analysis ().
  • Because one request consumes 65% of the entire GPU's KV cache pool, the serving engine is forced to preempt and evict 32 active chat sessions, pausing their token streaming to disk or host RAM.
Interactive Blueprint
Rendering diagram...

4. FP8 KV Cache Compression: Doubling Concurrency for Free

Modern serving engines (such as vLLM and TensorRT-LLM on NVIDIA Ada Lovelace, Hopper, and Blackwell) support FP8 KV Caching.

Instead of storing past Key and Value tensors in 16-bit BF16/FP16, each activation is dynamically quantized to an 8-bit floating-point representation (E4M3 or E5M2) as it is written to the cache:

  • Memory reduction: Exactly 50%.
  • Concurrency capacity: Exactly .
  • Accuracy impact: Extensive empirical benchmarks show perplexity degradation across reasoning, coding, and math benchmarks.

5. Capacity Planning Script (TypeScript)

The following TypeScript utility calculates KV cache consumption, simulates memory pool exhaustion, and verifies safe concurrency limits for production deployments.

typescript
Loading code editor...

6. Production Failure Modes and Engineering Runbook

Failure Mode: Sudden TTFT Latency Spikes Due to KV Cache Thrashing

  • Symptom: Under steady request volume, average Time to First Token (TTFT) suddenly increases from 120ms to 4,800ms, and GPU memory utilization oscillates wildly.
  • Root Cause: The serving engine reached 100% KV cache allocation. When new requests arrived, the engine had to preempt active decoding requests by swapping their KV blocks from GPU HBM to CPU Host RAM (or recomputing their tokens from scratch).
  • Resolution: Set an aggressive request queue limit (max_num_seqs) in the inference server and enforce a strict KV cache watermark (gpu_memory_utilization = 0.90). When the queue fills, reject incoming requests at the API Gateway with HTTP 429 rather than thrashing active VRAM.

7. Summary & Key Takeaways

  1. KV Cache Scales Linearly with Sequence Length and Concurrency: .
  2. GQA is Essential for Modern LLMs: Grouped-Query Attention slashes KV cache memory by compared to legacy Multi-Head Attention.
  3. Long Context Monopolizes VRAM: A single 64k or 128k prompt can consume as much KV cache as 50 standard chat requests combined.
  4. FP8 KV Caching Doubles Serving Capacity: Switching from FP16 to FP8 halves KV cache footprint with negligible degradation in benchmark accuracy.
Milestone Verification

Ready for the next lesson?

Mark this module complete to record verified progress and earn +15 XP toward your architect profile.