Prompt caching stores the key and value vectors for previously processed tokens, so a later request with the same prefix skips recomputation for that part. It works only when the prefix matches exactly: any change in whitespace, punctuation or order invalidates the cache. Structure the request with the static content first, instructions and long context documents before the dynamic user query, and the savings follow.
Before you start
An API provider that supports prompt caching, and a workload where many requests share the same instructions or documents.
Find the static part of your prompts
List what repeats across your requests: the system prompt, the policy document, the few-shot examples, the reference table. Measure how many tokens they add up to: caching pays off when the static part is long.
Mark what changes per request: the user question, the date, the order number. Those go last, after the static part.
Put the static part first, unchanged
Order every request the same way: static instructions and documents first, then the dynamic question. Keep the static text byte-for-byte identical across calls, including whitespace.
Do not inject the date or a random id into the static part: one changing token at the start breaks the cache for everything after it.
Check the cache hit, not the hope
Read the response headers or the usage fields for cache hit tokens on a few real calls. A hit means the prefix was reused; a miss means something changed.
If hits are rare, diff two consecutive requests to find the token that differs. It is usually a timestamp, a user id, or a reordered message.
Practice check: fictional Maple Corner support bot, 1,000 calls a day
The table below is fictional practice material, worked out by hand from the stated token counts. It is the expected result for the practice workload, not a run of any API.
| Part | Tokens per call | Cached | Verdict |
|---|---|---|---|
| System prompt + policy doc | 4,000 | Yes | Saved every call |
| User question | 120 | No | New each call |
| Total input | 4,120 | 4,000 cached | 97% cached |
With the static 4,000-token prefix cached, 97 percent of input tokens are cache hits. The 120-token question is the only part processed fresh on each call.
Expected result. UNTESTED: the token counts in the practice table are worked out by hand, not measured by running an API. Order your own requests with the static part first, read the cache-hit fields on real calls, and count the cached share.
Limits
- The cache only matches an exact prefix: any difference in whitespace, punctuation or order is a miss.
- Cached tokens are often free or heavily discounted, but the discount schedule is per provider: check the pricing page.
