← Back to Work

( 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

  • Permissions. If retrieval can return a document the user is not allowed to see, your assistant just leaked it. Filtering after retrieval is too late, because the text is already in the prompt.
  • Freshness. Indexes fail silently. Uploads stop being indexed, nobody notices for weeks, and the assistant answers from stale or missing data.
  • Exactness. Legal, financial and technical questions hinge on exact terms, numbers and clause references. Pure vector search is good at paraphrase and weak at exact matches.
  • Cost and latency. Retrieving on every message, even "thanks!", wastes tokens and time.
  • 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

  • Alert when the index has received no writes for longer than expected.
  • Compare document counts in storage with document counts in the index.
  • Track extraction failures: scanned PDFs need OCR, and spreadsheets often hide most of their data on sheets the parser never reads.
  • 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.

    How to know it is working

  • Freshness lag: the time from upload to searchable.
  • Coverage: the percentage of stored documents that are indexed.
  • Retrieval quality: recall at k on a labelled question set.
  • Permission tests, all passing, all the time.
  • Answer groundedness: the share of claims backed by a cited passage.
  • Checklist

  • A planner decides if, where and what to retrieve.
  • Retrieval mode (full-text, vector or hybrid) matches the question type.
  • Permission filters live inside the query and fail closed.
  • Ingestion is event-driven, with an idempotent reconciler.
  • The index has freshness and coverage alerts.
  • Answers cite sources, and "not found" is an acceptable answer.