Team-scoped RAG: how to prevent cross-team data leakage
By Amine Zaghdoudi, CTO and co-founder, Ragirag ·
Most internal RAG prototypes put every document into one vector index and rely on the prompt or the UI to keep teams apart. That works in a demo and fails in production. Isolation has to be part of the data layout and the request path, not a filter added at the end.
The problem with one shared index
A typical first version of an internal assistant looks like this: export documents from a few departments, split them into chunks, embed them, and load everything into one vector collection. A question comes in, the system retrieves the top matching chunks, and a language model writes an answer from them. It is fast to build and impressive in a demo.
The trouble starts when the documents belong to different teams. HR uploads salary bands, Sales uploads margin sheets, Legal uploads a dispute memo. Vector search does not know about departments. It returns whatever is semantically closest to the question, so a sales rep asking about “compensation for the new sales role” can easily retrieve the HR salary band document. The model then summarises it helpfully, with a citation.
Teams often try to patch this with prompt instructions (“only answer using documents from the user’s department”) or by hiding certain sources in the interface. Neither works. The model has already received the sensitive text in its context, so it can paraphrase it, and hiding the citation only hides the evidence that a leak happened.
Make the team the unit of isolation
The fix is to decide, at the architecture level, what the boundary of knowledge is, and to enforce it before retrieval. In Ragirag that boundary is the team. Each team has its own vector collection; a document uploaded to the HR team is chunked and embedded into HR’s collection and nowhere else. A query from the Sales team is only ever run against the Sales collection.
This has three useful properties. First, a mistake in ranking or prompt design cannot cross the boundary, because the other team’s chunks are not in the search space at all. Second, deleting a team is clean: you drop its collection. Third, it is easy to reason about and to explain to a security reviewer, which matters more than it sounds.
Check membership on every request
Separate collections only help if the request path chooses the right one for the right person. Every team-scoped request should verify two facts before doing anything else: the team belongs to the caller’s organisation, and the caller is a member of the team. If either check fails, the request fails with an error, never with an empty or partial result.
Do this check on the server for every request, including streaming chat, document listing and source lookups. It is tempting to trust a team identifier sent by the browser because the interface only shows the user’s own teams. An attacker, or a curious colleague with developer tools, will not respect the interface.
- Resolve the organisation from the authenticated session, not from the request body.
- Look up the membership row for the user and team; reject if it is missing.
- Pass the team identifier to the retrieval engine only after both checks pass.
- Apply the same checks to chat history, so one user’s past questions are never visible to another.
Do not let conversation history cross the boundary
Retrieval is not the only path into the model’s context. Many assistants send recent conversation turns along with each new question so that follow-ups work. If chat sessions are not tied to both a team and a user, a follow-up can drag text from another conversation into the context. Store each session with its team and its owner, and load history only for that pair.
The same applies to anything blocked by a guardrail. If a question was refused because it touched a forbidden topic, do not send it back to the model later as history; otherwise the refusal becomes a delayed leak.
Inside a team, some documents need a second boundary
Team isolation answers the question “which department’s knowledge can this person query?”. It does not answer “which documents inside the department can this person see?”. HR may want the whole team to query the handbook while only two managers can query compensation files.
That second boundary also has to be enforced before retrieval. We cover why in detail in our article on retrieval-time access control; the short version is that any filter applied after the model has seen the text is too late.
Test isolation like a feature
Isolation is easy to break by accident during a refactor, so it deserves automated tests that run on every change. The useful tests are negative ones: they prove that something cannot happen.
- A user from organisation A gets an error when requesting any resource of organisation B.
- A user who is not a member of a team gets an error on every team endpoint, including streaming chat.
- A document uploaded to team X never appears in sources for a question asked in team Y, even when it is the best semantic match.
- Deleting a team removes its collection, documents and chat history.
Summary
Cross-team leakage in RAG is rarely a model problem. It is a data-layout and request-path problem. Give each team its own collection, verify organisation and team membership on the server for every request, scope chat history to the same boundary, and enforce document-level access before retrieval. Then write tests that try to break each of those rules.
