Notes
The problem
Start with the problem. A model is stateless. It remembers nothing between calls, so every request carries the whole prompt: a ten thousand token system prompt plus a five hundred token question. Before writing a word, the model runs all ten and a half thousand tokens through its layers. That first pass is called prefill. On the next call it does the exact same work for the same ten thousand tokens. You pay for it, and the user waits for it.
- call 1: read 10,500 tokens
- call 2: read 10,500 tokens
- call 3: read 10,500 tokens
- 10,000 of them are identical
- every single time
- the model is stateless: it keeps nothing between calls unless you resend it
The idea
Prompt caching is the fix. On the first call, the provider processes your prompt and saves the intermediate result for the beginning of it, the prefix. On the next call, if the prompt starts with exactly the same tokens, it loads that saved work and only processes the new tokens at the end. Cached tokens are billed at a fraction of the normal input price, often around a tenth, and the reply starts noticeably sooner.
- first call: process the prefix and SAVE the result
- next call with the same
- prefix: LOAD it, process
- only the new tokens
- cached tokens are billed at a fraction of the normal
- price, and arrive faster
Under the hood
What exactly is saved? During prefill, every token gets a key and a value vector in each attention layer. That is the KV cache you met in the attention episode. Each token's vectors depend only on the tokens before it. So if two prompts share the same first ten thousand tokens, those vectors are identical, and the provider can reuse them. So caching does not change the answer. The model still generates fresh output every time.
- prefill: each token gets a key
- and a value vector per layer
- (the KV cache)
- these depend only on the tokens BEFORE them
- so an identical prefix gives
- identical keys and values: safe to reuse, same answer
Order matters
Because only an exact prefix can be reused, order is everything. Put the parts that never change first: tool definitions, then the system prompt, then examples and reference documents. Put the conversation history after that, and the new user message last. Anything that changes per request, a timestamp, a request id, the user's name, belongs near the end. One changing detail at the top makes everything after it uncacheable.
1 tool definitions stable 2 system prompt stable 3 examples, docs stable 4 conversation grows 5 new user message changes never at the top: timestamps, request ids, user names, random order
Hit or miss
Walk through three calls. Call A has tools, system prompt, documents and question one. Call B has the same first three parts and a new question, so all ten thousand prefix tokens are reused. Call C fixes a typo inside the system prompt. Now the match ends at that typo. Only the tools before it are reused. The rest of the system prompt and every document after it are computed again, even though the documents did not change.
A: [tools][system][docs][q1] B: [tools][system][docs][q2] same up to docs: 10,000 reused C: [tools][system v2][docs][q3] differs inside system: only tools reused, system, docs and q3 recomputed rule: reuse ends at the first token that differs, forever after
Worked cost: setup
Let us put numbers on it. A support chatbot has a ten thousand token system prompt, each question adds five hundred tokens, and each answer is three hundred tokens. One thousand calls arrive close together. Assume a cache read costs a tenth of the input price and a cache write costs a quarter extra. Without caching you pay for ten and a half million input tokens. With caching, one write, nine hundred ninety nine cheap reads, and the new tokens at full price.
1,000 calls, close together stable prefix 10,000 tokens new part 500 tokens output 300 tokens cache read ~0.1x input price cache write ~1.25x input price no cache: 1,000 x 10,500 = 10.5M cache: 1 write = 12.5k 999 reads x 10k x 0.1 = ~1.0M + 1,000 x 500 new = 0.5M
Worked cost: result
The result. Input drops from ten and a half million token equivalents to about one and a half million, roughly eighty six percent less. Output is not cached, so with output priced at four times input, the total falls from eleven point seven to about two point seven million. That is about seventy seven percent cheaper. But if calls come every ten minutes and the cache lives five, every call pays the write premium, about a quarter more than no cache.
no cache cache input 10.5M ~1.5M output (~4x) 1.2M 1.2M total 11.7M ~2.7M saving ~77% sparse: 1 call per 10 min with a 5 min lifetime, so every call is a write: 13k vs 10.5k per call, about 24% MORE than no cache
Latency
Caching also saves time. Latency has two parts. Prefill, which grows with the prompt length, and decode, which grows with the number of tokens you generate. For a long prompt, prefill is most of the wait before the first word appears. A cache hit skips most of it, so time to first token often drops by half or more. Decode speed stays the same, so a long answer still takes as long to stream out.
- latency = prefill + decode
- prefill grows with prompt
- length; decode grows with output length
- caching removes most of the prefill for long prompts: often ~50-80% faster start
- decode speed is unchanged
Lifetime
A cache does not live forever. It sits in fast memory on the provider's servers, which is expensive. An entry typically lives five to ten minutes after its last use, and every hit resets the timer, so a busy app keeps it warm. Some offer an hour or longer for a higher write price. Entries are kept per organisation, not shared with other customers. And a cache can be evicted early, so treat a hit as likely, never guaranteed.
- typical: ~5-10 minutes
- after the last use
- each hit refreshes the timer
- some offer ~1 hour or longer for a higher write price
- evicted early under load
- cached per organisation, never shared across customers
- treat a hit as likely, not guaranteed
What breaks it
The classic bug is a timestamp at the top of the system prompt. The time changes every second, so the very first line differs on every call, and nothing is ever reused. Others are tool definitions serialised in a random key order, the user's name inside the system prompt, A B test variants near the top, and switching the model or the tool list mid conversation. Put the date at the end, rounded to the day, and sort your JSON keys.
- "Now: 10:42:07" in line 1
- tools serialised in random key order
- user name or id in the system prompt
- A/B variants at the top
- editing the system prompt
- switching model or tools mid-conversation
Multi-turn chats
Chats benefit the most. Every turn resends the whole history, so the prompt from the last turn is the prefix of this one. Turn three reuses everything up to turn two and only processes the new messages. A long conversation stays cheap even as it grows. One trap: if you drop the oldest messages every turn to save space, the start of the prompt changes and nothing matches. Trim rarely, in big chunks, and accept one miss each time.
turn 1: [sys][u1] write
turn 2: [sys][u1 a1][u2] read
[sys][u1] reused, rest new
turn 3: [sys][u1 a1 u2 a2][u3]
reuses everything up to u2
trimming old turns at the front
breaks the match: trim rarely,
in big chunks, then re-warm Using it in practice
How you turn it on depends on the provider. Some cache long prefixes automatically. Others ask you to mark where the cached part ends, which gives you more control. Most need a minimum prefix, often one to a few thousand tokens. Every response reports how many input tokens were read from cache, so log that number and track the hit rate per endpoint. A sudden drop usually means someone put something dynamic near the top of the prompt.
- automatic: provider caches long prefixes for you
- explicit: you mark where the cached prefix ends
- minimum prefix: ~1-4k tokens
- check usage: cached vs uncached input tokens
- track hit rate per endpoint
- alert when it suddenly drops
Response caching
Do not confuse prompt caching with response caching. A response cache, which you build yourself, stores the whole answer. The key is a hash of the model version, the settings and the full prompt. On an exact match you return the stored answer in milliseconds and never call the model. That is safe when the same input should always give the same output, like classifying a fixed document, and when the prompt holds no other user's data.
- key: hash of model version, settings, the full prompt
- hit: return stored answer, zero model cost, ~ms
- safe when: same input should
- give the same output, e.g.
- classify or extract a fixed
- document; no per-user data;
- version the key; set a TTL
Semantic caching
A semantic cache goes further. It embeds the question and returns a stored answer if a past question is similar enough. That catches rephrasings, but similar is not the same. Cancel my order and cancel my subscription sit close together, yet need different answers. Use it only for public, FAQ style answers, with a high threshold, a short lifetime and no personal or live data. Prompt caching has none of these risks, because the model still writes a fresh answer.
- embed the question, find a stored one above a similarity
- threshold, return its answer
- risk: "cancel my order" and "cancel my subscription"
- look alike, answers do not
- safe for public FAQs, a high
- threshold, short TTL, no personal or live data
Numbers worth knowing
Some ballparks. Cached input usually costs ten to fifty percent of the normal input price, and writing to the cache costs about one to two times the normal price, depending on the provider. The prefix usually needs at least one to four thousand tokens. Entries live five to ten idle minutes, with longer options available. Time to first token often drops by half or more. Output is never cached, and costs three to five times as much as input.
cached input ~10-50% of input price
cache write ~1-2x input price
minimum prefix ~1,000-4,000 tokens
lifetime ~5-10 min idle, refreshed
on hit; ~1 h+ options
time to 1st tok often ~50-80% lower
output tokens never cached
output price ~3-5x input price
busy chat app: most input tokens
should come from cache Say it like this
How to say it. Our ten thousand token system prompt and tool list come first and never change, and the date goes at the end. Each chat turn reuses the history, so around ninety percent of input tokens are cached and input cost falls by about eighty five percent. We log cached tokens and alert on hit rate. Whole response caching is used only for public FAQ answers, never for personal data. That shows cost and safety awareness.
- "Our 10k-token system prompt and tools come first and never change; the date goes at the end. Chats reuse history, so ~90% of input is cached and input cost drops ~85%. We alert on hit rate. Response caching is only for public FAQs, never personal data."