Caching LLM responses: when it saves money and when it serves the wrong answer
How to cache LLM calls in a production app: what is safe to cache, designing cache keys, invalidation when prompts or data change, keeping caches per tenant and measuring whether caching helps.
Caching is one of the first ideas that comes up when an AI feature is slow or expensive. If the same question arrives twice, why pay for the answer twice? Often that’s exactly right. But an LLM cache that’s designed carelessly can serve one customer’s answer to another, keep showing an answer after the data behind it changed, or hide a prompt fix you just shipped.
Caching model output needs the same care as caching anything else, plus a few extra decisions. Here’s how we’d cache LLM responses in a production app.
Decide what is safe to cache
Not every AI output should be reused.
- Good candidates: outputs that depend only on stable inputs, such as a summary of a document that doesn’t change, a classification of an email or extracted fields from an uploaded file.
- Risky candidates: answers that depend on live data, such as account balances, stock levels or anything “as of now”. Cache these only for short periods, or not at all.
- Usually not worth caching: open-ended chat, where the exact same conversation rarely repeats.
- Never cache across users when the answer depends on who is asking, what they can see or their personal data.
Design the cache key carefully
The key decides when two requests count as “the same”. Too loose and you serve wrong answers. Too strict and you never get a hit.
- Include everything that affects the answer: the input, the prompt version, the model, important settings, and the account or user where the answer depends on them.
- Normalise inputs where it’s safe, such as trimming whitespace or ignoring email signatures, so trivial differences don’t cause misses.
- Be careful with “similar” matching. Reusing an answer for a question that’s only close in meaning can save money, but two questions can sound alike and need different answers. Test it on real examples before relying on it.
Plan for invalidation
The hard part of caching is knowing when a stored answer is no longer right.
- Include the prompt and model version in the key, so a prompt change or model upgrade automatically stops old answers being served. Our post on writing system prompts for production covers versioning.
- Tie cached answers to their source data, such as a document’s version or last-updated time, and clear them when it changes.
- Set an expiry even for answers you think are stable. Things change in ways you didn’t predict.
- Make it easy to clear a single entry, a single customer or the whole cache, without a deploy.
Keep tenants apart
In a multi-tenant product, a cache is one of the easiest places to leak data.
- Scope cache keys to the account unless you’re certain the answer contains nothing account-specific.
- Apply your normal retention and deletion rules to cached outputs. If a customer deletes their data, their cached answers go too. See keeping customer data safe in LLM features.
- Test isolation explicitly, with two test accounts asking the same question.
Use cheaper reuse first
Caching the final answer isn’t the only option.
- Store results you already compute, such as a summary saved with the record it describes, rather than a separate cache. Our post on adding AI to an existing SaaS app covers storing results with their inputs.
- Cache the expensive steps, such as embeddings for unchanged documents or retrieved context.
- Check whether your model provider offers its own caching for long, repeated prompt sections, and measure what it actually changes for your traffic.
Measure whether it helps
- Track the hit rate per feature. A cache that rarely hits adds complexity for nothing.
- Compare cost and speed with and without it. What an AI feature costs to run and why your LLM feature is slow show what to measure.
- Sample cached answers for quality, especially after prompt or data changes.
The short checklist
- Cache outputs with stable inputs and avoid caching live or personal answers
- Put input, prompt version, model and account in the key
- Tie answers to source data, set expiries and make clearing easy
- Scope caches to the tenant and apply deletion rules
- Store results and cache expensive steps before caching final answers
- Track hit rate, cost and quality
If your AI feature is slower or more expensive than it should be, see our fixed-scope offers or book a 30-minute call.