CACHING BY MEANING 按语义缓存

Semantic Caching for LLMs: Reuse Meaning Without Reusing Mistakes 面向 LLM 的语义缓存:复用含义,不复用错误

Semantic caching reuses an earlier answer for a meaningfully similar request. The efficiency gain is real only when identity, intent, freshness, policy and live-data needs are all safe to reuse.

语义缓存会为含义相近的请求复用早期答案。只有身份、意图、新鲜度、策略与实时数据需求都允许复用时,效率收益才真实成立。

Semantic caching flow with similarity policy, validation and risk controls

TL;DR

Similarity is a policy input

An embedding distance is evidence, not the final authorization to reuse an answer.

Tenant isolation is mandatory

Search, keys, metadata and storage must prevent cross-tenant discovery and return.

Freshness changes by workload

Stable explanations may tolerate reuse; prices, availability and tool results may not.

Validate hits like outputs

A cache hit still needs policy, source, quality and response-shape checks.

相似度是策略输入

Embedding 距离是证据,不是复用答案的最终授权。

租户隔离是强制要求

搜索、密钥、元数据与存储必须阻止跨租户发现与返回。

新鲜度因负载而异

稳定解释可能允许复用;价格、可用性与工具结果通常不行。

命中也要像输出一样验证

缓存命中仍需策略、来源、质量与响应形状检查。

A semantic cache is a retrieval system 语义缓存本质上是检索系统

Exact caches key on identical request material. Semantic caches create a representation of intent and search earlier entries for a sufficiently similar candidate. That makes paraphrases reusable, but it also introduces retrieval errors, threshold calibration, metadata filtering and evaluation as production responsibilities.

精确缓存以完全相同的请求材料为密钥;语义缓存生成意图表征,并搜索足够相似的历史条目。这让改写表达也可复用,但同时引入检索错误、阈值校准、元数据过滤与评估责任。

Cache policy should include tenant, user class, model or prompt version, locale, tool requirement, data sources, safety context and freshness window. Some requests should always bypass the cache, especially when they require external actions, current facts or non-idempotent tools.

缓存策略应包含租户、用户类别、模型或提示词版本、语言、工具需求、数据来源、安全上下文与新鲜度窗口。需要外部动作、当前事实或非幂等工具的请求通常应始终旁路缓存。

Semantic cache decision stages 语义缓存决策阶段

Stage 阶段 Best fit 最适合 Verify before choosing 选择前验证
Normalize and fingerprint 标准化与指纹 Remove irrelevant variation while preserving identity and intent. 移除无关差异,同时保留身份与意图。 Never strip fields that affect authorization, safety, tools or meaning. 不得删除影响授权、安全、工具或含义的字段。
Retrieve candidates 检索候选 Search only the authorized tenant and policy partition. 只搜索获授权的租户与策略分区。 Filter model, prompt, locale, sources, tools and freshness before similarity ranking. 相似度排序前按模型、提示词、语言、来源、工具与新鲜度过滤。
Apply reuse policy 应用复用策略 Combine similarity with workload-specific gates. 把相似度与工作负载特定 Gate 结合。 Calibrate thresholds on false-hit cost, not a universal score. 按误命中成本校准阈值,不能采用万能分数。
Validate response 验证响应 Check policy, shape, source, completeness and staleness. 检查策略、形状、来源、完整性与过期情况。 A cached unsafe or obsolete answer is still unsafe or obsolete. 被缓存的不安全或过时答案仍然不安全或过时。
Write with evidence 带证据写入 Store answer, representation and decision metadata. 存储答案、表征与决策元数据。 Version TTL, source timestamps, tenant, prompt, model and invalidation lineage. 版本化 TTL、来源时间、租户、提示词、模型与失效链路。

Decide what is safe to reuse 判断什么可以安全复用

Meaning boundary

Define accepted semantic equivalence with positive and adversarial examples.

Freshness contract

Attach source-aware TTLs and invalidate on model, prompt, policy or data changes.

Isolation boundary

Partition storage and retrieval by tenant plus all authorization-relevant context.

Bypass rules

Skip reuse for tools, actions, live data, personal data and high-risk decisions unless explicitly proven safe.

含义边界

用正向与对抗样例定义可接受的语义等价。

新鲜度契约

附加来源感知 TTL,并在模型、提示词、策略或数据变化时失效。

隔离边界

按租户与所有授权相关上下文划分存储和检索。

旁路规则

工具、动作、实时数据、个人数据与高风险决策默认跳过,除非明确证明安全。

Evaluate a semantic cache safely 安全评估语义缓存

  • Build labeled similar, dissimilar, ambiguous, stale and cross-tenant test pairs.
  • Measure false hits separately from misses because their risk is asymmetric.
  • Test invalidation after prompt, model, policy, source and schema changes.
  • Verify tool and live-data requests cannot return stale cached answers.
  • Canary with traceable hit decisions and a one-switch bypass path.
  • 构建已标注的相似、不相似、歧义、过期与跨租户测试对。
  • 分别测量误命中与漏命中,因为两者风险不对称。
  • 测试提示词、模型、策略、来源与结构定义变化后的失效。
  • 验证工具与实时数据请求不能返回过期缓存答案。
  • 通过可追踪命中决策与一键旁路路径进行灰度。

Put authorization and freshness before similarity 把授权与新鲜度放在相似度之前

Normalize only irrelevant variation, then derive a fingerprint with tenant and policy context. Retrieve from an authorized partition, filter stale and incompatible entries, rank by similarity and apply a workload-specific decision threshold. Validate the selected answer before return and log why the hit was accepted.

只标准化无关差异,并结合租户与策略上下文生成指纹。从授权分区检索,先过滤过期与不兼容条目,再按相似度排序并应用工作负载特定阈值。返回前验证所选答案,并记录接受命中的原因。

Production rule: when the request needs an external tool or current data, bypass is the safe default.

生产规则:当请求需要外部工具或当前数据时,默认旁路才安全。

QVeris makes live-data boundaries explicit QVeris 让实时数据边界更明确

QVeris discovers and calls external APIs, tools, services and live data. Mark those capability requirements in the workload envelope so the semantic cache can bypass reuse or key on verified source freshness. Cache reasoning artifacts only when doing so cannot suppress a required real-world call.

QVeris 发现并调用外部 API、工具、服务与实时数据。应在工作负载封装中标记这些能力需求,让语义缓存能够旁路复用,或按已验证来源新鲜度建立密钥。只有不会抑制必需真实调用时,才缓存推理产物。

The Production Operating Model for LLM semantic cachingLLM 语义缓存的生产运营模型

For Semantic Caching for LLMs, reliability begins when reliable only when requirements, policy, failure behavior, evidence, and ownership are explicit. Turn the diagram into an operating contract that can be tested before launch and during every change.

针对“LLM 语义缓存”,只有当需求、策略、失败行为、证据和责任都明确时,架构才会真正可靠。应把架构图转成运营契约,并在上线前和每次变更期间持续测试。

SCOPE
Define workload classes and objectives
定义工作负载类别与目标

Inventory tenant and authorization scope, normalized input, retrieval context, model and prompt version, similarity threshold, answer validity, freshness, tool effects, invalidation, and cache evidence. For each workflow, set quality, availability, p50 and tail latency, freshness, privacy, regional, cost, and recovery objectives instead of applying one global policy.

盘点租户与授权 Scope、规范化输入、检索上下文、模型与提示词版本、相似度阈值、答案有效性、新鲜度、工具副作用、失效和缓存证据。为每类工作流分别设置质量、可用性、常规与长尾延迟、新鲜度、隐私、区域、成本和恢复目标,而不是套用一个全局策略。

POLICY
Separate eligibility from optimization
把资格判断与优化分开

For Semantic Caching for LLMs, first reject routes that fail capability, authorization, residency, safety, health, or budget constraints. Only then optimize among eligible candidates. Version the policy and record the reason for every decision and override.

针对“LLM 语义缓存”,先排除不满足能力、授权、驻留、安全、健康或预算约束的路由,再在合格候选项中优化。版本化策略,并记录每次决策与覆盖的原因。

GAME DAY
Test degraded behavior deliberately
主动测试降级行为

To validate Semantic Caching for LLMs, inject rate limits, slow streams, malformed output, stale control data, credential loss, regional failure, quota exhaustion, schema drift, and dependent-tool outages. Verify bounded retries, semantic fallback, partial results, and safe recovery.

验证“LLM 语义缓存”时,注入限流、慢速流、畸形输出、过期控制数据、凭证丢失、区域故障、配额耗尽、Schema 漂移和依赖工具中断,验证有界重试、语义故障切换、部分结果和安全恢复。

EVIDENCE
Operate from task-level evidence
基于任务级证据运营

When operating Semantic Caching for LLMs, trace request, policy version, candidate set, selected route, transformations, attempts, latency, usage, cost, validation, and final task result. Tie alerts to runbooks, assign owners, and use incidents to update tests and acceptance thresholds.

运营“LLM 语义缓存”时,追踪请求、策略版本、候选集合、所选路由、转换、尝试、延迟、用量、成本、校验和最终任务结果,把告警连接到运行手册,明确负责人,并用事故更新测试与验收门槛。

FAQ

How is semantic caching different from exact caching?

It can reuse answers for meaningfully similar wording instead of only identical request keys.

What is the biggest risk?

A false hit that returns the wrong, stale, unauthorized or context-incompatible answer.

Should tool calls be cached?

Usually bypass them unless the tool result is idempotent, source-aware and governed by an explicit freshness contract.

语义缓存与精确缓存有何不同?

它能为含义相近的措辞复用答案,而不仅限于相同请求密钥。

最大风险是什么?

误命中导致返回错误、过期、未授权或上下文不兼容答案。

工具调用应被缓存吗?

通常应旁路,除非工具结果幂等、来源明确且受显式新鲜度契约治理。

Official sources and further reading 官方资料与延伸阅读