Keeping Tenants Apart in a Shared-Index LLM Assistant
System DesignModel answerLast updated
Design authorization boundaries so a multi-tenant document Q&A agent cannot leak data across tenants or sessions, or be steered by injected instructions in uploaded files.
You are building a hosted assistant where many customers upload documents and then ask questions about them. The service chunks and embeds the files into a shared vector cluster, retrieves relevant chunks at question time, feeds them to an LLM, and can optionally call external tools (for example search, email, ticketing or calendar APIs).
Design the trust boundaries that stop one tenant, user or session from seeing another's data, and that stop text hidden inside an uploaded file from taking control of the agent.
Assumptions
- Sharing a vector cluster must not mean sharing access to every vector.
- Arguments produced by the model, and the contents of uploaded documents, are untrusted when making authorization decisions.
- No prompt-injection or content classifier is assumed to be perfect.
- You must also account for sensitive-data exposure, jailbreak attempts and abusive request rates.
Questions you may want to raise first
- Is access scoped by tenant, user, session, document, or a combination?
- Which tools change external state, and which need explicit user confirmation?
- Can collaborators share a session? How are revocation and document expiry propagated?
- What may enter prompts, provider logs, result caches and telemetry?
Part 1 - Data isolation. Follow identity and permissions through upload, indexing, retrieval, context assembly and answer delivery. Explain how caches and asynchronous jobs keep the same boundary.
Part 2 - Guardrail placement. Assign responsibilities to the gateway, orchestrator, retrieval service, model-side checks and a tool broker. Explain how a malicious instruction inside a retrieved document is prevented from authorizing an unrelated action.
Expect a structured answer with trade-offs, failure modes and common mistakes.
Hints
Hint 1
Where does the identity used for a retrieval filter come from? It must never come from the prompt, the model, or the request body fields the client controls.
Hint 2
Think of each layer as having a different failure mode: gateway (who are you, how much), retrieval (which data), model (what looks suspicious), tool broker (what may be done). No layer should be the only defense.
Hint 3
A retrieved document is data, not a principal. Ask what would have to be true for a tool call to be allowed if the document text were deleted from the context.
Hint 4
Cache keys, embeddings of queries, object-store URLs, background re-index jobs and logs are all places where the tenant tag quietly gets lost.
Model answer
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Core principles
- Identity is derived server-side. Tenant, user and session IDs come from the verified auth token and the session record, never from prompt text, model output or client-supplied filter fields.
- Authorization is enforced where data and actions live, not in the prompt. "Please don't reveal other tenants' data" is not a control.
- Layers with different failure modes. Assume the classifier misses something, the model is persuaded, and a developer forgets a filter once. The design should still not leak.
- Untrusted content is data, never authority. Retrieved text can inform an answer; it cannot grant permission.
Part 1 - Data isolation
Upload and ingestion
- The gateway authenticates the caller and attaches a signed context (tenant_id, user_id, session_id, permission_version). The upload service stamps every document with this trusted ownership metadata. Client-provided metadata is ignored for ownership.
- Raw files go to object storage under a tenant-prefixed path with encryption keys per tenant (or per tenant KMS data key). Downloads use short-lived signed URLs minted after an authorization check.
- Async workers (parsing, chunking, embedding) receive the ownership context in the job message, and every chunk inherits it. Workers run with credentials scoped to the job's tenant where practical, so a bug cannot write to another tenant's namespace.
Indexing
- Every vector carries metadata: tenant_id, doc_id, session_id or ACL group, permission version, expiry. For high-sensitivity or large tenants, use a separate namespace, collection or index per tenant, which makes a missing filter a hard failure rather than a leak. For many small tenants, a shared index with mandatory metadata filtering is cheaper; accept the extra risk and compensate with the checks below.
Retrieval
- The retrieval service is the only component with vector DB credentials. It builds the filter from the signed context (tenant AND session/ACL AND not expired AND permission_version current). The orchestrator and model cannot pass or modify it.
- Pre-filtering inside the ANN query is preferred over post-filtering, since post-filtering after top-k can both leak and return too few results.
- Defense in depth after retrieval: re-verify each returned chunk's tenant/ACL against the authoritative document store before it is placed in context. Drop and alert on any mismatch; a mismatch is a security incident signal, not a normal case.
Context assembly and delivery
- Build the prompt only from verified chunks. Wrap them in clear delimiters with provenance (doc_id) so citations can be checked: before returning an answer, confirm every cited doc_id is in the authorized set.
- Streaming responses are bound to the session; the stream channel authenticates on connect and re-checks on reconnect.
Caches
- Every cache key includes tenant, and session or ACL scope, plus permission_version: e.g.
hash(tenant, scope, perm_version, normalized_query). This covers semantic caches, retrieval result caches, embedding caches and answer caches. - Semantic (similarity-based) answer caches are the classic leak: two tenants asking similar questions must never share an entry. Scope them per tenant at minimum, or avoid caching answers derived from private data.
- Bumping permission_version on revocation invalidates cached results without enumerating keys. Expiry is enforced both by TTL and by the retrieval filter.
Revocation and deletion
- Revocation updates the authoritative ACL and bumps the version; retrieval filters pick it up immediately. Deleting a document removes vectors, object-store blobs, cache entries (via version/tag) and queued jobs; run a reconciliation sweep to catch stragglers.
- In-flight async jobs re-check authorization at commit time, not just at enqueue time.
Logs and telemetry
- Redact or hash document content in application logs; tenant-tag everything; restrict provider-side retention (zero-retention agreements where possible). Debug tooling gets the same tenant scoping as production.
Part 2 - Guardrails at the right layer
| Layer | Responsible for | Not responsible for |
|---|---|---|
| Gateway | AuthN, token validation, per-tenant and per-user rate limits, request size/upload quotas, basic abuse and WAF rules | Judging meaning of content |
| Orchestrator | Builds the signed context, controls the agent loop, step and token budgets, assembles prompts | Deciding data access |
| Retrieval service | Authorization filters, post-retrieval ACL verification | Trusting caller-supplied filters |
| Model-side checks | Input/output classifiers for injection patterns, PII, secrets, jailbreak phrasing; output redaction | Being the sole authorization mechanism |
| Tool broker | Authorization, validation and audit of every tool call | Believing the model's stated intent |
Gateway controls cover who and how much; they cannot see that a PDF says "ignore previous instructions". Semantic checks catch some of that, probabilistically. Only retrieval authorization and the tool broker give hard guarantees.
Stopping an injected instruction from triggering an action
- Retrieved text is placed in a clearly marked data section, and the system prompt states it is untrusted. This reduces but does not eliminate risk, so it is not relied on.
- The tool broker authorizes each call against the user's authenticated request and policy, not against anything the model says. Policy checks: is this tool allowed for this tenant and role; do the arguments pass schema validation and allow-lists (domains, recipient lists, record IDs owned by the tenant); is the rate within limits.
- Provenance/taint tracking: if the proposed call was influenced by untrusted retrieved content (or its arguments contain strings from it), treat it as tainted. Tainted state-changing calls require explicit user confirmation showing the exact action and arguments in the UI.
- Least privilege: read-only tools by default; separate scoped credentials per tenant; state-changing tools disabled unless enabled by the tenant; no ambient credentials in the agent. Egress is allow-listed to block data exfiltration via URLs or rendered markdown images.
- Explicit intent check: compare the proposed action with the user's latest message; a question about a contract should not result in "send email to external address".
- Every call is logged with identity, arguments, decision and source chunks for audit and incident review.
Trade-offs
- Per-tenant indexes give strong isolation but cost more and complicate operations; shared indexes with filters scale better but rely on disciplined enforcement and verification.
- Confirmation prompts reduce automation; reserve them for state-changing or tainted actions.
- Post-retrieval verification adds a lookup per chunk; batch it and cache per request.
Common mistakes
- Taking tenant_id from the request body or letting the model supply filter parameters.
- Post-filtering after top-k, or filtering only in the UI.
- Shared semantic caches or embedding caches without tenant scoping.
- Relying on a system prompt or a single classifier as the defense.
- Forgetting async workers, re-index jobs, backups and logs.
- Checking authorization at enqueue time but not at execution time.
- Letting tools run with a broad service account instead of user- or tenant-scoped credentials.