Page 1 of 8~112 min topic

Semantic cache lab

Frame the tenant-scoped semantic cache experiment

Ship a falsifiable slice of **tenant-scoped semantic cache for repeated FAQ paraphrases** — success is paraphrase hit returns cached answer with cache=HIT; different tenant never hits, not a polished screenshot.

~14 min this pageExperiment brief

1Try it yourself

Decision drill

Semantic cache lab

Near-duplicate questions should cache-hit — novel ones call the LLM.

Cache wisdom60%

1/3User asks: “What are your refund hours?” (asked many times today).

2Learn the idea

Read

Name the operable slice

This lab builds tenant-scoped semantic cache for repeated FAQ paraphrases. The human in the loop is cost owner cutting duplicate GPT calls for Acme Corp FAQs. Scope is intentionally narrower than “make AI reliable”: you will prove one oracle — paraphrase hit returns cached answer with cache=HIT; different tenant never hits — and one invariant — cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high. Record non-goals in your notes so a later change cannot silently expand authority. The incident mnemonic for the chapter is CACHE-XTENANT-2; design as if that ticket is already written and you are filling evidence.

Read

Write the acceptance contract

Turn the oracle into a table: input fixture, expected observable, prohibited side effect, owner, latency/cost ceiling. Separate model taste from software correctness — transport, auth, parsing, and termination must be deterministic even when generated text varies. Primary metric family: hit_rate ∈ [0.25,0.55] and cross_tenant_hits == 0. Averages without a denominator or revision label do not gate release. Fake external dependencies in unit tests; live calls wait until fakes pass.

Read

Implementation artifact

CACHE_RULES = {"min_sim": 0.92, "scope": ("tenant_id", "policy_version"), "deny_risk": {"high"}}

Read

Freeze the first red test

Before implementation, encode a failing check that would have caught cross-tenant near-duplicate question returns Acme's private pricing blurb. That failure is the pedagogical north star for later pages: contracts reject it, happy path never performs it, validation asserts it, failure-handling contains it, observability detects it, security-ops prevents privilege tricks around it, and mastery replays it in a drill. Endpoint under study: POST /v1/answer.

Read

Stage depth

Capacity note for planners: estimate peak demand on POST /v1/answer and the cost ceiling for a failed retry storm. Write the abort conditions — unbounded spend, cross-tenant leakage, or inability to roll back — before you enjoy the first green test. Prefer synthetic fixtures shaped like production over anonymized production dumps you cannot share in class. When you are tempted to widen scope, re-read the oracle (paraphrase hit returns cached answer with cache=HIT; different tenant never hits) and cut features that do not serve it. The teaching outcome is judgment under constraints: cost owner cutting duplicate GPT calls for Acme Corp FAQs gets a trustworthy control, not a kitchen-sink framework. Keep the language of release decisions: promote, hold, or roll back — never “see if it gets better.”

Read

Field notes for `semantic-cache-lab` / `lab-goal`

Decide what will live in version control on day one: fixtures, contract markdown, and a failing test name. Write the cost ceiling as a hard number with currency and period. If the lab involves clusters, name the non-prod context you will use and forbid prod kubecontexts in scripts. Capture the baseline metric once before changing code so later gains are comparative. Refuse tools that hide the request path behind magic macros until the oracle is green on fakes. Your README section for this page should be five lines or fewer and still falsifiable. In this chapter the product is tenant-scoped semantic cache for repeated FAQ paraphrases, the human stakeholder is cost owner cutting duplicate GPT calls for Acme Corp FAQs, and the incident id you design against is CACHE-XTENANT-2. Re-state the oracle in your notes — paraphrase hit returns cached answer with cache=HIT; different tenant never hits — and keep the invariant visible: cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high. Track hit_rate ∈ [0.25,0.55] and cross_tenant_hits == 0 as the scoreboard. Surface under change control: POST /v1/answer. If you only have forty minutes, finish the fixture for cross-tenant near-duplicate question returns Acme's private pricing blurb before polishing UI. Promotion language stays ternary: promote, hold, or roll back based on evidence, not hope.

Go deeper

Before you start

Why this matters

Write the single done-definition a reviewer would accept for Semantic cache lab (CACHE-XTENANT-2). Include the numeric gate hidden in this oracle: paraphrase hit returns cached answer with cache=HIT; different tenant never hits. Then name the fake success you refuse: a demo that ignores cross-tenant near-duplicate question returns Acme's private pricing blurb. Keep the sentence beside your editor; every later page should make this sentence easier to prove.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Is the oracle (paraphrase hit returns cached answer with cache=HIT; different tenant never hits) falsifiable from a fixture?
2. Is the invariant (cache key includes tenant_id + policy_version; similarity ≥ 0.92 and risk≠high) stated without hand-waving?
3. Does the contract name CACHE-XTENANT-2 as a risk you design against?

All responses are required.