Multi-tenant RAG: how to isolate customer data in an AI SaaS product
Namespaces, row-level security or a database per tenant: how to choose a multi-tenant RAG isolation model, and the retrieval-quality trap that a tenant_id filter hides.
If you are building multi-tenant RAG into a SaaS product, the question that
decides your enterprise sales cycle is not which model you use. It is whether
customer A's documents can ever appear in customer B's answer. One such leak
ends a security review, and sometimes the account.
The failure is rarely dramatic. It is a missing WHERE tenant_id = ... in one
code path that was added in a hurry. This post compares the three isolation
models that work, shows where each one stops protecting you, and covers a
retrieval-quality problem that only appears once you share an index.
What are the three ways to isolate tenants in RAG?
Every design is a point on one spectrum: how much infrastructure tenants share.
Model
Isolation enforced by
Cross-tenant leak if...
Typical fit
Shared index, metadata filter
Application code, per query
Any query omits or mangles the filter
Prototypes, internal tools
Shared index, database-enforced (Postgres RLS)
The database
The app connects as a role that bypasses policies
Most B2B SaaS on Postgres
Namespace or partition per tenant
The storage layout
The namespace is taken from user input
Most B2B SaaS on a vector database
Database or cluster per tenant
Infrastructure
Credentials or backups are shared
Regulated or very large tenants
The first row is the one to avoid in production. It makes every future
developer responsible for remembering a filter, and security reviewers know it.
How does a namespace per tenant work?
Vector databases that support namespaces let you store each tenant's vectors
separately. Pinecone's multitenancy guide
recommends one namespace per tenant on a serverless index, says each namespace
is stored separately, and notes that every read and write targets a single
namespace, so a bug that queries the wrong one is less likely to expose another
tenant's data. It describes offboarding as deleting the namespace, a
"lightweight and almost instant operation".
The same page lists the costs of the model:
Namespace limits depend on the plan, from 100 on Starter up to 1,000,000
on Enterprise, so a product with many small tenants needs to check its tier
before choosing this design.
You cannot search across tenants in one query. If you ever need
cross-tenant analytics or a shared knowledge base layered on tenant data, you
need a metadata-filter fallback with its own cost and performance trade-offs.
The remaining risk is where the namespace string comes from. Derive it from the
authenticated session on the server, never from a request parameter, a prompt
or a tool argument the model can influence.
How does pgvector row-level security protect tenant data?
If your application already runs on Postgres, pgvector keeps vectors next to
the rest of your tenant data and lets the database enforce isolation. Row-level
security (RLS) attaches a policy to the table, and the database applies it to
every query regardless of what the application wrote.
The PostgreSQL manual
documents the behaviours that decide whether this is safe:
If no policy exists for a table, a default-deny policy applies: no rows are
visible or modifiable.
Table owners normally bypass row security, unless you run
ALTER TABLE ... FORCE ROW LEVEL SECURITY.
Superusers and roles with the BYPASSRLS attribute always bypass it, and
FORCE does not change that.
In practice this means the application must connect as a dedicated, non-owner
role without BYPASSRLS, and the tenant must be set per transaction from the
authenticated session. Migrations and backup jobs use a different, privileged
role. Teams get this wrong by testing RLS as a superuser, where every policy
appears to pass.
Add a test that connects as the application role, sets tenant A, and asserts
that a query for tenant B's document returns nothing. Run it in CI.
Why does a tenant filter make vector search return too few results?
This is the part most tutorials skip. Approximate indexes such as HNSW find the
nearest neighbours first and filter afterwards. The
pgvector README states that "with
approximate indexes, filtering is applied after the index is scanned", and
gives an example: if a condition matches 10% of rows, with HNSW and the default
hnsw.ef_search of 40, only 4 rows will match on average.
A tenant filter is exactly that kind of selective condition. In a shared index
with many tenants, a small tenant can ask for ten chunks and get two, not
because their data is missing, but because the index scanned a neighbourhood
dominated by other tenants. The visible symptom is an assistant that says it
cannot find something that is plainly in the customer's documents.
The README offers three mitigations:
Iterative index scans (SET hnsw.iterative_scan = strict_order;, or
relaxed_order for better recall), which scan more of the index when the
filter leaves too few rows.
Partial indexes when there are few distinct filter values.
Partitioning when there are many. For tenant isolation specifically, the
README's multitenancy guidance is to use list partitioning or separate
tables, because a shared approximate index lets one tenant's vectors affect
recall for others.
So a per-tenant boundary protects both privacy and retrieval quality. Measure
recall per tenant, not just on an average across the shared index, and include
your smallest tenant in the test set.
Where do multi-tenant RAG systems actually leak?
If you get the vector layer right, the leaks move to places the retrieval
diagram does not show:
Caches. A semantic or response cache keyed only on the query text will
serve tenant A's answer to tenant B asking a similar question. Put the tenant
in the cache key. This matters more if you use the caching patterns in our
guide to reducing LLM API costs.
Agent tools. A tool that fetches a document by id and does not check
ownership turns a prompt injection into a cross-tenant read. The model should
never choose the tenant; the server does. Our post on
AI agent security covers this
boundary in detail.
Logs and traces. Retrieved chunks written to a shared observability
store are tenant data in a place with different access rules and retention.
Embeddings and derived data. Summaries, titles and "related document"
features computed across tenants are copies of data that your deletion
process needs to know about.
Deletion. A tenant offboarding request has to reach the vector store,
the cache, the logs and the backups, with a defined schedule for each.
For products serving Indian users, deletion and purpose-limitation duties are
part of the picture too; see our post on the
DPDP Act and your AI product.
Which model should you choose?
A short decision path that holds up for most products:
Already on Postgres, hundreds to thousands of small tenants: pgvector
with RLS, plus iterative scans or partitioning. Start here unless you have a
reason not to.
On a managed vector database: namespace per tenant, with the namespace
derived server-side. Check the plan's namespace limit against your tenant
count first.
A handful of large or regulated tenants: a dedicated database or cluster
for them, behind the same application interface, sold as a higher tier.
Shared index with a filter: only for prototypes, and write the
migration into the plan.
Whatever you pick, put the choice behind one retrieval function that takes an
authenticated tenant context and nothing else. The design is easier to review,
and moving a large customer from a shared pool to a dedicated store later
becomes a configuration change, not a rewrite.
What to do next
Before you onboard a security-conscious customer, run these checks:
Can any code path build a retrieval query without a tenant context? If so,
remove it.
Does a CI test prove tenant B cannot read tenant A's chunks as the real
application role?
Is your recall measured per tenant, including the smallest one?
Are caches, agent tools, logs and deletion all tenant-aware?
If you are weighing these options for a product you are building, the AI SaaS
page below describes how we approach multi-tenant platforms, and our post on
RAG or fine-tuning is a useful companion if you
are still deciding whether retrieval is the right architecture at all.
CodeKrypt
Drafted by CodeKrypt's AI writing pipeline, built and maintained by our engineers, under fixed rules on sources and claims. How we write
Frequently asked questions
How do you isolate customer data in a multi-tenant RAG system?
Enforce isolation below the application code. Either give each tenant its own namespace, partition or database, or use Postgres row-level security so the database refuses to return another tenant's rows even when a query forgets its filter. A tenant_id filter written by hand in every query is the weakest option.
Is a vector database namespace per tenant secure enough?
Pinecone's documentation describes namespaces as physically separate storage and says a query always targets exactly one namespace, which makes accidental cross-tenant reads less likely. It is a strong default for B2B products, but it does not remove the need to derive the namespace from the authenticated session rather than from user input.
Does pgvector row-level security work with HNSW indexes?
It works for correctness, but the pgvector README warns that with approximate indexes filtering is applied after the index is scanned, so a selective filter can return fewer results than you asked for. Enable iterative index scans, or partition by tenant, and test recall per tenant.
Should each tenant get its own database?
Only when contracts, regulation or a very large tenant demand it. A database per tenant gives the hardest isolation and the heaviest operating cost, so most products reserve it for an enterprise tier.
What is the most common multi-tenant RAG leak?
Anything that sits outside the vector query: a shared response cache, an agent tool that fetches by document id without checking the tenant, or logs and traces that store retrieved chunks. The retrieval step is usually the best-protected part.
Enterprise buyers ask to use their own LLM provider account or encryption keys. What BYOK means in an AI SaaS product, how to store customer keys, and what it does to cost, support and quality.
Enterprise buyers ask where prompts go and how long they live. How provider retention, zero data retention and your own logs fit together, and what to settle before the questionnaire arrives.
LLM output guardrails are a layered set of checks between the model and your user. Which checks are worth their latency, which are theatre, and how to place them.