Most of a long prompt is the same every time: the instructions, the tools, the document. A provider can save its work on that part and reuse it on the next request, at a fraction of the price. The catch is that the match runs from the first token and stops at the first difference. Put what changes at the end. Put one changing word at the top and you pay full price for everything under it.
Why only a prefix. With causal attention, the saved work at each position depends on every token before that position. Change token 10 and the saved work for every later position is wrong. The cache can keep everything before the change and nothing after it.
The model. Every block is an array of seeded token IDs. On each request the sim compares the new token array with the cached one and takes the longest common prefix. Those tokens are cache reads. Everything after the first difference is computed fresh and written to the cache. The reply (150 tokens) and the next question (40 tokens) join HISTORY, so the prefix grows by 190 tokens per turn.
Example pricing in relative units: fresh 1.0, cache read 0.1, cache write 1.25. Every fresh token on a cached request is also a write. Providers differ on the discount, the write cost, the minimum size, and how long an entry lives.
Simplified: one cache entry, one model, and a match at any token. Real providers add a minimum cacheable length and may match only at set breakpoints. The entry here refreshes on every use and expires once it sits idle past the TTL. Each turn adds 90 seconds of idle time; Wait adds five minutes.