When building an LLM semantic cache, gate vector hits with cosine threshold tau=0.08 and exact entity/negation matching to prevent drift.
TL;DR30-second simplified summary▾
L1 matches exact 256-token prefixes via XXH3-64. L2 encodes user turns with ONNX INT8 and searches tenant-filtered SQ8 HNSW in <6 ms, enforcing slot parity before serving cached RocksDB payloads.
P · Problem & Why Exact String Caching Breaks on Natural Language.
In traditional web APIs, URLs are canonical keys. In LLM applications, two users asking 'Summarize Q3 churn drivers' and 'Give me the main reasons customers churned in Q3' have completely different SHA-256 hashes, resulting in a 0% exact-cache hit rate.
Conversely, dropping prompts blindly into a vector database with a loose threshold (tau = 0.20) causes catastrophic false positives: 'How do I delete my prod cluster?' can match 'How do I back up my prod cluster?'.
Exact String Hash vs. Calibrated Dual-Tier Cache
- 10–12% hit rate on paraphrases
- 88% requests hit GPU origin
- No partial KV-prefix reuse
- >25–35% combined hit rate
- <15 ms p99 cache evaluation
- <0.5% semantic drift via slot guard
R · Representation: Radix Prefix Trees & Normalized Vector Geometry.
Every LLM prompt has two parts: a rigid prefix (system instructions, tool schemas, few-shot examples) and a fluid suffix (the user's natural-language question).
L1 stores 256-token prefix hashes in an in-memory Radix Trie keyed by HMAC(tenant_id, model_epoch). L2 embeds only the normalized user turn into a unit-normalized 384-d INT8 vector.
Prompt Splitting Across L1 Radix Trie and L2 HNSW
I · Invariant: Cosine Calibration Curve & Entity/Negation Slot Guard.
Embedding models capture broad topic similarity, not logical polarity. 'Enable audit logging' and 'Disable audit logging' often have cosine distance d = 0.06.
We enforce two invariants before serving an L2 hit: (1) d(u, v) < tau = 0.08, and (2) exact set equality on extracted numbers, dates, currency amounts, and negation tokens.
Cosine Threshold + Lexical Slot Verification
M · Mechanics at Scale: Tenant Isolation & Lazy Epoch Invalidation.
Every cache entry is prefixed with HMAC-SHA256(master_key, tenant_id) and indexed in HNSW with a Roaring Bitmap tenant filter so Tenant A can never retrieve Tenant B's vectors.
When model weights or system prompts change, bumping model_epoch in etcd/Redis invalidates stale entries in O(1) time on read without flushing 100M keys.
O(1) Lazy Model-Epoch Invalidation State Machine
E · Edge Cases, Assumptions & Glossary.
Assumptions & Limitations: Semantic caching assumes deterministic or low-temperature (T <= 0.3) queries where approximate equivalence is acceptable. Never enable L2 semantic caching for creative writing, high-temperature brainstorming, or real-time volatile data (stock prices, live inventory)—route those via TTL = 0s bypass.
Decision Check: If cache evaluation p99 exceeds 12 ms or async shadow drift exceeds 1.5%, fail open directly to the Foundation Model Origin.
Semantic Caching Fit by Workload Profile
Your L2 cache hit rate jumps from 26% to 44% after an embedding model update, and users report wrong answers on date-sensitive queries. What is the fastest safe mitigation?
Practice: Make the trade-off yourself.
Why must an L2 semantic cache pair cosine distance thresholding with an Entity/Negation Slot Guard?