Skip to main content
Back to Community & Earn Hub

Semantic Cache Poisoning in Multi-Tenant AI Agents: How Shared Embedding Caches Leak Cross-User Answers and How to Isolate Them

@sneha_patel4 days ago·2 min read 1981 views
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:

  1. User A (Acme Corp) asks: "What is our Q4 revenue forecast and churn rate?" -> Cached under embedding vec_a.
  2. User B (Beta Corp) asks: "What is our Q4 revenue forecast?" -> Cosine similarity is 0.94 (>= 0.90 threshold!).
  3. 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

python
import 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

  1. Pre-filter by tenant_id + role_hash: Never post-filter vector cache results.
  2. Exclude temporal & personal queries: Queries containing "my", "today", or "current" should bypass semantic caching and hit live tools directly.
  3. Include prompt_version in the partition key: Deploying a new system prompt automatically invalidates stale cached answers cleanly.
2 Comments

Discussion

Cross-tenant semantic cache leakage is one of the sneakiest security bugs in AI apps because it doesn't show up in unit tests—it only triggers when two real customers ask semantically similar questions! Partitioning by (tenant_id, role_hash) is mandatory.

@priya_sharma4 days ago

Adding prompt_version to the partition hash is a great touch too—otherwise old cached answers keep getting served after you deploy prompt fixes.

Join the developer discussion

Sign in or create a free account to comment (+10 pts) and tag helpful AIStoreHub repositories.