GO TO A SECTION

↑ ↓ to navigate · Enter to openExplore all sections →
FDEInterviews logo
MLSys 2024

Prompt Cache: Modular Attention Reuse for Low-Latency Inference

Precompute the attention states of reusable prompt pieces once and splice them into later prompts: 5 to 10 times faster first tokens, with an approximation that can cost accuracy.

Breakdown by FDE Interviews · 12 min read · Updated

About the original · 2023
AUTHORS
Gim, Chen, Lee, Sarda, Khandelwal, Zhong
FROM
Yale University, Google
PUBLISHED
Nov 2023 · MLSys 2024
CITED BY
391Semantic Scholar · Oct 2026
IN THIS BREAKDOWN · 10 SECTIONS

You can read 22% of this breakdown.

01

The paper in one minute

Before a language model writes its first token, it computes attention keys and values for every token in the prompt. The standard KV cache saves those states within one request so they are not recomputed for each generated token. But across requests, the same system message, document or template is recomputed every time. In Gim, Lin Zhong and colleagues at Yale and Google asked how to reuse those states across prompts. Their Prompt Cache appeared at MLSys 2024.

Two problems stand in the way. Attention states depend on position, because positional encodings are folded into keys and values, so a document's states computed at one position cannot be dropped in at another. And the system has to recognize which parts of a new prompt it has already seen.

The answer is a Prompt Markup Language (PML). A schema declares prompt modules, reusable text segments such as a city guide, a document or a template, and fixes each module's range of position IDs. Modules can have parameters, placeholders of declared length filled at request time, and can be grouped into unions of alternatives that share a starting position. A prompt names its schema, imports modules, fills parameters and adds new text. Prompt Cache precomputes and stores the attention states of every module when the schema is loaded. Serving a prompt concatenates the cached states of imported modules and computes only the parameters and new text.

The method rests on an empirical finding: models still work when position IDs have gaps between modules, as long as relative positions within each module are preserved. The price is an approximation: each module was encoded alone, so its tokens never attended to other modules, much like a local attention mask. On LongBench with Llama 2, MPT and Falcon, accuracy stayed close to full prefill on average, while time to first token dropped sharply, especially for CPU inference.

THE IDEA, VISUALLY
SCHEMA → CACHED MODULES → PROMPT <schema name="travel"> <module name="miami"> … city guide … </module> <module name="trip-plan"> plan a trip of <param name="duration" len="2"/> </module> </schema> <prompt schema="travel"> <miami/> <trip-plan duration="3 days"/> Highlight the surf spots. </prompt> SERVING THE PROMPT miami cached KV trip-plan cached KV 3 days compute new text compute Modules keep their own position IDs; the authors found models tolerate the gaps between modules. Unions of alternatives share a starting position. Parameters get reserved slots. The catch: each module was encoded alone, so its tokens never attended to the other modules, as if a local attention mask were applied. Cross-document tasks pay for it. TIME TO FIRST TOKEN, LONGBENCH (FIG. 3, 4) GPUs, modules in GPU memory 5-10x faster GPUs, modules in CPU memory 1.5-3x faster Intel CPU inference up to 70x AMD CPU inference up to 20x Llama 2 7B, RTX 4090, 3K context: 900 ms → 90 ms LONGBENCH, LLAMA 2 7B (TABLE 1) NarrativeQA F1 19.93 19.38 2WikiMultihopQA F1 16.63 13.95 GovReport ROUGE-L 24.67 25.37 Passage retrieval acc. 7.5 4.25 full prefill Prompt Cache KV MEMORY PER TOKEN, FP16 (TABLE 2) Llama 7B 0.50 MB · 13B 0.78 MB: match the configs. Llama 70B listed at 2.5 MB; Llama 2 70B shares 8 KV heads across 64 query heads, so about 0.31 MiB per token.
Prompt Cache declares reusable prompt modules in a schema, precomputes each module's attention states once, and assembles prompts from cached modules plus new text. Time to first token fell 5 to 10 times with modules in GPU memory, but because modules are encoded separately, accuracy moved on tasks that need text from several documents.
Research notes: the problem, approach and results
900 → 90 ms

time to first token for a 3K-token context with Llama 2 7B on an RTX 4090, before and after caching

5-10x

time-to-first-token reduction with prompt modules held in GPU memory, across three GPUs

0.5 MB

KV state per cached token for Llama 2 7B at FP16, so a 1,000-token document needs about 0.5 GB

THE PROBLEM

Many prompts share large pieces: system instructions, the same documents, templates. Each request recomputes their attention states from scratch, and the standard KV cache only helps within one request or for an identical prefix.

THE IDEA

Make reusable segments explicit as prompt modules in a markup language, give each a fixed range of position IDs, precompute and store each module's attention states independently, and serve a prompt by concatenating the cached states of the modules it imports with freshly computed states for new text.

THE RESULT

On LongBench-style prompts of about 5K tokens, time to first token fell 5 to 10 times with cached modules in GPU memory, 1.5 to 3 times from CPU memory, and up to 70 times for CPU inference. Accuracy was close to full prefill on average, with drops on multi-document tasks.

02

Why it matters to an FDE

Customer prompts are repetitive: the same long system prompt, the same policy manual, the same product catalog section, the same few-shot examples. Reusing their computation is one of the cheapest wins in serving.

Prefix caching is the safe default. Serving engines and API providers commonly reuse states for an identical prompt prefix, which is exact. Put the stable, shared parts at the start of the prompt and the variable parts at the end to benefit.

Modular reuse goes further and is approximate. Prompt Cache reuses modules anywhere in the prompt and in any combination, at the cost of modules not attending to each other. Fine for independent context such as a style guide plus a document; risky when the answer needs to connect facts across documents.

Memory is the binding constraint. Cached states take 0.5 MB per token for a 7B model at FP16. A few long documents fit in GPU memory; hundreds need CPU memory, where the speedup shrinks to 1.5 to 3 times because of copy time.

Time to first token is not total latency. Generation speed per token is unchanged. For short answers to long prompts, the gain is large; for long answers, it is a smaller share of the total.

THE FULL BREAKDOWN

Understand the mechanism. Try it on a real problem.

Unlock this breakdown and 99 more papers.

Premium opens every paper in full, plus every answer, concept, course and guide on the site. One payment for six months of access.

6 months · One payment · No auto-renewal

Get Premium · ₹2,000 / $25
  • ◇1 architecture diagram
  • </>1 runnable code experiment
  • ▦2 results tables
  • ?5 interview questions with answers
  1. 03Key takeaways

    PML, position handling, the approximation, results, memory and misreadings to avoid

    Free account
  2. 04How it works

    The schema and prompt syntax, position assignment, cached inference, and the evaluation setup

    Premium
  3. 05The numbers that matter

    Latency by memory placement, accuracy by task type, memory per token, and what was measured

    Premium
  4. 06Try it yourself

    Recompute KV size per token from model configs and estimate TTFT from GPU and CPU memory

    Premium
  5. 07Where it breaks

    Where modular reuse is wrong, slow or expensive

    Premium
  6. 08Using it on a deployment

    Getting reuse benefits for a customer's prompts safely

    Premium
  7. 09Interview angles

    Five questions this paper prepares you for, with answer hints

    Premium
Back to the paper library →