Q8Architecture

Q8. How do you make sure customer A never sees customer B's data?

Tenant isolation for AI backends in Python and TypeScript: tenant_id from verified auth, Postgres row-level security, pre-filtered vector search, namespaced Redis keys, and never letting the LLM choose the tenant.

Rajendra Kumar · · 2 min read

Q8. How do you make sure customer A never sees customer B's data?

You never rely on a prompt that says "only show data for company A." Prompts can be talked around. Isolation has to live in the backend and the database, where a clever input can't change it.

Login JWT goes to auth middleware, which sets tenant_id in the request context. The query layer always filters by it across Postgres with row-level security, a pre-filtered vector index and namespaced Redis keys. LLM tool arguments trying to set the tenant are ignored.
Architecture · tenant_id comes only from the verified login. The model can't set it, so it can't change it.

How it works

  • From login, not from the request. Middleware verifies the user's token and puts tenant_id into the request context. That's the only place it comes from.
  • In the database. Every table carries tenant_id, and Postgres row-level security (RLS) enforces it, so even a buggy query can't read another tenant's rows.
  • In vector search. The vector index is filtered by tenant before the similarity search, never after.
  • In caches. Redis keys are namespaced per tenant, like t:42:....
  • Not in tool arguments. The LLM never gets tenant_id as a parameter it can fill in. The backend injects it.

Why it matters for agents

An agent that can call tools can be tricked into calling them with different inputs. If tenant_id were a tool argument, a prompt injection could ask for another company's records. When the backend injects it from the verified session, that whole class of attack doesn't exist. ResolveOps does tenant isolation in the database schema for this reason.

Key takeaways

  1. 01tenant_id comes from verified auth, nowhere else.
  2. 02Enforce it in Postgres, vectors and cache keys.
  3. 03The model never sees tenant_id as an input.
Multi-tenancySecurityPostgres RLSpgvectorFastAPIInterview prep