Sub topic 9
Reuse stable prompt prefixes with provider caching
This chapter assumes you can call an LLM API, know that input and output tokens are priced separately, and have a baseline of cost and latency from earlier chapters. Here you will learn how provider prompt caching cuts the cost and delay of repeated prompt prefixes.
What provider caching does
Many requests to the same application start with identical text: a long system message, tool definitions, a policy document, or few-shot examples. Without caching, the provider processes those tokens again during prefill on every call, and you pay full input price each time.
Provider prompt caching lets the provider keep the processed state of a prefix for a short time. When a later request begins with the same prefix, the provider reuses that state. Cached input tokens are usually billed at a discount, and time to first token usually drops because less prefill work remains.
This differs from response caching. The model still runs and still generates a fresh answer. Only the input processing is reused.
The prefix rule
Caches match on an exact prefix, token for token, from the start of the prompt. One changed character early on breaks the match for everything after it. This shapes how you build prompts.
Order your prompt from most stable to least stable:
- System instructions and tool definitions.
- Fixed reference material and examples.
- Conversation history that only grows by appending.
- The new user message.
Put anything that changes per request, such as a timestamp, user name, or request ID, after the stable part. A timestamp at the top of the system message defeats caching entirely.
Provider differences to check
Details vary, so read your provider's documentation and do not assume.
| Aspect | What to check |
|---|---|
| Activation | Some providers cache automatically. Others require you to mark cache breakpoints in the request. |
| Minimum size | Prefixes below a minimum token count are not cached. |
| Lifetime | Entries expire after minutes of inactivity, and a hit usually refreshes the timer. |
| Pricing | Cached reads are cheaper. Some providers charge extra to write a cache entry. |
| Scope | Caches are typically tied to your account and the exact model. |
Verify that it works
Do not assume caching is active. Most APIs report cached tokens in the usage block of the response. Compute a hit rate from it.
def cache_hit_rate(usages):
"""usages: list of dicts with 'input_tokens' and 'cached_tokens'."""
total = sum(u["input_tokens"] for u in usages)
cached = sum(u["cached_tokens"] for u in usages)
return cached / total if total else 0.0
print(cache_hit_rate([
{"input_tokens": 2000, "cached_tokens": 0},
{"input_tokens": 2050, "cached_tokens": 1900},
{"input_tokens": 2100, "cached_tokens": 1900},
]))The field names differ between providers, so map them to these two keys. A hit rate near zero on a workload with a long shared prefix means something is breaking the match.
Common errors
- Changing the prompt top. Timestamps, random IDs, or reordered tool lists make each request unique.
- Unstable serialization. If you build tool definitions from a dictionary or set, key order can vary between calls.
- Sparse traffic. If requests arrive less often than the cache lifetime, entries expire before reuse.
- Short prefixes. A prefix under the minimum size gets no benefit.
- Ignoring write cost. If a prefix is used only once, a write surcharge can make caching cost more.
Exercise
Your system message begins with "Current time: <timestamp>" followed by 3,000 tokens of fixed instructions. Your cache hit rate is near zero. What change should you make, and why?
Show answer
Move the timestamp out of the start, to the end of the prompt or into the user message. Caches match on an exact prefix, so the changing timestamp at the top makes every request differ from the first token, and the 3,000 stable tokens never match a cached entry.