download

Multi-tenant RAG: filter by tenant before or after top-k?

System design~12 minReviewed 1 Nov 2026Free · No sign-in

Question

A helpdesk company runs one support bot for many client companies. All their help pages share one vector index, and every chunk has a tenant field. For each question the bot takes the top 5 chunks, then drops chunks from other tenants.

An agent at Amber asks "How do I reset 2FA?". The similarities:

chunktenantsimilarity
b1blue0.92
b2blue0.90
b3blue0.88
a1amber0.86
b4blue0.85
a2amber0.80
a3amber0.74
a4amber0.70
a5amber0.65
  1. Which chunks reach the model? What is wrong, even though no Blue text gets through?
  2. Where should the tenant filter go, and what else must include the tenant?
Show hints (3)
  1. "TAKE FIVE FIRST"

    Take the 5 highest similarities over the whole table. Then cross out every Blue chunk. How many are left?

  2. "WHO USED THE PLACES"

    Amber has 5 chunks about 2FA. Why did only one of them reach the model? Whose pages took the other places?

  3. "OTHER SHARED KEYS"

    The bot also caches answers by question text. Two clients ask the same question. Whose answer does the second one get?

Show checked explanation

Only a1 reaches the model. Blue's 4 chunks used 4 of the 5 places and were then thrown away, so Amber gets 1 chunk instead of 5. Filter inside the search query, with the tenant taken from the login, so only Amber's chunks compete. The tenant also belongs in every cache key.

"SOLUTION · PYTHON"
chunks = [  # (chunk id, tenant, similarity to "how do I reset 2FA?")
    ("b1", "blue", 0.92), ("b2", "blue", 0.90), ("b3", "blue", 0.88),
    ("a1", "amber", 0.86), ("b4", "blue", 0.85), ("a2", "amber", 0.80),
    ("a3", "amber", 0.74), ("a4", "amber", 0.70), ("a5", "amber", 0.65),
]
K = 5


def top_k(rows, k):
    return [cid for cid, _, _ in sorted(rows, key=lambda r: -r[2])[:k]]


tenant_of = {cid: tenant for cid, tenant, _ in chunks}
after = [c for c in top_k(chunks, K) if tenant_of[c] == "amber"]
inside = top_k([r for r in chunks if r[1] == "amber"], K)
print("filter after top-5: ", after)
print("filter in the query:", inside)
"OUTPUT"
filter after top-5:  ['a1']
filter in the query: ['a1', 'a2', 'a3', 'a4', 'a5']

Code run on CPython 3.12 against the example above. Checked by an editor other than the author.

Walkthrough

Sort all 9 chunks by similarity and take 5: b1, b2, b3, a1, b4. Drop Blue's chunks and only a1 is left.

where the filter runschunks Amber gets
after the top 5a1
inside the querya1, a2, a3, a4, a5

No Blue text reached the model, but Blue's chunks took 4 of Amber's 5 places. A filter after top-k can return fewer than k results. If Blue had 5 chunks above a1, Amber would get none, and the bot would say "I don't know" about Amber's own docs.

Filter in the query. Send tenant = amber with the vector search, so only Amber's chunks compete for the 5 places. Vector databases call this a metadata filter or pre-filter. Permissions inside one client work the same way: filter by tenant and by the groups the user may read.

Take the tenant from the login. The server reads it from the user's session, never from the question text, a field the browser sends, or a tool argument the model writes.

Put the tenant in every shared key. Say the bot caches answers by question text. Blue asks "How do I reset 2FA?" first, and the answer is built from Blue's pages. Then Amber asks the same question and gets Blue's answer: a real leak. The cache key must be tenant plus question. The same goes for any cache of search results.

Common mistakes

  • Raising k to 50 and filtering afterwards. A big tenant can still fill all 50 places, and you fetch 10 times as many chunks.
  • Telling the model "use only Amber's documents". By then the chunks are already in the prompt. Keep them out of the search instead.
  • Reading the tenant from the request body or a tool argument. A user, or text injected into the prompt, can change it.
  • Removing other tenants' chunks only after reranking. They still pushed Amber's chunks out of the list the reranker received.

Follow-up questions

  • "Your vector index applies the filter only after its approximate search. Now what?" Then the same shortage happens inside the database. Some approximate indexes collect a fixed number of candidates, say 40, and filter afterwards. A filter that matches 10% of rows then leaves about 4. Fixes: a scan that continues until it has k matches, or a separate index or partition for each big tenant.
  • "One shared index, or one index per tenant?" In a shared approximate index, one tenant's vectors can lower another's recall and speed. Separate indexes isolate tenants and make deleting a client easy, but thousands of small ones cost more. Share for small tenants, separate the big ones.

What a strong answer covers

Traces the top 5 and shows the post-filter leaves 1 chunk of 5. Filters inside the search with the tenant from the session, adds it to cache keys, and knows an approximate index may filter late.

Open in interactive practice deck →

Original OfferVibe question. Topic idea from System Design with AI Interview Guide (Rohit Bhardwaj, 2026); text, examples and code are our own.
© 2026 OfferVibe · Text CC BY-NC-SA 4.0 · code MITFree to share and adapt with credit, not for commercial use. Code samples are free for any use.