共享语义缓存示例没有租户隔离键,可能把一个客户的 AI 答复返回给另一个客户
原文依据:7 处完整策略在入站阶段按语义相似度查找缓存,并在出站阶段保存答复,但示例没有按订阅、用户或租户划分缓存。多租户示例虽然为限流和指标提取 tenantId,却仍未把它用于缓存。
当不同租户提交相似提示时,缓存可能复用包含另一租户上下文、业务数据或个性化内容的答复;较低相似度阈值会扩大错误命中的范围。
示例确实执行语义缓存查找和存储,且未展示租户键;另一个多租户示例只把 tenantId 用于限流和指标。但所给源码没有说明 APIM 语义缓存的实际隔离范围,因此不能仅凭缺少显式键确认缓存会跨订阅或租户共享。用户应要求作者说明缓存的默认作用域,并在多租户部署前验证隔离行为。
这项判断针对展示的代码和适用条件,不表示风险已经实际发生。 <!-- 2. Semantic Cache Lookup --> <azure-openai-semantic-cache-lookup score-threshold="0.8" embeddings-backend-id="embeddings-backend" embeddings-backend-auth="system-assigned" /> <!-- 3. Token Rate Limiting --> <azure-openai-token-limit tokens-per-minute="50000" counter-key="@(context.Subscription.Id)" estimate-prompt-tokens="true" />查看另外 6 个位置
<outbound> <base /> <!-- Cache store (after successful response) --> <azure-openai-semantic-cache-store duration="3600" /> </outbound> <base /> <!-- Extract tenant from subscription or header --> <set-variable name="tenantId" value="@(context.Subscription.Id)" /> <!-- Per-tenant token limit --> <azure-openai-token-limit tokens-per-minute="10000" counter-key="@((string)context.Variables["tenantId"])" estimate-prompt-tokens="true" /> <!-- Per-tenant metrics --> <azure-openai-emit-token-metric namespace="ai-gateway"> <dimension name="Tenant" value="@((string)context.Variables["tenantId"])" />| Cause | Fix ||-------|-----|| `score-threshold` too high | Lower from 0.9 to 0.7 (more matches) || Embeddings backend misconfigured | Verify backend URL and auth || Redis not configured | Deploy Azure Cache for Redis Enterprise with RediSearch || Streaming requests | Semantic caching doesn't work with `"stream": true` |```xml<azure-openai-semantic-cache-lookup score-threshold="0.8" embeddings-backend-id="embeddings-backend" embeddings-backend-auth="system-assigned" />```**Store** (in `<outbound>`):```xml<azure-openai-semantic-cache-store duration="3600" />``` <!-- Extract tenant from subscription or header --> <set-variable name="tenantId" value="@(context.Subscription.Id)" /> <!-- Per-tenant token limit --> <azure-openai-token-limit tokens-per-minute="10000" counter-key="@((string)context.Variables["tenantId"])" estimate-prompt-tokens="true" /> <!-- Per-tenant metrics --> <azure-openai-emit-token-metric namespace="ai-gateway"> <dimension name="Tenant" value="@((string)context.Variables["tenantId"])" /> <dimension name="API" value="@(context.Api.Name)" />