Prompt Caching Practice: How to Reduce LLM API Costs
Prompt caching is one of the most underrated ways to cut costs on LLM API expenses. In this article, I will explain how it works in practice, how to estimate savings, and what pitfalls to watch out for during implementation.
How Prompt Caching Works
Each request to an LLM API processes the entire input context. If you send the same system prompts, long instructions, or common context fragments, these computations are repeated. Caching allows reusing intermediate states for recurring prefixes.
The mechanism depends on the provider. Some enable caching automatically, while others require a special parameter. The general principle is that if the request prefix matches a previously processed one, the cost of processing this prefix is reduced by several times.
Provider Approaches Comparison
| Provider | Mechanism | Cache Hit Cost | Automatic Enablement |
|---|---|---|---|
| Provider A | Automatic prefix caching | 10% of regular price | Yes |
| Provider B | Explicit indication of cacheable blocks | 25% of regular price | No |
| Provider C | Context-level caching | 50% of regular price | Yes |
It's important to understand: caching works only for exact prefix matches. Any change at the beginning of the prompt invalidates the cache for the entire subsequent part.
Patterns for Effective Caching
To maximize benefits, I apply several patterns.
Stable prefix. Place the system prompt, task description, and examples at the beginning of the request. Dynamic data — at the end.
messages = [
{"role": "system", "content": SYSTEM_PROMPT}, # stable part
{"role": "user", "content": f"Task: {task}\nData: {data}"} # dynamic part
]
Grouping by request type. Use different system prompts for different tasks. This increases the likelihood of cache reuse for similar requests.
Caching common instructions. If you have a common instruction block for all requests, place it at the beginning. For example, rules for response formatting or style requirements.
Estimating Savings
To estimate savings, I compare the cost of requests with and without cache. Most providers return metadata in the response indicating whether the cache was used.
response = client.chat.completions.create(...)
usage = response.usage
print(f"Prompt tokens: {usage.prompt_tokens}")
print(f"Cached tokens: {usage.prompt_tokens_details.cached_tokens}")
Knowing the prices for regular and cached tokens, you can calculate savings for each request and in total over a period.
Pitfalls
Unstable prefix. If you add a timestamp or random ID to the beginning of the request, the cache won't work. Ensure the prefix is stable.
Cache size. Providers have limits on cache size and retention time. If requests are infrequent, the cache may be displaced.
Cache write cost. In some cases, writing to the cache is more expensive than regular processing. This is relevant for one-off requests with large context.
Monitoring Tools
To track caching efficiency, I use metrics in observability system. Key indicators:
- cache hit rate;
- cost savings in monetary terms;
- average length of cacheable prefix.
These metrics help understand which prompts are worth further optimization.
Conclusion
Prompt caching is a simple way to reduce LLM API costs without changing application logic. Properly structuring prompts and monitoring metrics is enough. Combined with other methods from caching strategies article, this yields significant savings at scale.
