---
name: Reuse answers that are almost the same
description: >-
  Teach an edge engineer to build an on-device semantic cache that
  embeds a question, returns a nearby past answer, and tracks hit rate
  for LanguageModelSession on iOS 27 and macOS 27.
---
# Reuse answers that are almost the same

Use this course to build a small cache in front of a `LanguageModelSession`. The cache lives in your app. It is not Apple's token KV cache. Confirm embedding type names in the Xcode 27 SDK you have installed.

## End-to-end agent recipe

1. Start with [lesson 01](01-why-reuse-answers.llms.md) to separate a semantic hit from a fresh generate, and from `usage.input.cachedTokenCount`.
2. Use [lesson 02](02-embed-the-question.llms.md) to embed the question with a Core AI embedding path or the documented Foundation Models embedding helper. Stay on device.
3. Use [lesson 03](03-similarity-and-threshold.llms.md) to store `{embedding, question, answer, createdAt}` and compare with cosine similarity.
4. Use [lesson 04](04-track-hit-rate.llms.md) to count hits, misses, and stale evictions.
5. Use [lesson 05](05-wire-into-session.llms.md) to ask the cache before `session.respond`. On a miss, generate, then write the pair back.
6. Use human HTML for reading and `.llms.md` files for agent execution.

## Constraints

- iOS 27 and macOS 27 only.
- Foundation Models and Core AI. No CUDA. No LangChain.
- Illustrative Swift only. Check names in the installed docs. Do not use Python for the app cache.
- No Mermaid. Build progressive `.flow` diagrams in HTML.
- Do not use em dashes.
- Cite WWDC26 Foundation Models and Core AI lightly.

## Key concepts

- An app-owned semantic cache sits in front of `LanguageModelSession` on iOS 27 and macOS 27.
- Embed the question, compare with cosine similarity, and return a stored answer on a hit.
- This is not Apple's token KV cache (`usage.input.cachedTokenCount`).
- Track hit rate, wrong-hit rate, and stale evictions before you lower the threshold.

## Takeaways

- You can skip `session.respond` when a new question is close enough to a past one.
- You can lower latency and battery use while measuring whether reuse is safe.
- You can wire a cache-first path with stats and a debug screen.

## Acceptance checks

- Every human lesson HTML has **Key concepts** and **Takeaways** sections before lesson-nav. The course home has the same two headings for the whole course.
- Five human lessons exist in numeric order.
- Five matching `.llms.md` briefs exist and link to their human HTML pages.
- Every human lesson has its Agent brief link.
- The course index lists all five lessons, SKILL.md, and all five briefs.
- The cache is described as app-owned. KV cache is named as a different thing.
- Diagrams contain no Mermaid or em dash.

## Environment setup

Xcode 27 on a Mac. iOS 27 or macOS 27 SwiftUI App. Add Foundation Models and Core AI. Confirm embedding APIs in the installed SDK.
