Streaming and semantic caching
medium 6 minA retrieval service can improve perceived latency by streaming, and avoid repeated work by caching.
Video lesson · Chapter 1 of 4
The cached answer from yesterday
Theory
Read or listen first. About 2 min.
What it is
A retrieval service can improve perceived latency by streaming, and avoid repeated work by caching. Streaming changes when content becomes visible. A semantic answer cache decides whether an earlier answer is still eligible for a new request.
Why it exists
Define cache identity before tuning similarity thresholds. Tenant, access scope, document revision and task context can invalidate an answer even when the wording is nearly identical. Benchmark first visible content and final verified completion separately.
A fast cached answer after a policy revision
Inside this chapter
Chapter sources- Streaming Architectures
- RAG streaming has several clocks: retrieval completion, first generated token, first displayed token and final validation. A provisional answer can appear quickly while its citations remain unchecked. Communicate incomplete states and decide which content needs buffering until verification is complete.
Streaming shows text as it is generated, which feels faster but means the user can read sentences before anything has checked them. Decide what may stream immediately (plain explanation) and what should wait for validation (citations, prices, policy numbers). Mark the answer as provisional until checks finish, and track time to first token and time to verified completion as separate metrics.
Example. Retrieval finishes at 0.3 seconds, text appears at 0.8 and citations validate at 2.1. Report all three milestones with their meanings.
Four clocks on one request
Times from the example. Report each milestone separately. - Semantic Caching
- A semantic cache reuses an answer for a sufficiently similar query. Scope cache entries by tenant, permissions, corpus revision and other facts that change the answer. “Can I return it after 30 days?” and “before 30 days?” can be close embeddings but require opposite answers.
A semantic cache embeds the incoming question and looks for a stored question close enough to reuse its answer. Embedding closeness is a weak test of equivalence: negations, dates and before/after flips barely move the vector. So the key must first match on everything that changes the answer (tenant, permissions, corpus revision, locale), and only then is similarity consulted, ideally with a check on qualifiers or a small verifier.
Example. Ask two nearly identical questions with opposite before/after qualifiers. Recheck both policy scope and expected answer before reusing cached text.
Another example. A retail assistant caches "what's the delivery time to Glasgow". A later question "what's the delivery time to Galway" scores 0.97 similarity but crosses a border with different carriers; a location field in the cache key prevents the wrong reuse. Illustrative score.
Cache lookup with eligibility first
Similarity is the last check, not the first. - Exact and embedding caches
- Cheaper caches that sit below the answer cache. An embedding cache stores vectors for text already encoded; a retrieval cache stores the result list for an identical normalised query under the same index revision.
The lower the layer, the safer the reuse. An embedding of identical text with the same model is deterministic enough to cache freely. A retrieval result is safe as long as the index revision and filters are in the key. A generated answer is only safe under a strict eligibility key, because it bakes in evidence, permissions and phrasing. Start caching at the bottom.
Example. A popular question "reset password" is asked thousands of times a day. Its query vector and its top-20 list are cached under index revision 42 and reused until revision 43 is published.
Three layers of reuse
Embedding cache
Key: model version + exact text. Safe: same input, same vector.
Retrieval cache
Key: normalised query + index revision + filters. Invalidated on publish.
Answer cache
Key: scope + revision + similar question. Riskiest; needs eligibility checks.
- Stage latency budget
- Splitting the end-to-end latency target across retrieval, reranking, generation and validation, so each stage has a number to meet and regressions can be traced to one stage.
Users feel tail latency, so budget against a high percentile such as p95, not the mean. Trace every request with per-stage spans so a slow answer can be attributed. Typical levers: fewer rerank candidates, a smaller reranker, caching lower layers, or streaming generation while validation runs in parallel.
Example. A 2.5-second target might allow 0.3 s for search, 0.3 s for reranking, 1.5 s for generation and 0.4 s for citation checks. Illustrative split.
An illustrative budget for a 2.5 s answer
Stage Budget Measured p95 Status Retrieval 0.3 s 0.25 s OK Rerank 50 pairs 0.3 s 0.45 s Over Generation 1.5 s 1.4 s OK Citation check 0.4 s 0.3 s OK Illustrative numbers. The reranker is the stage to tune or shrink.
How it works
Step through the idea, or press Play.
Query context. Tenant A · policy v8
Understand it
Worked example
A fast cached answer after a policy revision
- 1
Tenant A · policy v8
The new question resembles an old question. The customer's scope and effective policy revision still participate in cache eligibility.
- 2
Tenant A · policy v7
A semantic match alone is insufficient. Reject the stale entry and rebuild evidence under v8 before generation.
- 3
First text ≠ final citation
Stream provisional text according to product policy. Record first-token and validated completion latency independently, then cache the verified result in the correct scope.
Follow the handoffs
Cache eligibility for the revised policy
| Field | Request | Cached entry | Match? |
|---|---|---|---|
| Tenant | A | A | Yes |
| Policy revision | v8 | v7 | No |
| Similarity | 0.96 | Not consulted | |
| Decision | Miss, rebuild under v8 |
Lifecycle of a cache entry
Summary
The whole topic in one minute.
Similarity does not transfer permission. Tenant and evidence scope are prerequisites for eligibility.
Define cache identity before tuning similarity thresholds. Tenant, access scope, document revision and task context can invalidate an answer even when the wording is nearly identical. Benchmark first visible content and final verified completion separately.