Metadata, filters and permissions in RAG
8 min read
Text alone is not enough
A ChunkingSplitting a document into passages that a retriever can return one at a time. Chunk size and boundaries decide what the model can read, so a bad cut can separate a rule from its condition. of text such as "Return an unused bike within 14 days" has a problem. Is it current? Which document is it from? Is it meant for customers or for staff? The words cannot say.
So each chunk is stored with a small record of metadata: facts about the chunk, held beside its text. These are the fields that most systems need.
- Source. The file or page the chunk came from, so the answer can point back to it.
- Section. The heading above it, so a reader can find the place.
- Date or version. When it was written or last checked.
- Audience. Who may read it: the public, staff, one customer's account.
- Topic, product or language. Anything you want to narrow a search by.
Metadata is written once, when the document is indexed. It costs almost nothing, and it makes filtering possible.
Filter first, then rank
A Metadata filterA rule on stored facts about a chunk, such as its date or audience, that removes chunks before ranking. It keeps results current and limits users to text they may read. is a rule on those fields, such as "audience is public" or "year is 2025 or later". The retriever applies the rule to throw out chunks, and then ranks only what is left.
The program below holds three chunks. One is an old return policy, and one is for staff only. The search gets the user's groups and a minimum year, and removes chunks that fail either rule.
chunks = [
{"text": "Return an unused bike within 30 days.", "source": "returns-2026.md", "audience": "public", "year": 2026},
{"text": "Return an unused bike within 14 days.", "source": "returns-2023.md", "audience": "public", "year": 2023},
{"text": "Staff may refund up to 500 euros without approval.", "source": "staff-refunds.md", "audience": "staff", "year": 2026},
]
def allowed(chunk, groups, min_year):
return chunk["audience"] in groups and chunk["year"] >= min_year
def search(question, groups, min_year=2025):
query = set(question.lower().replace("?", "").split())
pool = [chunk for chunk in chunks if allowed(chunk, groups, min_year)]
ranked = sorted(pool, key=lambda chunk: -len(query & set(chunk["text"].lower().replace(".", "").split())))
return [chunk["source"] for chunk in ranked[:2]]
question = "How many days to return a bike and refund it?"
print("customer:", search(question, {"public"}))
print("staff: ", search(question, {"public", "staff"}))It prints:
customer: ['returns-2026.md']
staff: ['returns-2026.md', 'staff-refunds.md']The customer sees only the 2026 policy. The 2023 policy never competes, even though it matches the words just as well, and the staff chunk is invisible. The staff member sees both current chunks. Same question, same search, different results, because the filter changed the pool.
Filter before the search, not after
Filters can run in two places, and the difference matters.
| Before ranking | After ranking | |
|---|---|---|
| What it does | Removes chunks, then ranks the rest | Ranks everything, then removes chunks from the top results |
| Results returned | Always a full list, if enough chunks pass | Can return fewer than you asked for, or none |
| Private text | Never scored for this user | Scored first, and a mistake can let it through |
| Used when | You can, which is most of the time | The search tool has no filter option |
Suppose the top 5 results for a customer are all staff chunks. A filter after ranking removes all five, and the customer gets nothing, though a public answer existed at rank 6. Most search tools support filters during the search, so use them.
Permissions belong in the retriever
It is tempting to give the model every chunk and write "do not mention staff-only material" in the prompt. That does not work as a security control. The model has already read the text, and a user can ask in a way that makes it repeat what it read.
The rule is simple: if the user may not read it, it must never enter the prompt. Check permissions in the retriever, using the signed-in user's identity, and keep that identity out of the model's control. The document text from the Loading and cleaning documents Lesson is untrusted, and the user's rights are not something a document should be able to change.
Keep the index current
Metadata also handles change. When a policy is replaced, either delete the old chunks or mark them with an end date, and filter on it. Otherwise an answer from 2023 can sit beside the 2026 rule and the model must guess which to believe. Record a source and a date for every chunk, and check them when you debug a wrong answer.
Where should a permission check happen?
Staff-only refund rules sit in the same index as public policy.
If the user may not read it, it must never enter the prompt. A prompt instruction is not a permission check.
Why filter before ranking instead of after?
The top 5 results for a customer are all staff chunks.
Filtering first means the search ranks only chunks the user may read, so the list stays full.
How do you stop an old policy competing with the new one?
The 2023 and 2026 return rules are both indexed.
A date or version in the metadata lets the retriever return only current text.
Your reading position is saved
0 of 15 complete
Lesson complete
Metadata, filters and permissions in RAG
Up next