Caching AI Responses to Cut Costs and Waiting Time
How a cache that reuses answers to the same question works, and cautions specific to AI. We also cover how to deal with answers going stale.

Ren

Procura
When caching fits and when it doesn't
- Fits: standard answers to frequently asked questions, questions about information that doesn't change
- Doesn't fit: questions whose answers should differ for each user, questions that need to reflect the latest situation
- When the answer changes depending on the flow of a conversation, it's hard to use as is
Ren

Procura
The problem of stale answers
The biggest pitfall of caching is continuing to return an old answer even though circumstances have changed. If you cache answers that include information that can change, like prices or stock, always decide how long they stay valid. If you save without setting an expiry, an answer that was correct quietly turns into incorrect guidance.
- Always set an expiry for cache entries
- Delete related cache entries when the underlying information changes
- Make it possible to tell internally whether an answer was returned from the cache

Procura
Ren
Procura

Procura
How to decide cache keys
Even for the same question text, the answer may need to differ if users have different preconditions. If you ignore those conditions and match the cache on the question text alone, an answer meant for one user may be returned to another by mistake. Include the conditions that affect the answer (a user's settings, the selected language, and so on) in how you distinguish cache entries, along with the question text.
- Identify the conditions that affect the answer and include them in how you distinguish cache entries
- For questions whose answers differ per user, separate the cache per user
- Be careful that adding too many distinguishing conditions makes the cache less effective

Ren
This article is translated from the Japanese original. If the two differ, the Japanese version is the one we keep up to date.