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
cacheas its path in the trace, and since a hit is an answer, 05’sserved()inscripts/paths.jscountscacheas 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: