( Engineering Notes · 2026 · 3 min read )
Agentic RAG Done Right: Grounded Retrieval with Access Control Built In
Why naive RAG breaks in production, and how to build retrieval that is permission-safe, fresh, exact and grounded.
The textbook RAG recipe is simple: embed every document, retrieve the top few chunks, and paste them into the prompt. It demos beautifully. In a real company it runs into four problems: permissions, freshness, exactness and cost. This post covers how to build retrieval for agents that is grounded, permission-safe and still fast.
Why naive RAG breaks in production
How to build it well
1. Let a planner decide whether and what to retrieve
Put a small, fast model (or simple rules) in front of retrieval. It decides whether the question needs documents at all, which sources to search and which terms to search for. This cuts cost and latency, and it improves recall because the search query is better than the raw chat message. Tag the planner's output as internal so it never streams to the user.
2. Pick the retrieval method that fits the question
Full-text search (BM25) is excellent for exact terms, identifiers and numbers. Vector search is good for semantic paraphrase. Hybrid search combines both when you need both. Do not default to vectors-only because a tutorial did.
3. Enforce access inside the query, and fail closed
Apply tenant and permission filters inside the search query itself, so documents the user cannot read are never retrieved at all. If the permission lookup fails, return nothing rather than everything.
def search(user: User, query: str, k: int = 8) -> list[Passage]:
try:
principals = permissions.principals_for(user) # user id, roles, groups
except PermissionServiceError:
log.warning("permission lookup failed, returning no results", user=user.id)
return [] # fail closed
return index.search(
query,
k=k,
filter={"tenant": user.tenant_id, "allowed_principals": principals},
)Write tests that prove user A can never retrieve user B's private document, and run them on every change.
4. Make ingestion event-driven, with a reconciler as a safety net
Trigger indexing from storage events (upload, then a function, then the indexer) for speed. Events still get lost, so add a periodic, idempotent reconciler that compares the source of truth with the index and backfills anything missing. Use deterministic document IDs so re-indexing the same file is always safe.
5. Monitor the index like a production database
6. Ground every answer
Return passages with their source IDs, ask the model to cite them, and prefer quoting over paraphrasing for anything that will be acted on. If no passage supports an answer, the right answer is "I could not find this", not a guess.