← All sessions, cheat sheets & labs
Week 3 · Field guide · Caching

Semantic Caching

A cache keyed by meaning, not exact text: embed the request, and if a past one is close enough, return its answer instead of paying for the call again.

cachingconcept + tools40–70% fewer calls

What it is

A cache that matches on meaning.

An ordinary cache needs the same string twice. A semantic cache keys on meaning instead: you embed the incoming request, and if a past request is semantically close, you return its cached answer. "Reset my password" and "how do I change my password" become one entry. You implement it with GPTCache, a Redis vector index, or a gateway's built-in cache (Portkey and LiteLLM both ship one), so it is as much a pattern as a product.

The one job

Stop paying for the same question twice.

It stops you paying for the same question asked in different words. When traffic repeats, that is the difference between billing every rephrasing and billing only the genuinely new ones.

Reach for it when

Skip it when

A minimal look

Embed, look up, answer if close.

// embed the request and find the nearest past question
const q = await embed(request);
const hit = await store.nearest(q);   // nearest-neighbour over cached questions

// only trust the cache above a similarity threshold
if (hit && hit.score > 0.92) {
  return hit.answer;             // the call you never make
}

const answer = await callModel(request);
await store.put(q, answer);        // remember it for next time

The principle it teaches

Course principle

The cheapest model call is the one you never make. This is Week 3's first cost lever, but it carries a Week 5 tension: a wrong cache hit is a silent quality bug, so the threshold you pick is something your evals have to police.

Where it fits your labs

This is a Week 3 tool with a Week 5 tail. Lab 3 has you add a semantic-cache step and measure the call reduction; the same lab is where you learn to guard the threshold so cheap answers do not become wrong ones.

Use in
Lab 3, the semantic-cache step, measuring the drop in redundant calls.
week 3
Pairs with
Any embedding model and a vector store; catch bad hits with your eval harness.