Semantic Cache Poisoning in Multi-Tenant AI Agents: How Shared Embedding Caches Leak Cross-User Answers and How to Isolate Them
The Multi-Tenant Semantic Cache Vulnerability
Semantic caching stores (query_embedding -> llm_response) pairs in Redis or a vector store so similar future queries return in <10ms without hitting an LLM API.
When teams bolt a global semantic cache onto a multi-tenant AI agent, they accidentally introduce Cross-Tenant Semantic Cache Poisoning:
- User A (Acme Corp) asks: "What is our Q4 revenue forecast and churn rate?" -> Cached under embedding
vec_a. - User B (Beta Corp) asks: "What is our Q4 revenue forecast?" -> Cosine similarity is
0.94(>= 0.90threshold!). - The semantic cache serves Acme Corp's confidential revenue numbers directly to Beta Corp!
Even within the same company, if an HR Director asks "Show active payroll bands" and an Intern asks "Show salary bands", a global cache bypasses Row-Level Security (RLS) completely.
Architecture: Tenant + RBAC Scoped Semantic Cache
Never run a flat semantic similarity lookup. Every cache write and read must be cryptographically partitioned by (tenant_id, permission_scope_hash, prompt_release_id) before vector similarity is evaluated:
text[Incoming Query + Auth Context] │ ▼ [Compute Partition Prefix: sha256(tenant_id + sorted(user_roles) + model_id)] │ ▼ [Redis Hybrid Vector Query: FT.SEARCH idx "@partition:{9f2a...}=>[KNN 1 @vec $q_vec]"] ├── Match >= 0.94 AND time-invariant query ──► Return Tenant-Safe Cache (<8ms) └── Miss (or query contains temporal/personal pronouns) ──► Execute Live Agent
Python Redisvl / Pre-Filtered Lookup Pattern
pythonimport hashlib import numpy as np def build_cache_partition_key(tenant_id: str, roles: list[str], prompt_version: str) -> str: raw = f"{tenant_id}|{'|'.join(sorted(roles))}|{prompt_version}" return hashlib.sha256(raw.encode()).hexdigest()[:16] def is_cacheable_intent(query: str) -> bool: # Never semantically cache personalized or real-time mutable queries! forbidden_tokens = {"my ", "mine", "today", "right now", "latest", "current status"} lowered = query.lower() return not any(tok in lowered for tok in forbidden_tokens) async def lookup_tenant_semantic_cache(redis_vec_client, query: str, query_vec: np.ndarray, tenant_id: str, roles: list[str]): if not is_cacheable_intent(query): return None partition = build_cache_partition_key(tenant_id, roles, prompt_version="v14") # Strict pre-filter guarantees zero cross-tenant or cross-role leakage! hits = await redis_vec_client.search( vector=query_vec, filter_expr=f"@partition:{{{partition}}}", top_k=1, score_threshold=0.94, ) return hits[0]["response"] if hits else None
Three Rules for Safe Semantic Caching
- Pre-filter by
tenant_id+role_hash: Never post-filter vector cache results. - Exclude temporal & personal queries: Queries containing "my", "today", or "current" should bypass semantic caching and hit live tools directly.
- Include
prompt_versionin the partition key: Deploying a new system prompt automatically invalidates stale cached answers cleanly.