ealeng

Episode 34 of 56 · Speed & cost

Semantic caching and cascades: shortcuts around the model

Two ways to call an expensive model less often. A semantic cache reuses a saved answer when a new question scores similar enough: "refund within 30 days" and "refund after 30 days" scored 0.96, higher than any pair that really meant the same, and no cutoff on 12 pairs was both useful and safe. A cascade lets gpt-5.4-nano answer first and sends unsure tickets to gpt-5.4-mini: tuned on 16 tickets nano got all right, it chose "never ask mini" and missed an outage.

Lab 12 · Inference & Serving11 min read
Video transcript9:07

0:00Cold open

0:01

A support chatbot answers the same questions over and over.

0:05

And every answer costs a call to a paid AI model.

0:09

So I tried a shortcut: save each answer, and reuse it when a new question means the same thing.

0:15

Here’s a saved question: can I get a refund within 30 days?

0:20

And a new one: can I get a refund after 30 days?

0:24

My similarity check scored them 0.96, where 1 means identical.

0:30

In my test, no two questions that truly meant the same scored that high.

0:36

But these two ask different things. The saved answer is wrong for the new customer.

0:42

So can a shortcut like this ever be safe?

0:45Intro

0:47

Serving models, part six.

0:49

Today: two shortcuts around an expensive model call. A semantic cache, and a model cascade.

0:56Where we are

0:57

I’m Lea, a machine learning engineer at an outdoor gear shop. I run the AI models behind its support team.

1:05

In part four, I timed how fast OpenAI’s API answers.

1:10

In part five, I cut its cost with OpenAI’s prompt cache. But the model still ran every time.

1:18

Today’s cache is my own. When it finds a match, the model isn’t called at all.

1:23

Everything here is real calls to OpenAI’s models, recorded on November 2nd.

1:29

The questions are made up, for a software company’s help desk, and each has a known right answer.

1:3701 · A binder of answers

1:38

Think of a help desk with a binder of answers already given.

1:42

Same question again? Copy the answer out of the binder.

1:47

A semantic cache is that binder, for a model. Semantic means it matches by meaning, not by exact words.

1:55

Reusing a saved answer is called a hit. On a hit, the model isn’t called.

2:01

How much does a hit save? I timed ten of each.

2:04

A hit still makes one quick call, to turn the question into numbers it can compare. That took a quarter of a second.

2:12

A full model call, with answers capped at about 150 words, took nearly two seconds.

2:19

And a miss pays for both: the quick check, then the model.

2:23

In money, a model call cost 25 cents per thousand answers, on a small, cheap model. A hit costs almost nothing.

2:3302 · Measuring meaning

2:35

But how does a program tell that two questions mean the same? It can’t read.

2:40

So each question becomes an embedding: a long list of numbers.

2:45

The model that makes them is trained so that similar meanings get similar numbers.

2:50

Think of it as a spot on a map of meaning. Questions about the same thing land close together.

2:57

The similarity score says how close two spots are. 1 means the same spot.

3:03

Here’s a pair: how do I reset my password? And: I forgot my password, how can I change it?

3:09

They mean the same, and they score 0.77.

3:1503 · Look-alikes

3:16

I tested twelve pairs of questions.

3:19

In six, the new question means the same as the saved one. Reusing the answer is right.

3:25

In the other six, the new question only looks alike. Like renewing a plan, instead of cancelling it. Reusing the answer is wrong.

3:34

Which group do you think scored higher?

3:38

Neither. The pairs that mean the same scored between 0.72 and 0.84.

3:45

The look-alikes scored below that, inside it, and above it.

3:50

And the highest score of all is the refund pair: a look-alike.

3:5504 · The cutoff

3:57

So the cache needs a cutoff: a score where it decides.

4:01

At or above the cutoff, it reuses the saved answer. Below it, it calls the model.

4:07

Try a cutoff of 0.75. Five of the six good pairs are reused.

4:13

But so are three look-alikes. That’s three customers with a wrong answer.

4:19

Raise it to 0.85. Now not one good pair is reused, so the cache saves nothing.

4:26

And the refund look-alike still gets the wrong answer.

4:30

Only a cutoff of 0.96 or higher has no wrong answers. And that one reuses nothing at all.

4:38

Careful, though: I picked these pairs to show the risk. Real traffic may have fewer look-alikes.

4:4505 · A model cascade

4:47

The second shortcut. Back at the help desk, a junior agent answers first.

4:52

When the junior isn’t sure, they ask a senior colleague.

4:56

That’s a model cascade. A small, cheap model answers every request first. If it’s unsure, a bigger, pricier model answers instead.

5:06

The small model is gpt-5.4-nano, the same one from the cache test. The bigger one is gpt-5.4-mini. I’ll call them nano and mini.

5:18

For this, I switched to a task with one right answer, so it’s easy to grade: give each support ticket a priority, from 1 to 3.

5:27

1 is urgent, like an outage or lost data. 2 is one person completely blocked, or a big billing error. 3 is everything else.

5:37

I used twenty-four tickets with known right answers, and ran each one three times.

5:42

Mini got all twenty-four right, every time. Nano got 22 or 23.

5:49

But mini costs 3.7 times as much as nano.

5:5406 · How sure?

5:56

So how does the cascade know nano is unsure?

5:59

The API can also return how likely the model thought its own answer was. That number is called a logprob.

6:07

Turned into a plain probability, it’s the model’s confidence. 0.89 means about 89 percent sure.

6:16

Here’s every ticket, in all three runs, sorted from least sure to most.

6:21

In every run, twenty-one of the twenty-four answers were more than 95 percent sure, and right.

6:28

Now the hard ones. Please remove my former employee’s access: priority 3.

6:34

Nano was only about half sure, every run. And it got it wrong once.

6:39

Nobody on our team can upload files: an outage, so priority 1. Nano said 2, every run.

6:47

And it was about 90 percent sure. Wrong, but confident.

6:52

And a right answer was just as sure: three months of project history gone.

6:57

So confidence can’t tell them apart. A cutoff high enough to catch the outage every run also sends that right answer up to mini.

7:0707 · Dev and holdout

7:09

The cascade needs a cutoff too. Below it, the ticket goes up to mini.

7:14

To choose it fairly, I split the tickets into two sets.

7:18

Sixteen are the dev set. I try cutoffs on those, and keep the cheapest cutoff that scores as well as mini.

7:26

Eight are the holdout. They’re kept aside, for one final check.

7:31

On the dev set, nano got all sixteen right, every run.

7:35

So the cheapest cutoff that matched mini was zero. Never ask mini at all.

7:41

Then the holdout. What happened?

7:44

The upload outage was in the holdout. Nano missed it every time.

7:50

And with a cutoff of zero, it was never passed up to mini.

7:5508 · What the holdout says

7:56

Here’s the holdout, tried with a few cutoffs.

8:00

Any cutoff from 0.95 to 0.99 would have caught the outage. All eight right, every run.

8:08

And it would cost half to two thirds of what mini alone costs.

8:13

But I only know that by peeking at the holdout. Choose the cutoff there, and it’s no longer a fair check.

8:20

The real lesson: to tune a cascade, the dev set needs tickets the cheap model gets wrong.

8:27

My sixteen dev tickets had none. So the tuning had nothing to learn from.

8:32

And with only eight holdout tickets, one ticket is an eighth of the score. This is a demonstration, not a result.

8:41What I do now

8:43

So, can these shortcuts be safe? Here’s what I do now.

8:47

A semantic cache goes only where a wrong reuse does no harm.

8:51

For a cascade, the dev set includes tickets the cheap model gets wrong, mixed in with ones it gets right. Then I tune the cutoff on that.

9:00

And I test both on our own questions. Similar isn’t the same.Lea: Thanks!

Key takeaways

  • ✓A semantic cache saves a model's answers and reuses one when a new question means the same as a saved one. "Means the same" is judged by an embedding similarity score. A hit took 0.24 s (one call to make the embedding) against 1.76 s for a gpt-5.4-nano call capped at 200 tokens, typical of 10 each, and costs almost nothing against $0.25 per 1,000 answers.
  • ✓Similar isn't the same. On 12 pairs, the 6 that meant the same scored 0.72–0.84; the 6 that only looked alike scored 0.68–0.96. The top score of all, 0.96, was "refund within 30 days" vs "refund after 30 days": one word flips the answer, but the texts are nearly identical.
  • ✓So no cutoff was safe. At 0.75 the cache reused 5 of 6 good pairs, and 3 wrong answers. From 0.85 it reused no good pair, but still the refund pair. Only 0.96 and up had no wrong answers, and it reused nothing. The pairs were picked to show the risk; real traffic may have fewer look-alikes.
  • ✓A cascade lets a cheap model answer first and passes the request to a bigger one when the cheap model is unsure. Here, nano (22–23 of 24 tickets right) and mini (24 of 24, at 3.7× the cost). Confidence is the probability nano gave its own one-digit answer, from the logprob the API returns.
  • ✓Confidence helped but wasn't enough: 21 of 24 answers were over 0.95 sure and right, every run, but nano said priority 2 for a team-wide upload outage at about 0.89–0.91 sure, as sure as a right answer about lost project history.
  • ✓Tune the cutoff on examples the cheap model gets wrong. Nano got all 16 dev tickets right, so the lab's rule (the cheapest cutoff that matches mini on dev) chose 0: never ask mini. On the 8 holdout tickets it missed the outage every run. Any cutoff from 0.95 to 0.99 would have caught it, 8 of 8 for half to two thirds of mini's cost, but that was found by peeking at the holdout.

Every call to a paid model costs time and money. This chapter tests two shortcuts around that call: a semantic cache, which reuses an answer already given when a new question means the same, and a model cascade, which lets a cheap model answer first and asks a bigger one only when the cheap one is unsure. Both work by guessing when the expensive call isn't needed, and both can guess wrong.

The source is Lab 12 (Inference & Serving), scripts 05_semantic_cache.py and 06_cascade.py. The video ran the lab's own code once, live, on November 2, 2026: real calls to OpenAI's text-embedding-3-small, gpt-5.4-nano and gpt-5.4-mini, 216 calls for about 2¢. The numbers below are from that recording. The questions and tickets are made up, for a software company's help desk, and each has a known right answer. The lab's README reports its own earlier run; where it differs, it's noted. Chapter 32 timed how fast the API answers, and chapter 33 cut its cost with OpenAI's prompt cache, where the model still runs every time. Today, on a hit, it doesn't.

A binder of answers

Think of a help desk with a binder of answers already given. Same question again? Copy the answer out of the binder. A semantic cache is that binder, for a model. Semantic means it matches by meaning, not by exact words. Reusing a saved answer is a hit; on a hit, the model isn't called.

What does a hit save? The lab timed ten of each on "How do I reset my password?":

Path Typical time (of 10) Range Cost
Hit: one quick call to make the embedding, then a lookup 0.24 s 0.21–0.58 s about $0.0001 per 1,000
Model call, gpt-5.4-nano, answer capped at 200 tokens (about 150 words) 1.76 s 1.58–2.23 s $0.25 per 1,000

Every model call was slower than every hit. A miss pays for both: the lookup, then the model. (The README's run: 0.19 s and 1.72 s.)

Measuring meaning

A program can't read, so each question becomes an embedding: a long list of numbers (1,536 of them here). The model that makes them is trained so that similar meanings get similar numbers. Think of it as a spot on a map of meaning; questions about the same thing land close together. The similarity score (cosine similarity) says how close two spots are: 1 is the same spot.

"How do I reset my password?" and "I forgot my password, how can I change it?" mean the same and score 0.77.

Same meaning vs. look-alikes

The lab scored 12 pairs of "a question already answered" and "a new question". In 6, the new question means the same, so reusing the answer is right. In the other 6 it only looks alike ("cancel my subscription" vs. "renew my subscription"), so reusing the answer is wrong.

  • Same meaning: 0.72 to 0.84.
  • Look-alikes: 0.68 to 0.96: below that range, inside it, and above it.
  • The highest of all, 0.96, is "Can I get a refund within 30 days?" vs. "Can I get a refund after 30 days?". One word flips the answer, and the texts stay almost identical.

An embedding measures how alike two texts are, not whether one answer fits both.

The cutoff

The cache needs a cutoff: at or above it, reuse the saved answer; below it, call the model. Drag it and watch both rows:

Try it · When should the cache reuse an answer?Lab 12 · script 05 · text-embedding-3-small
0.651.00

top row: same meaning, reuse is right bottom row: only looks alike, reuse is wrong

Good reuses (of 6)
5
Wrong answers sent (of 6)
3
Saved questionNew questionSimilarityCache does
Can I get a refund within 30 days?Can I get a refund after 30 days?0.96reuses: wrong answer
Do you offer a student discount?Is there a discount for students?0.84reuses: right
Is my data encrypted at rest?Is my data encrypted in transit?0.83reuses: wrong answer
What are your support hours?When is your support team available?0.78reuses: right
How do I add a teammate to my workspace?How can I invite a colleague to my workspace?0.78reuses: right
How do I export my data to CSV?Can I download my data as a CSV file?0.77reuses: right
How do I reset my password?I forgot my password, how can I change it?0.77reuses: right
How do I add a teammate to my workspace?How do I remove a teammate from my workspace?0.76reuses: wrong answer
How do I cancel my subscription?How do I renew my subscription?0.73calls the model
What does the Pro plan cost per month?What does the Team plan cost per month?0.73calls the model
How do I cancel my subscription?What are the steps to cancel my plan?0.72calls the model
How do I delete my account?How do I delete my project?0.68calls the model

Similarities are the average of 3 runs, which agreed within 0.001. The refund pair is 0.9576: it shows as 0.96, but a cutoff of 0.96 no longer reuses it. The 12 pairs were picked to show the risk; real traffic may have fewer look-alikes.

Cutoff Good pairs reused (of 6) Wrong answers sent (of 6)
0.70 6 5
0.75 5 3
0.80 1 2
0.85 to 0.95 0 1 (the refund pair)
0.96 and up 0 0

Every cutoff that reuses even one good pair also reuses the refund pair. The only cutoff with no wrong answers reuses nothing, so the cache saves nothing. Careful, though: these 12 pairs were picked to show the risk. Real traffic may have far more true repeats and fewer look-alikes; the way to know is to score your own questions.

A model cascade

The second shortcut. Back at the help desk, a junior agent answers first; when they aren't sure, they ask a senior colleague. A cascade does that with models: a small, cheap model answers every request, and if it's unsure, a bigger, pricier model answers instead. Here the small one is gpt-5.4-nano and the big one gpt-5.4-mini; call them nano and mini.

It differs from a router, which picks a model before anything answers. A cascade decides after the cheap model has answered, so it knows more, but a request sent up pays for two calls and takes about twice as long (one call was about 0.6 s for either model).

The task has one right answer, so it's easy to grade: give each support ticket a priority. 1 is urgent: a breach, an outage for many users or a whole team, lost data. 2 is one person completely blocked, or a billing error over $500. 3 is everything else. 24 tickets with known answers, each run 3 times:

  • mini: 24 of 24, every run, at $0.142 per 1,000 tickets;
  • nano: 22, 23 and 23 of 24, at $0.038 per 1,000. Mini costs 3.7× as much.

How sure was nano?

The API can return a logprob for each token: the log of how likely the model thought it was. Nano answers with one digit, so turned back into a plain probability, that's its confidence: 0.89 means about 89% sure.

  • 21 of the 24 answers were more than 95% sure, and right, in every run.
  • "Please remove my former employee's access" (priority 3): nano was only about half sure (0.45–0.53), every run, and said 2 in one run of three.
  • "Nobody on our team can upload files" (an outage, so priority 1): nano said 2, every run, about 0.89–0.91 sure. Wrong, but confident.
  • "3 months of project history disappeared" (priority 1): right, and just as sure, 0.89–0.90.

So confidence can't tell those last two apart. A cutoff high enough to catch the outage every run (above 0.912) also sends that right answer up to mini. (The README's run had nano's least-sure mistake at 0.41.)

Choosing the cascade's cutoff

Below the cutoff, the ticket goes to mini. To choose it fairly, the lab splits the tickets: 16 dev tickets to try cutoffs on, and 8 holdout tickets kept aside for one final check. Its rule: keep the cheapest cutoff whose cascade scores as well as mini on dev.

  • On dev, nano got all 16 right, every run. So a cutoff of 0 already matched mini, and it's the cheapest: never ask mini.
  • On holdout, nano alone got 6, 7 and 7 of 8. The upload outage was in the holdout, nano missed it every time, and with a cutoff of 0 it never went up.

Pick a run, then try the cutoffs on dev and on holdout:

Try it · Nano first, mini when unsureLab 12 · script 06 · recorded gpt-5.4-nano and gpt-5.4-mini answers
Run:Tickets:
Right (of 8)
6
Sent to mini
0
Cost per 1,000
$0.038mini alone $0.143

Nano alone: 6 of 8 right. Mini alone: 8 of 8, every run. A ticket sent to mini pays for both calls.

Nano's confidenceTicketRightNanoFinal
0.45
Please remove my former employee's access. She left the company last week.322 ✗
0.89
Nobody on our team can upload files. Every upload fails with 'network error' since your update.122 ✗
0.99
One of our admins clicked a link in a fake login email and typed in their password.111 ✓
0.99
We were charged $650 for seats we removed last month.222 ✓
0.99
Refund request: I cancelled during the trial but was still charged $15.333 ✓
1.00
My account is locked after too many login attempts and I need to submit payroll today. There's nothing else I can do until it's unlocked.222 ✓
1.00
I can't change my profile picture, the button does nothing. Not a big deal.333 ✓
1.00
We'd love an API endpoint for exporting audit logs. We're comparing you with a competitor and this is a must-have for us.333 ✓

Priority 1 is urgent (an outage, lost data, a breach), 2 is one person completely blocked or a billing error over $500, 3 is everything else. Confidence is the probability nano gave its own one-digit answer (its logprob). Shaded rows went to mini.

On the holdout, in all three runs:

Setup Right (of 8) Sent to mini Cost per 1,000 tickets
Cutoff 0 (the one dev chose) 6–7 0 $0.038
Cutoff 0.95 8 2 $0.074
Cutoff 0.99 8 3 $0.092
Mini alone 8 all $0.143

Any cutoff from 0.95 to 0.99 got all eight right, every run, for about half to two thirds of mini's cost. But that's known only by peeking at the holdout; choose the cutoff there and it's no longer a fair check. (The README's run put 0.99 at about three quarters of mini's cost.)

The lesson is about the dev set: to tune a cascade, it needs tickets the cheap model gets wrong. These 16 had none, so the tuning had nothing to learn from. And with 8 holdout tickets, one ticket is an eighth of the score. This is a demonstration, not a result.

Also in the lab

  • A safety check on hits (untested here). The lab suggests asking nano, on each hit, whether the saved answer fully answers the new question. The video recorded it but didn't use it: nano wrote the saved answers with no product context, so they mostly ask "which service do you mean?", and the check's yes/no says nothing about the design.
  • Other ways to make a cache safer, from the lab's checklist answers: cache only question types where a near miss is harmless, require exact matches on key details (plan names, numbers, dates), and expire saved answers when the facts behind them change, such as a new refund policy.
  • Free-text confidence. Logprobs work for a one-token answer. For free text, the lab's answers list sampling several answers and checking they agree, or a separate check, each at the cost of extra calls.

What to do

  • Put a semantic cache only where a wrong reuse does no harm, or add checks on the details that flip an answer.
  • Measure your repeat rate first, from your logs. A cache saves only on questions that truly repeat.
  • Score your own pairs, including look-alikes, and count wrong reuses at each cutoff, not just savings.
  • For a cascade, build a dev set that includes the cheap model's mistakes, mixed with ones it gets right, then tune the cutoff on it.
  • Check the cutoff once on a holdout you didn't tune on, and use enough tickets that one ticket doesn't swing the score.
  • Count both calls when a request is sent up: in the cost, and in the wait.

Caveats

  • Picked pairs. The 12 pairs were chosen to show the risk; they say nothing about how often look-alikes occur in real traffic.
  • Tiny samples. 12 pairs, 24 tickets, 3 runs. With 8 holdout tickets, one ticket is 12.5 percentage points.
  • Made-up questions and tickets, for a software help desk, each with one right answer chosen by the lab.
  • One day, one network. Times include the internet trip from one connection on November 2, 2026; costs are list prices.
  • Confidence comes from one token. It works for a priority digit, not for a paragraph.

The checklist

  1. Log real questions and measure how many truly repeat before building a cache.
  2. Label pairs as "reuse is right" or "reuse is wrong", including look-alikes that differ by one detail.
  3. Pick the cache cutoff by counting wrong answers, and weigh what one wrong answer costs.
  4. Give saved answers an expiry, or clear them when the facts change.
  5. For a cascade, label examples the cheap model gets wrong and put them in the dev set.
  6. Choose the cutoff on dev, check it once on a holdout, and don't re-tune on the holdout.
  7. Watch the confidently wrong: the cascade can't catch an answer the cheap model is sure of.

Practice: the cache and the cascade in code

Write cosine similarity, the cache's reuse decision, a confidence from a logprob, the cascade's cost and accuracy, and script 06's rule for choosing its cutoff. The tests use the recorded run: the 12 pair similarities (5 good and 3 wrong at 0.75, none of either at 0.96), nano's 22 of 24 and 3.7× cheaper, "never ask mini" from the dev set, 6 of 8 on holdout, and 0.95 and 0.99 at $0.074 and $0.092 per 1,000. The last test adds the outage to the dev set and watches the rule choose a real cutoff.

Try it · Practice in PythonRuns in your browser · ⌘/Ctrl + Enter to run
shortcuts_practice.py52 lines

Quiz

Try it · Check your understanding0 of 6 answered
  1. 1.What does a semantic cache do on a hit?

  2. 2."Can I get a refund within 30 days?" vs "Can I get a refund after 30 days?" scored 0.96. Why is that a problem?

  3. 3.On the 12 pairs, what did a cutoff of 0.75 do?

  4. 4.How does the cascade decide that nano is unsure?

  5. 5.Why did tuning on the 16 dev tickets choose "never ask mini"?

  6. 6.Cutoffs from 0.95 to 0.99 got all 8 holdout tickets right. Why isn't that the answer?

Check your understanding

Try each question before opening its answer.

How does a semantic cache differ from a provider's prompt cache?

A prompt cache (chapter 33) discounts a repeated start of a prompt, and the model still runs. A semantic cache is your own: when a new question scores similar enough to a saved one, it returns the saved answer, and the model isn't called at all.

Why did the refund pair score 0.96, higher than any pair that meant the same?

An embedding measures how alike two texts are. "Within 30 days" and "after 30 days" differ by one word, so the texts are almost identical, but that word flips the right answer. Similar isn't the same.

Why was no cache cutoff safe on these 12 pairs?

The two groups overlap: same meaning 0.72–0.84, look-alikes 0.68–0.96. Every cutoff that reuses one good pair also reuses the refund pair; only 0.96 and up has no wrong answers, and it reuses nothing.

How does a cascade differ from a router?

A router picks a model before anything answers. A cascade lets the cheap model answer first and passes the request up only when that answer looks unsure, so it knows more, but a request sent up pays for both calls and waits for both.

What can a cascade never catch?

An answer the cheap model is sure of but wrong about. The upload outage was about 0.9 sure, as sure as a right answer, so any cutoff that catches it every run also sends that right answer up.

Why did the dev set choose "never ask mini", and how would you fix that?

Nano got all 16 dev tickets right, so a cutoff of 0 already matched mini and was the cheapest. Put tickets nano gets wrong into the dev set, mixed with ones it gets right, tune the cutoff there, and check it once on a fresh holdout.

Next up: Chapter 35 covers speculative decoding: a small model guesses the next few words and a big one checks them, which made one model almost 1.5× faster, and at 4 bits made it slower.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.