← Reference · Nestor G Pestelos Jr

Systems Architecture · Artificial Intelligence

Prompt Caching

Reference entry · last updated August 26, 2026

Prompt caching is an architectural optimization in large language model inference engines that preserves pre-computed Key-Value (KV) cache tensors for repeated prefix tokens across requests.[1] By bypassing the redundant prefill computation of static context (such as system instructions, tool schemas, and base documents), prompt caching substantially reduces Time to First Token (TTFT) and token consumption costs.[2]

System Prompt & Tools (20k tokens) User Query A Cached KV Tensors in GPU Memory Prefix Reused (0ms Prefill / Discounted) User Query B Operational Impact TTFT Latency: -80% Cached Input Token Cost: -90% GPU Compute Reclaimed

Prefix caching mechanics

In standard transformer inference, when a prompt arrives, the model performs matrix multiplications across all input tokens to generate intermediate Key (\(K\)) and Value (\(V\)) activation tensors for every attention head in every layer.[1]

Prompt caching identifies shared token prefixes using hierarchical tree structures (such as Radix Trees in RadixAttention):[3]

  1. Prefix hashing: The engine hashes token ID sequences starting from index 0.
  2. KV cache lookup: If a matching prefix tree node exists in GPU VRAM, the engine loads the pre-computed \(K\) and \(V\) tensors directly into memory without invoking model forward passes.
  3. Suffix prefill: The engine computes attention only for the newly added dynamic tokens at the end of the prompt.[3]

Latency and economic impact

Prompt caching fundamentally shifts the economics of multi-turn conversational agents and large-document RAG:[2]

Structuring prompts for cache stability

Because caching relies on exact sequence prefix matching from token index 0, any mutation at the beginning of a prompt invalidates the cache for all subsequent tokens:[1]

Cache eviction and multi-tenant security

GPU memory is finite. Runtimes manage prompt cache lifecycles using Least Recently Used (LRU) eviction policies with Time-to-Live (TTL) expiration windows.[3] In multi-tenant systems, prefix keys are partitioned by organization and workspace IDs to prevent unauthorized cross-tenant attention leakage.

See also

References

  1. Mitra, Sampriti. System Design for the LLM Era: Patterns and Principles for Production-Grade AI Architecture. Packt Publishing, 2026. Ch. 2: "Core Architectural Patterns for LLM System Design."
  2. Anthropic. "Prompt Caching Documentation and Best Practices," 2024. https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching
  3. Zheng, Lianmin, et al. "SGLang: Efficient Execution of Structured Language Model Programs." arXiv preprint arXiv:2312.07104, 2023.