Sub topic 2
What the provider actually stores: reusable attention state
This chapter assumes you can call an LLM API, know what tokens and context windows are, and can read short Python snippets. It builds on the previous chapter's point that prefill (processing the prompt before generating) is the costly step. Here you will see what a provider keeps so it can skip that work.
What is computed during prefill
A transformer model processes a prompt one layer at a time. In each attention layer, every token produces three vectors: a query, a key, and a value. The query asks "what should I look at?" The keys and values are what later tokens look at.
The model must compute keys and values for every prompt token before it can produce the first output token. During generation, each new token attends to the keys and values of all earlier tokens. Implementations therefore keep those keys and values in memory instead of recomputing them. This stored collection is the KV cache (key/value cache).
What gets reused across requests
Prompt caching applies the same idea across separate API calls. The provider saves the KV state it computed for a prompt prefix. If a later request starts with the same tokens, the provider loads the saved state and runs prefill only on the new tokens.
Reuse is safe because of causal attention. A token can only attend to tokens at or before its own position. The keys and values for token 50 therefore depend only on tokens 1 to 50. Whatever follows token 50 cannot change them.
What is not stored
The cache holds attention state, not your content in a reusable form and not model answers.
- It does not store responses. Each request still generates its output from scratch, and sampling settings such as temperature work as usual.
- It does not match on meaning. Two prompts that say the same thing in different words produce different keys and values.
- It does not change what the model sees. The loaded state is what the model would have computed for those tokens anyway.
Why the state has a size and a lifetime
The saved state is large. It grows with the number of layers, the number of attention heads, the vector width, and the number of cached tokens. A long prompt can occupy a substantial amount of fast accelerator memory.
That memory cost explains several rules covered later in this tutorial. Providers expire entries after a short time. They may only cache prompts above a minimum length. Some charge extra to write an entry. This chapter only needs you to see the cause: the stored thing is bulky and kept in scarce memory.
A toy model of the saving
This snippet counts how many tokens need prefill with and without a cached prefix. Real providers do this on token IDs, not words.
def tokens_to_prefill(prompt_tokens, cached_prefix_len):
return len(prompt_tokens) - cached_prefix_len
prompt = ["tok"] * 5000 # a 5,000-token prompt
print(tokens_to_prefill(prompt, 0)) # 5000 -> cold call, nothing reused
print(tokens_to_prefill(prompt, 4800)) # 200 -> only the new tail is processedCommon misunderstandings
- "The provider remembers my conversation." It stores computed state for token sequences, and the entry expires. Your application still sends the full prompt every time.
- "A hit changes the answer." A hit changes cost and latency. The model receives the same input either way, apart from tiny numerical differences that GPU computation can introduce.
- "Similar prompts share cache." Only an identical token sequence from the start of the prompt can reuse stored state. The next chapter explains why.
Exercise
A cached prefix contains 4,000 tokens. A new request has those same 4,000 tokens followed by 300 new tokens. Why can the provider reuse the state for the first 4,000 tokens without recomputing it, and how many tokens still need prefill?
Show answer
With causal attention, each token's keys and values depend only on the tokens before it and itself. The first 4,000 tokens are identical, so their stored keys and values are exactly what the model would recompute. Only the 300 new tokens need prefill.