← All courses ← Course home

Lesson 01 of 05

Why reuse a close answer

People ask the same thing in different words. Generating again burns battery and time. A close past answer can be the product.

Agent brief (llms.md)

EDGE FDE work on the phone is paid in heat, latency, and battery. A LanguageModelSession that answers "What is my next meeting?" and then "When is my next meeting?" should not always run the model twice.

AskWhat is my next meeting?
GenerateFull on-device pass
AskWhen is my next meeting?
CacheClose enough
ReturnSame answer, no generate

Two caches, two jobs

Apple's session can report usage.input.cachedTokenCount. That is a token KV cache inside the model run. It speeds a generate that still happens. A semantic cache is different. It is a table you own. You store a question vector and the answer text. You skip the generate when a new question is near an old one.

Do not mix the two in logs. KV cache hits mean the model reused tokens. Semantic hits mean the app never called respond.

When reuse is wrong

Those cases are misses. Lesson 03 sets a threshold. Lesson 04 tracks how often you were right.

EDGE FDE rule. Reuse is a product choice. Measure hit rate before you raise the threshold to look good.

WWDC26 Foundation Models talks about usage and context. It does not ship a semantic cache for you. You build that layer. Next, embed the question on the device.

Key concepts

  • Repeat questions in different words should not always trigger a full generate.
  • A semantic cache is your table of question vectors plus answer text.
  • usage.input.cachedTokenCount is the model's KV cache inside a generate, not a semantic skip.
  • Reuse fails when answers depend on time, files, rewrites, or a different intent.

Takeaways

  • Do not mix KV cache hits and semantic hits in logs.
  • Treat reuse as a product choice, not a free speed win.
  • Know when a close question is still a miss.