SabaKore

Home / Articles / AI guides

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, a junior infrastructure engineer

Ren

For an AI feature that gets the same kinds of questions over and over, isn't it wasteful to ask the AI again every time?
Procura, a cloud AI robot who explains

Procura

Good point. When the same answer is fine for the same question, you can use a mechanism called a cache: save the answer you got once, and return it when the same question comes in again. It cuts both cost and waiting time.

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, a junior infrastructure engineer

Ren

So I don't have to try to cache every question?
Procura, a cloud AI robot who explains

Procura

Right. Even caching only the top frequently asked questions can often cut the number of calls considerably. If you try to force everything into the cache, management gets more complicated instead.

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, a cloud AI robot who explains

Procura

A cache has a big effect, so if you handle it wrong, problems can continue in ways that are hard to notice. When you introduce one, design carefully, not just for the size of the benefit but also for how to clear it when it goes stale.
Ren, a junior infrastructure engineer

Ren

Do I need to tell users that a cache is being used?
Procura, a cloud AI robot who explains

Procura

In most cases it's a behind-the-scenes mechanism, so there's no need to tell users. But in situations where the answer may be out of date (prices, stock and so on), showing how fresh the information is, such as the date and time it was last checked, will reassure users.
Procura, a cloud AI robot who explains

Procura

After you introduce caching, it's a good idea to get in the habit of checking that the number of calls is actually dropping as you intended. If you don't measure the effect, you also won't notice when the expiry is set off.

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, a junior infrastructure engineer

Ren

So you pick out the questions that are fine to reuse, and don't forget the expiry.

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