Give the model the right evidence.
Learn the retrieval-augmented generation path from documents to search results to grounded answers.
Start with the meaning.
A model's general training may not contain a company's current policies or a user's private files. Retrieval-augmented generation, often called RAG, finds relevant material at request time and supplies it to the model. The model then has evidence that can be cited. This is useful when facts change, documents are too numerous to place in every prompt, or access differs by user.
The pipeline begins before a question arrives. Documents are collected, cleaned, divided into passages, given identifiers, and indexed for search. At question time, search selects candidates, ranking narrows them, and the application packs a small set into the model context. The answer is then checked against those passages and shown with source links. Each step can fail independently.
How the idea works.
Chunk size controls what a search result means. A tiny chunk may lose context; a huge chunk may include too much irrelevant text. Preserve document title, section, version, and permission metadata so the application can interpret a match. Overlap can keep a sentence from being split awkwardly, but overlap also creates duplicate results and extra cost. Chunking is a design choice to evaluate, not a universal number.
Keyword search matches terms; semantic search uses embeddings to find related meaning. Both have strengths. Exact product codes and names often favor lexical matching, while paraphrased questions may benefit from semantic matching. Hybrid retrieval combines signals. Whatever method is used, permission filtering must happen before context reaches the model.
A citation is useful only if it points to evidence that actually supports the claim. The model may cite a passage that mentions a topic but not the asserted fact. Citation validation therefore needs more than checking that an ID exists. For high-stakes uses, inspect claim-to-passage support or require reviewer approval.
A retrieval system has two questions to answer: which passages are relevant, and which passages are allowed? Both matter before generation. If a user asks about their own order, a search result from another customer's order may be highly relevant by wording but forbidden by access rules. The model should never receive it. Permission filtering must be integrated with the search path, and the generated answer should cite only passages actually supplied.
A document also has a lifecycle. It may move from draft to approved to superseded. The index must carry that state, and a new answer should prefer the authoritative version. If two approved sources conflict, an honest system should surface the conflict or route it to review. Merely adding more context can worsen the problem by making both versions available without explaining which one governs.
Follow the code.
def grounded_answer(question, search, generate):
hits = [hit for hit in search(question) if hit["approved"]]
context = hits[:3]
if not context:
return {"status": "declined", "answer": None, "sources": []}
draft = generate(question, context)
allowed = {hit["id"] for hit in context}
cited = set(draft.get("sources", []))
if not cited or not cited <= allowed:
return {"status": "review", "answer": draft.get("answer"), "sources": []}
return {"status": "candidate", **draft}The code filters out unapproved hits, limits context to three, and declines when none remain. It then checks that the generated citations are a nonempty subset of the supplied IDs. A bad citation sends the answer to review rather than publication. The example assumes search results are ranked and approved is a reliable access decision; real systems should enforce permissions in the retrieval layer too.
This check verifies citation identity, not support. The returned status says candidate for that reason. The pipeline makes unsupported answers less likely, but no step turns model text into a guaranteed fact automatically.
What the result does not say.
If the corpus contains conflicting versions, retrieval must choose the authoritative one or show the conflict. A newer file is not always the approved file.
RAG is a data system as much as a prompting technique. Document ownership, updates, deletion, and access control determine whether answers stay trustworthy.
One clear idea.
Grounded answers depend on document quality, retrieval quality, permissions, and citation support.
The AI cookbook
Complete, explained local workflows sit alongside this reading course.