Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Expire Cached Answers With the Reports

In this lesson, you added a response cache to the assistant, watched it tell hikers the Lauzet footbridge was open after the wardens had closed it, and fixed the key so a new report makes the old answers miss.

Key Points

  • A response cache stores the model’s answer and serves it again, with no model call. Hikers ask the same thing all morning, so it saves calls the provider’s prompt cache can’t.
  • The key holds everything in the handler that shapes the answer: the model from the register, the prompt version, the request’s other settings, today’s date and the question, tidied by normalize() so small typing differences share one entry. The instructions run to a few thousand tokens, so their prompt version stands in for them.
  • Each entry expires after an hour, and a sweep every minute deletes the expired ones, so old questions don’t linger. A hit goes back to the hiker with cache as its path in the trace, and since a hit is an answer, 05’s served() in scripts/paths.js counts cache as served. Only a complete answer from the answer role is stored, never a fallback, which would outlive the outage.
  • cacheable() lets in only conditions questions about one place on the trail. No names or bookings, since the cache is one more copy of what hikers type. And no beds, which change by the minute, faster than the key can follow.
  • The wrong turn: with the key missing the reports, an answer cached at 10:31 said “open” until 11:31. The Hut 3 warden filed the closure at 10:40, and five hikers got the stale answer. The prompt version names the instructions, not the reports inside them.
  • The fix: blockVersion() gives the reports block a version, a hash of its full text, and the key holds it. A new report means a new block, a new version and a miss. Any report on the trail changes the version, so every cached answer misses once. That’s safe, but busy report days cost hits.
  • The rule: the key holds everything that shaped the answer, the reports’ version included, and anything it can’t hold stays out of the cache.
  • The numbers, on a simulated morning of 39 questions with a stand-in model: without the version, 16 hits (41.0%), five of them wrong, so only 28.2% of questions got a useful cached answer. With it, 38.5% did, none stale. This toy replay (node scripts/morning.js) gives everyone these numbers. Real hikers word questions in more ways and languages, so expect fewer hits.

Final Code

The response cache, in memory, in cache.js:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In Order Prompts for the Prompt Cache, the provider started reusing the reports block, but the model still writes every answer.

00:08 On a busy morning, hikers ask the same thing again and again. A response cache in your app stores the answer and serves it to the next hiker, with no model call.

00:21 The cache needs a key, built from everything in the handler that shapes the answer. That’s the model from the register, the request’s other settings, today’s date, and the question, tidied so small typing differences share one entry.

00:36 The instructions run to a few thousand tokens, so their prompt version stands in for them.

00:43 Each entry expires after an hour, and a sweep deletes it, so old questions don’t linger. A hit goes back to the hiker, with cache as its path in the trace.

00:55 Only a complete answer from the answer role is stored, never a fallback, which would outlive the outage. It takes only conditions questions about one place on the trail.

01:06 No names or bookings, since the cache is one more copy of what hikers type. And no beds, which change by the minute.

01:15 A script replays a simulated morning, thirty-nine questions answered by a stand-in model that quotes the reports block. At 10:31, a hiker’s answer about the Lauzet footbridge, open, goes into the cache for an hour.

01:30 At 10:40, the Hut 3 warden files a closure. Four minutes later, the next hiker asks, and the cache serves the same answer, open. Four more hikers get it before the entry expires at 11:31.

01:46 That’s a hit rate of 41 percent. Five of those answers were stale.

01:54 The answer came from the reports block, so the block shapes the answer too. The prompt version names the instructions, not the reports inside them. When the block changed, the key stayed the same.

02:08 blockVersion() gives the block a version, a hash of its full text, so new reports give a new version. Add it to the key. Replay the morning. At 10:44, the key holds the new version, so the lookup misses.

02:24 The model answers closed, from the new block. Any report on the trail changes the version, so every cached answer misses once. That’s safe, but busy report days cost hits.

02:39 A cached answer is right only while everything that shaped it holds. So the key holds all of it, the reports’ version included, and anything it can’t hold stays out of the cache.

02:52 Without the version, five of sixteen hits were wrong, so only 28 percent of questions got a useful cached answer. With it, 38.5 percent did. This toy replay gives everyone these numbers.

03:07 Real hikers word questions in more ways and languages, so expect fewer hits. Deciding what to cache, and when questions only look alike, is the next lesson, Decide Which Answers to Cache.

Become a Member to join the conversation.