From 300KB to 69KB per Token: How LLM Architectures Solve the KV Cache Problem
TL;DR Highlight
A breakdown of how LLM KV Cache architecture has evolved from GPT-2 to DeepSeek V3, comparing per-token memory costs across architectures as they dropped from 300KB to 69KB.
Who Should Read
ML engineers who serve LLMs directly or need to optimize inference costs, as well as backend/infrastructure developers who want to understand the internal workings of LLM architectures.
Core Mechanics
- KV Cache is not just an abstract concept — it's physical bytes residing in GPU memory. For each token, query, key, and value vectors are computed, and storing the key-value pairs in GPU memory eliminates the need to recompute all previous tokens when generating the next one. This reduces computational complexity from O(n²) to O(n).
- Without KV Cache, processing a 2,000-token conversation requires reprocessing all 2,000 tokens from scratch for every single token generated — meaning the entire history is read 2,000 times over. KV Cache eliminates this redundant computation.
- GPT-2 (2019) used the simplest form of Multi-Head Attention, where every attention head maintained independent key-value pairs. This resulted in a KV Cache cost of 300KiB per token, meaning a single 4,000-token conversation occupied approximately 1.2GB of GPU memory, separate from model weights.
- Llama 3 (2024) introduced GQA (Grouped-Query Attention) across all model sizes. By having multiple query heads share the same key-value pairs, the per-token cost dropped to 128KiB — less than half of GPT-2. Benchmark quality loss was minimal, as many attention heads had already been learning redundant representations.
- DeepSeek V3 (2024) adopted MLA (Multi-Head Latent Attention), which instead of caching key-value tensors directly, compresses them into a low-dimensional latent space for storage and decompresses at inference time. Despite being a 671B parameter model (with only 37B activated per token via MoE routing), the per-token cache cost dropped to 68.6KiB. Notably, despite being lossy compression, it slightly outperformed standard MHA on some benchmarks.
- Gemma 3 (2025) combined GQA with a sliding window approach. Local-to-global attention layer ratio is set at 5:1, with local layers processing only the most recent 1,024 tokens. Older context is only accessible through narrow global attention layers. Despite this aggressive filtering, perplexity loss was negligible.
- There are also approaches that eliminate KV Cache entirely. State Space Models (SSMs) like Mamba (2023) maintain a fixed-size hidden state that is updated token by token. The tradeoff is that memory doesn't grow, but the model must decide in real time what to compress and discard.
Evidence
- "A research project called 'Cartridges' — which optimizes KV Cache directly via gradient descent — was introduced in the comments. Developed by Stanford's Hazy Research team, this approach keeps network weights frozen and instead trains the KV Cache itself to compress large documents or entire codebases into a smaller set of tokens. Commenters found it compelling that this enables more systematic compression than ad-hoc LLM summarization. Beyond architecture-level optimizations, a practical tip was shared about quantizing the KV Cache at inference time. In llama.cpp, quantizing keys to q8 and values to q4 can cut memory nearly in half on top of savings already achieved by GQA or MLA. One user reported running a Qwen 70B 4-bit model on an M2 Max 96GB machine where KV quantization allowed long contexts to fit within unified memory. An asymmetric quantization strategy — applying different precision levels to keys and values — was also discussed. Because keys directly determine attention scores, they require higher precision, while values are far more tolerant of lossy compression. This makes the asymmetric approach of q8 for keys and q4 for values practically effective. A fun fact was noted in the comments: Voyager 1's RAM capacity is 69KB — the same as DeepSeek V3's per-token KV Cache size — humorously highlighting the scale of memory consumption in modern AI models."
How to Apply
- "When serving large models (70B+) locally or on-premises with llama.cpp and long context is required, enable KV Cache quantization options (key: q8, value: q4) to reduce memory by roughly half on top of savings already provided by GQA/MLA. This is especially effective on Apple Silicon with unified memory. When comparing new models by architecture, refer to Sebastian Raschka's LLM Architecture Gallery to find per-token KV Cache costs for each model. Since GQA support and MLA adoption directly impact long-context serving costs, include these figures in your pre-deployment checklist for cost-sensitive services. If you're running a RAG pipeline that repeatedly references large documents or codebases, consider reviewing Stanford's Cartridges approach (https://hazyresearch.stanford.edu/blog/2025-06-08-cartridges). Directly optimizing KV Cache via gradients to cache documents in a more compressed form can improve memory efficiency compared to stuffing entire documents into context every time."
Terminology
Related Papers
Claude Code sends 33k tokens before reading the prompt; OpenCode sends 7k
동일한 모델과 작업 환경에서 Claude Code와 OpenCode의 실제 토큰 사용량을 API 레벨에서 측정한 결과, Claude Code가 시스템 프롬프트 오버헤드만으로 OpenCode 대비 4.7배 더 많은 토큰을 소비한다는 것을 확인했다.
Mesh LLM: distributed AI computing on iroh
사무실, 집, 클라우드에 흩어진 GPU들을 하나의 OpenAI 호환 API로 묶어주는 분산 LLM 실행 시스템으로, 비싼 API 비용 없이 큰 모델을 직접 운영할 수 있다.
Show HN: Frugon – Find which LLM calls a cheaper model could handle (local, MIT)
내 LLM API 비용이 어디서 새는지 로컬에서 분석해주는 오픈소스 CLI 도구로, 비싼 모델 대신 저렴한 모델로 전환 가능한 호출을 골라낸다.
Jamesob's guide to running SOTA LLMs locally
2천 달러짜리 RTX 3090 한 장부터 4만 달러짜리 RTX PRO 6000 4장 셋업까지, 로컬에서 최신 LLM을 직접 돌리는 방법을 하드웨어 선택·구성·실행 설정까지 통째로 정리한 실전 가이드다.
Faster embeddings: how we rebuilt the ONNX path in Manticore
Manticore Search가 기존 SentenceTransformers/Candle 백엔드를 ONNX Runtime으로 교체해 텍스트 임베딩 생성 속도를 평균 14배 향상시켰다. 별도 모델 서비스 없이 DB 내부에서 직접 임베딩을 처리하는 구조에서 INSERT 속도가 곧 임베딩 속도이기 때문에 이 개선은 실질적인 ingest 처리량 향상으로 직결된다.
Asymmetric Quantization: Near-Lossless Retrieval with 97% Storage Reduction
멀티벡터 검색 모델의 문서 벡터를 1비트 이진값으로 압축하고 쿼리 벡터만 int8로 유지하는 비대칭 양자화 기법으로, 스토리지를 97% 줄이면서 검색 품질 손실을 0.61점(NDCG@10 기준)에 그치게 만든 실제 프로덕션 적용 사례다.