Put access control in retrieval, not in the prompt
An AI assistant built on your documents must respect the permissions your team already has. Telling the model to withhold restricted content is not enough; the filter belongs in the retrieval step.
When a company asks for an assistant that answers from its own documents, the first question I ask is not about models or chunk sizes. It is this: who is allowed to read each document today?
Almost every organisation already has an answer. Contracts are visible to the legal team. Salary files are visible to HR. Board papers are visible to a handful of people. An assistant that ignores those rules is not a productivity tool. It is a data leak with a friendly interface.
The tempting shortcut
The quickest way to build retrieval-augmented generation (RAG) is to put every document into one index, retrieve the closest passages for each question and let the model write an answer. When someone points out that not everyone should see everything, the shortcut is to add a line to the prompt:
Do not reveal content from documents the user is not permitted to see.
It looks reasonable, and in a demo it often works. It is still the wrong place for the rule.
Why the prompt is the wrong layer
A prompt is an instruction, not a boundary. Once restricted text has been retrieved and placed in the model’s context, the only thing between that text and the user is the model’s willingness to follow instructions. That willingness can be worn down:
- a user rephrases the question until the model “helpfully” summarises what it was told to withhold;
- a document itself contains text that reads like an instruction and overrides yours;
- a later change to the prompt quietly weakens the rule, and nobody notices until something leaks.
There is also an audit problem. If someone asks “could this user ever have seen that paragraph?”, the honest answer is “yes, it was in the context; we hope the model did not repeat it”. That answer does not survive a security review.
Filter before the model sees anything
The rule I follow is simple: restricted text must never reach the model for a user who cannot open it. That means the permission check happens in the retrieval query itself.
In practice:
- Store access metadata with every chunk. When documents are ingested, each chunk carries the roles, groups or identities allowed to read its source document.
- Filter in the vector query. The search for relevant passages includes the user’s role and identity as a filter, so the database only returns chunks that user may read. With PostgreSQL and PGVector, this is an ordinary
WHEREclause next to the similarity search. - Let the model work with what is left. If nothing permitted matches, the assistant says it cannot find an answer, which is the correct behaviour.
The prompt can still remind the model to stay grounded in the retrieved sources. It just no longer carries the job of security.
What this buys you
- Permissions that hold whatever someone types. There is nothing to talk around, because restricted text was never retrieved.
- A clear audit story. Access is decided by a query you can test, log and review, not by model behaviour you can only sample.
- Fewer surprises when things change. Swapping the model or rewriting the prompt does not change who can see what.
Where it gets harder
Filtering in retrieval is only as good as the access metadata behind it. Two things deserve time early in a project:
- Keeping permissions in sync. When someone changes team or a document’s sharing changes, the index has to follow. Decide up front whether that happens on every change or on a schedule, and what the acceptable delay is.
- Testing with real roles. Build a small set of real questions for each role, with the answers that role should and should not get. It is the fastest way to prove the filter works, and to catch the day it stops working.
The short version
If an assistant must respect who can see what, put that rule where the data is selected, not where the answer is written. The model should only ever see what the person asking is allowed to read.
I applied this pattern across several private retrieval builds, including ones that run entirely on the client’s own servers. The write-up covers the architecture and the decisions behind it.