Sub topic 13
Grounding Answers in Supplied Documents
What this chapter assumes
You should have read the earlier chapters on why fluent text is not the same as true text, on context failures, and on verifying claims against sources. You should be comfortable reading short Python. The chapter builds on one idea: a model that is given the source text and told to use it makes up fewer facts than a model that answers from memory. It still makes some up.
What grounding means
Grounding is giving the model the documents it should answer from, and instructing it to base its answer on those documents. The documents might be a policy PDF, a support article, a contract, or a few database rows. The model's job changes from "recall what you learned in training" to "read this and report what it says."
This helps for two reasons.
- It fills knowledge gaps. Rare facts, recent events, and private company information are often missing from training data. Putting them in the prompt removes the need to guess.
- It makes checking cheap. If every claim should come from a passage you supplied, you can compare the claim against that passage. You no longer have to search the whole internet.
Grounding does not change how the model works. It still predicts the next token. You have only changed the context so that the most likely continuation is usually the correct one.
The basic setup
A grounded prompt has four parts: the instruction, the documents, the question, and the output format. Each document gets a label so the model can point back to it.
def build_prompt(question, passages):
numbered = "\n\n".join(
f"[{i}] {text}" for i, text in enumerate(passages, start=1)
)
return (
"Answer the question using only the numbered passages below.\n"
"After each claim, cite the passage number, for example [2].\n"
"Support each claim with a short exact quote from the passage.\n"
"If the passages do not contain the answer, reply exactly: "
"NOT IN SOURCES.\n\n"
f"Passages:\n{numbered}\n\n"
f"Question: {question}\n"
)
passages = [
"The report was published in March and covers 2021 data.",
"Refunds are issued within 14 days of a written request.",
]
print(build_prompt("What year does the report cover?", passages))Each instruction in the prompt does a specific job.
| Instruction | What it prevents |
|---|---|
| "Using only the passages" | Mixing in remembered facts that may be wrong or outdated |
| Cite passage numbers | Claims with no traceable origin |
| Quote exactly | Paraphrases that quietly change the meaning |
| Reply "NOT IN SOURCES" | The pressure to guess when the answer is absent |
The last row matters most. Earlier chapters explained that models tend to guess instead of saying they do not know. Grounding gives the model an explicit, easy way out. Without that exit, a model asked about something the documents do not cover will often fill the gap from memory.
Where the documents come from
You can supply documents in two ways.
- Paste them. You put the whole text in the prompt. This works when the material is short and you know which document is relevant. It is the simplest and most predictable approach.
- Retrieve them. A search step finds the most relevant passages from a large collection, and only those go into the prompt. This is called retrieval-augmented generation, or RAG. The model never sees the rest of the collection.
In a RAG system, the quality of the answer depends on the quality of retrieval. If the search returns the wrong passages, the model has nothing correct to work from. It may then answer from memory, or it may build a confident answer from a passage that is only loosely related. Many errors blamed on the model are retrieval errors. When you debug a wrong answer, read the retrieved passages first.
How grounded answers still go wrong
Grounding reduces fabrication. It does not remove it. The failure modes below are the ones to expect.
The model ignores the sources
The model may rely on what it learned in training, especially when the passage disagrees with something it "knows." If a passage says a product costs 40 dollars and the model remembers 30, it may answer 30. This is more likely when the instruction to use only the passages is weak or buried in a long prompt.
The model misreads the sources
The answer cites a real passage but says something the passage does not say. Typical cases are reversing a negation ("is not covered" becomes "is covered"), merging two similar entities, or attaching a condition from one sentence to a fact in another. The citation looks correct, which makes this error harder to spot than an invented source.
Retrieval returns the wrong or partial context
The passage that answers the question is not in the retrieved set. The relevant text may have been split across two chunks, ranked too low, or phrased in different words from the question. The model sees only what retrieval returned, and it cannot know what is missing.
The documents themselves are wrong
Grounding makes the answer faithful to the documents, not faithful to reality. An outdated policy or an incorrect internal memo produces a well-cited wrong answer.
False premises in the question
If the question assumes something untrue ("Why did the 2021 report drop the refund policy?"), the model may accept the premise and explain a change that never happened. Tell the model it may reject a premise, and add that to your instructions.
Checking quotes automatically
Asking for exact quotes gives you something a program can test. If the model says a passage contains a phrase, you can search for that phrase. This check catches invented quotes and quotes attributed to the wrong passage.
import re
def norm(s):
return " ".join(s.lower().split())
def check_quotes(answer, passages):
results = []
for quote, number in re.findall(r'"([^"]+)"\s*\[(\d+)\]', answer):
index = int(number) - 1
if 0 <= index < len(passages):
ok = norm(quote) in norm(passages[index])
else:
ok = False
results.append((quote, int(number), ok))
return results
passages = ["The report was published in March and covers 2021 data."]
answer = 'It covers 2021: "covers 2021 data" [1]. Also "covers 2023 data" [1].'
print(check_quotes(answer, passages))This prints [('covers 2021 data', 1, True), ('covers 2023 data', 1, False)]. The second quote is not in passage 1, so it is flagged.
Know what this check proves and what it does not.
- It proves the quoted words appear in the cited passage.
- It does not prove the answer's claim follows from the quote. A model can quote a true sentence and then draw a wrong conclusion from it.
- It does not catch claims that have no quote at all. Count the claims that lack a quote and treat each as unchecked.
- Whitespace and case are normalized, but punctuation and curly quotes can still cause false mismatches. A failed match means "look at this", not "this is definitely invented."
Prompting details that help
A few habits make grounded setups more reliable.
- Put the instruction near the question. In long prompts, instructions at the very start can be followed less closely than those placed next to the question.
- Keep the passage set small. More text means more chances for the model to pick up a distracting sentence. Retrieve a few good passages rather than many mediocre ones.
- Label the source of each passage. Include a title, date, or document name. This lets the model, and you, notice when two passages conflict or when one is out of date.
- Ask for conflicts to be reported. Add "If passages disagree, say so and cite both."
- Separate the answer from the evidence. Ask for the quotes first and the conclusion second. A model that must produce the evidence before the claim is less likely to invent a claim and then search for support.
Choosing how much grounding you need
Grounding costs effort, so match it to the stakes. For a casual question, pasting one article and asking for a summary may be enough. For answers that others will rely on, such as legal terms, medical dosing, or financial figures, use retrieval, require quotes, run the automatic check, and have a person read the flagged items and a sample of the unflagged ones.
The next chapter covers what all these checks, including this one, still cannot catch.
Exercise
A grounded assistant answers "Refunds take 30 days [2]" and includes the quote "within 30 days of a written request" [2]. Your quote check returns True. Passage 2 says "Refunds are not issued within 30 days of a written request; the review period is 30 days." Is the answer supported? What does this show about the quote check?
Show answer
The answer is not supported. The quoted words do appear in passage 2, so the check passes, but the passage says refunds are not issued in that window. The model dropped the negation. The quote check only confirms that the words exist in the cited passage. It cannot confirm that the claim follows from them, so a person or a separate meaning-level check must still read the claim against the passage.
Exercise
Your RAG assistant gives a wrong answer with a correct-looking citation. Name the first thing you should inspect and one reason that step can cause the error.
Show answer
Inspect the retrieved passages before blaming the model. The relevant passage may be missing because it was split across chunks, ranked too low, or worded differently from the question. The model then works from partial or loosely related text and may fill the gap from memory or from a nearby passage.