By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Support traffic is repetitive: the same shipping question arrives fifty times a day, worded fifty slightly different ways. Every model call costs tokens, latency and a rate-limit slot. A cache turns a repeated request into a lookup — but the cache is only safe if you understand what its key covers and who is allowed to see a stored answer.
An exact cache keys on the normalised request — messages, model and generation parameters — and returns the stored response only when the key matches exactly. It is cheap, deterministic and safe to reason about. A semantic cache embeds the request and returns the stored answer when a new question is close enough to an old one. It hits more often, but "close enough" can mean "different customer" or "different order", so it needs a threshold, a scope and a review before it goes anywhere near personal data.
LangChain caches at the model wrapper: set_llm_cache installs a process-wide cache and every BaseChatModel call consults it. InMemoryCache lives inside one process and disappears on restart; for a service use a shared backend (SQLite, Redis or a provider-side cache) so replicas do not each pay to learn the same answer. You can also set model.cache on one model instance instead of the whole process, which keeps tests isolated from the production cache.
from langchain_core.caches import InMemoryCache
from langchain_core.globals import set_llm_cache
from langchain_core.language_models.fake_chat_models import GenericFakeChatModel
# One cache per process; use a shared backend across replicas in production.
set_llm_cache(InMemoryCache())
prompt = "Where is order #4521?"
model = GenericFakeChatModel(
messages=iter(["Order #4521 was delivered on Tuesday."])
)
first = model.invoke(prompt)
second = model.invoke(prompt)
print(first.text == second.text)Caching lowers cost; budgets keep it bounded. Track three limits separately, because each fails differently: context tokens per request (does the prompt fit and is the output reserved?), tokens or spend per ticket and per day (does one conversation drain the month?), and provider requests/tokens per minute (does load turn into 429s?). Estimate size before the call as system prompt plus trimmed history plus retrieved passages plus the question plus max_tokens; if that total exceeds the window, trim history or retrieval, not the output budget.
| Budget | Symptom when it breaks | Control |
|---|---|---|
| Context tokens per request | Provider 400, or a silently truncated prompt | Trim history, cap retrieval chunks, reserve output tokens |
| Tokens or spend per ticket and per day | One enthusiastic user drains the month | Per-thread caps, a hard daily cap and an alert at 80% |
| Provider requests and tokens per minute | 429s, retries and rising latency under load | Queue, exponential backoff with jitter, smaller model for routine answers |
| Cache hit rate | Every request pays full price | Measure it, keep prompt versions stable, tune TTLs |
Key takeaways
Support traffic is repetitive, so a cache trades tokens and latency for a lookup — if the key and scope are right.
Exact-match caching is cheap and predictable; semantic caching hits more but needs a threshold, a scope and a review.
An InMemoryCache behind set_llm_cache is process-local; use a shared backend for production and keep tests on their own cache.
Budget context, spend and provider rate separately, and keep cache TTLs and namespaces short and narrow enough for stale or private data.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.