Prompt Cache: 2,688 of 2,896 Tokens Hit, One Line Kills It
A prompt cache is supposed to cut the cost of a repeated system prompt by an order of magnitude, and it does, until a logging layer puts a request id or a timestamp on the first line.
📜 Table of Contents
📖 Keep reading
A repeated prompt is billed twice over unless the front of it is byte-identical to the last request. I put a 2,201-word house handbook in the system message and asked eight questions against it. Seven of those calls billed 207 input tokens instead of 2,896. Then I put a twelve-token request-id line above the handbook, and seven calls out of seven paid the full 2,912.
Both outcomes come from the same mechanism, and the rules around it are stricter in one place and looser in another than the documentation suggests. Stricter: anything new at the top of the prompt wipes the whole prefix, even a date. Looser: a sentence appended at the end costs only its own twenty tokens, and editing a clause seven-tenths of the way in costs only what follows it.
How the run was set up
- deepseek-flash, temperature 0, max_tokens 50, six arms, forty-two calls, all on 2026-11-28. The question text changes between calls and that variation is inside every figure below.
- The prefix is a synthetic order-desk handbook: twelve clauses repeated to 2,201 words, which the API counts as 2,896 input tokens.
- Each arm warms the cache with one call on the unmodified prefix, then repeats the same eight questions. One extra arm used a 15,014-word prefix, 19,519 tokens.
- Prices are from the provider pricing page read on 2026-11-28: cache hit $0.003 per million input tokens off-peak, cache miss $0.15 per million, output $0.60 per million. Peak is exactly double and runs 01:00 to 04:00 and 06:00 to 10:00 UTC on weekdays; this run fell in the off-peak window.
What a hit is worth at 2,896 tokens
| call | prompt tokens | cache hit | cache miss | ms |
|---|---|---|---|---|
| warm (prefix not seen before) | 2,896 | 0 | 2,896 | 686 |
| 2 | 2,895 | 2,688 | 207 | 661 |
| 3 | 2,895 | 2,688 | 207 | 644 |
| 4 | 2,897 | 2,688 | 209 | 678 |
| 5 to 8 | 2,894 to 2,896 | 2,688 | 206 to 208 | 635 to 764 |
Steady state is 2,688 cached of 2,895 prompt tokens, 92.8 per cent, and the 207 that still bill are the question plus the tail of the handbook that never fits into a full cache block. Every hit figure in this post is a multiple of 64, because the cache is accounted in 64-token blocks: 256, 1,792, 2,688, 19,328. Two consequences follow. The last partial block of a prompt is always billed, and a prompt shorter than one block is never cached at all.
That also explains the small arm: a 300-word prefix of 425 tokens returned 256 cached, four blocks, 60 per cent of the prompt. Caching works at small sizes, it just cannot reach the tail.
One line at the top kills the whole prefix
Arm B prepends the string Request 2026-11-28-02, run id 7002. and changes nothing else. Seven follow-up calls: zero cached, 2,911 to 2,914 billed. Not a partial hit, not a smaller hit, zero every time. The prefix behind the line is identical to arm A, character for character, and it does not matter, because matching starts at the first token.
This is the expensive part of ordinary middleware. A request id, a session stamp, a clock, or a run counter is exactly what a logging layer puts at the top of the system prompt, and each of those turns a 92.8 per cent discount into nothing. The same line placed at the bottom of the prompt would have cost twelve tokens.
An edit in the middle costs only the tail
Arm C changes one clause seventy per cent into the handbook, 30 days becomes 45 days, and appends a six-word amendment. The first call after that edit: 1,792 cached, 1,110 billed. 1,792 is twenty-eight blocks of 64, which is where the edit falls. Everything before it was reused.
Calls three through eight in that arm went back to 2,688 cached, because the edited prefix had itself become the cached one by then. Which is the second rule worth internalising: the cache does not hold your original prompt, it holds whichever prefix it saw last. Rotating between two long versions of a handbook on alternate calls is the worst case, since neither version survives to the next call.
The end of the prompt is nearly free
| arm | change to the prefix | cache hit, first call after the change | miss tokens |
|---|---|---|---|
| A | none | 2,688 | 207 |
| B | one request-id line prepended | 0 | 2,912 |
| C | one clause edited 70% in | 1,792 | 1,110 |
| D | a trailing space added | 2,688 | 208 |
| F | a 20-token sentence appended | 2,688 | 227 |
| E, 425-token prefix | none | 256 | 168 |
Appending a space changed nothing. Appending a twenty-token circular cost twenty tokens. Both are obvious once the block accounting is clear, and both are useful: anything that has to change per request should sit at the end, not the beginning.
The hierarchy that follows is worth writing into the prompt builder rather than remembering. Stable rules first, then stable reference material, then the tool schemas, then the per-request fields, then the question. Anything that rotates goes below everything that does not.
Cost per thousand calls
| 1,000 calls, prefix | no cache | cache warm | saved |
|---|---|---|---|
| 2,896 tokens | $0.434 | $0.039 | 91% |
| 19,519 tokens | $2.93 | $0.087 | 97% |
Arithmetic, so it can be checked: hit tokens times $0.003 per million plus miss tokens times $0.15 per million, output excluded because it does not change. At 2,896 tokens the saving is 91 per cent; at 19,519 tokens it is 97 per cent, because the fixed part grows while the per-request tail stays the same size. A batch job that reuses one long instruction set a thousand times a day is where this shows up as real money rather than a rounding difference.
Latency did not move
Mean wall clock across the forty-two calls: 614 to 904 milliseconds, with the uncached warm calls at 633 to 686 milliseconds and cached calls in the same range. At 19,519 tokens the five calls ran 633, 738, 865, 904 and 734 milliseconds, cached from the second call on. If prefill were the bottleneck the cached calls would be visibly faster, and they are not, because generation of the answer dominates at these sizes and this model prefills quickly anyway.
So treat the cache as a bill discount, not a speed feature. Anyone quoting a time-to-first-token win from caching is describing a much larger prompt or a slower prefill than this one.
The recipe
def build_messages(rules, reference, tools, request_fields, question):
# every element above the question is stable across calls
system = rules + chr(10) + reference
return [
{"role": "system", "content": system},
{"role": "user", "content": request_fields + chr(10) + question},
]
Three habits do the work. Keep the volatile fields below the stable ones. Never put a date, a request id or a run counter at the top, put it in the user turn. Do not alternate between two versions of the same reference document on consecutive calls unless the second version is the one you intend to keep.
What this does not show
One provider, one model, one evening. The cache lifetime was not tested, so the hour-scale expiry in the documentation is unverified here. Concurrency was not tested, and parallel workers sharing one prefix may evict each other in ways a serial run cannot reveal. The handbook is synthetic and highly repetitive, so its token boundaries are tidier than a real policy document. And nothing here says whether a cache hit changes the answer: temperature was zero, so there was no answer variance to measure.
Four numbers to keep: 2,688 of 2,896 tokens cached on a stable prefix, 0 of 7 after a single request-id line was prepended, 1,792 recovered after an edit at seventy per cent depth, and 91 to 97 per cent off the input bill at 2.9k and 19.5k tokens. Where the same accounting bites from the other direction is in the 8,038 tokens four MCP servers spend on tool schemas, in the difference between JSON and CSV tool results, and in six codebases that overflow a 200k window; the tokenizer side of the same bill is in twelve sentences counted four ways.
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.