Sub topic 1
What Grounding Means and Why Citations Help
Prerequisites
This chapter assumes you have:
- Basic Python, enough to read short functions and f-strings.
- Experience calling a large language model (LLM) through an API, including how a prompt goes in and text comes out.
- A general idea of retrieval-augmented generation (RAG): a system finds relevant passages and gives them to the model along with the question.
- Basic familiarity with JSON.
You do not need to have built a RAG system. Later chapters build the pieces. This chapter sets up the vocabulary and the reasoning behind them.
What grounding means
A model's answer is grounded when each factual claim in it is supported by material the system supplied to the model for that request. In a RAG system, that material is the set of retrieved passages, called chunks.
Grounding is a property of the relationship between an answer and its sources. It is not a property of the answer alone. The statement "The service retries failed requests up to three times" may be true in the world. It is still ungrounded if none of the supplied sources say it. The model may have recalled it from training data, or guessed it.
This distinction matters because the usual reason to build RAG is to control where answers come from. You may want answers drawn from your own documentation, a policy library, or a contract set. If the model mixes in its own memory, you lose that control, even when the memory happens to be right.
Hallucination, in precise terms
A hallucination is model output that is not supported by reliable information. For a RAG system, it helps to separate two cases:
- Unsupported by the sources. The answer contains a claim that appears nowhere in the retrieved chunks. This is the case grounding addresses.
- Contradicted by the sources. The answer says the opposite of what a chunk says. This is worse, and it is harder to catch.
A third case is easy to overlook: the sources themselves are wrong or out of date. A faithful answer built on a stale document is grounded, and it is still wrong. Grounding gives you traceability, not truth.
What a citation is
A citation is a pointer from a part of the answer to the source that supports it. In this tutorial, the pointer is usually a number such as [2] that maps to a chunk you supplied, and through it to a URL or document.
Citations help in three separate ways.
- Verification. A reader can open source 2 and check the claim. Without a pointer, the reader has to search the whole corpus.
- Constraint on the model. Asking the model to attach a source to each statement pushes it to stay with the supplied text. It does not guarantee this, as the next section shows.
- Debugging and measurement. When an answer is wrong, a citation tells you which chunk the model relied on. You can then tell whether retrieval, the prompt, or the model failed.
A citation does not prove grounding
This is the most important point in the chapter. A citation is something the model writes. The model can write a citation that looks right and is not.
Consider a source and an answer:
[1] (https://example.com/docs/retries)
Failed requests are retried up to three times, with a delay between attempts.
[2] (https://example.com/docs/timeouts)
The default request timeout is 30 seconds.Here are four possible answers to "How many times are failed requests retried?"
| Answer | What is wrong |
|---|---|
| "Failed requests are retried three times [1]." | Nothing. Supported and cited correctly. |
| "Failed requests are retried five times [1]." | **Misrepresented.** The marker is valid, but the claim contradicts source 1. |
| "Failed requests are retried three times [2]." | **Misattributed.** The fact is in the sources, but source 2 does not say it. |
| "Failed requests are retried three times [7]." | **Fabricated marker.** Source 7 was never supplied. |
Only the first answer is both cited and grounded. The other three all contain a citation. A reader who sees [1] or [2] and trusts it without checking has been misled by the presence of a marker.
A fifth case has no marker at all: "Failed requests are retried three times." That statement may be supported, but the reader cannot tell, and neither can your code.
Levels of checking
Each failure above is caught by a different kind of check. These checks get progressively more expensive and less certain, which is why later chapters treat them one at a time.
| Check | Question it answers | Catches | Cost |
|---|---|---|---|
| Marker validity | Does `[n]` refer to a source that was supplied? | Fabricated markers | Very low. A regular expression and a range check. |
| Quote verification | Does the quoted text appear in the cited source? | Invented quotes, some misattribution | Low. String matching. |
| Support checking | Does the cited source actually support the claim? | Misrepresentation, subtle misattribution | Higher. Needs a second model or a human. |
The first two are deterministic code. The third involves judgment, so it can itself be wrong. A good design uses the cheap checks on every answer and saves the expensive one for sampling or high-stakes use.
How the pieces fit together
The diagram shows the path from a question to a checked answer. Grounding is the goal of the whole path, and citations are what let you inspect it.
Each box maps to a later chapter. Numbering and context building come before the prompt. Validation comes after the answer. Nothing in the model's output is trusted until it passes through the check.
Why retrieval limits grounding
The model can only be grounded in what it was given. If retrieval returns the wrong chunks, there are two bad outcomes:
- The model abstains correctly, but the user gets no answer even though the corpus contains one.
- The model reaches past the sources and answers from memory, producing an ungrounded answer that may carry a plausible-looking citation to an irrelevant chunk.
Citations do not repair retrieval. A perfect citation scheme on poor retrieval still gives poor answers. When you debug, check retrieval first: look at the chunks, and ask whether a human could answer the question from them.
A first look at a numbered context
The simplest foundation for citations is giving each chunk a number the model can reference. This function, which later chapters expand on, builds a numbered block from retrieved chunks.
def build_context(chunks):
"""chunks: list of dicts with 'url' and 'text'. Sources are numbered from 1."""
blocks = []
for i, chunk in enumerate(chunks, start=1):
blocks.append(f"[{i}] ({chunk['url']})\n{chunk['text']}")
return "\n\n".join(blocks)
chunks = [
{"url": "https://example.com/docs/retries",
"text": "Failed requests are retried up to three times."},
{"url": "https://example.com/docs/timeouts",
"text": "The default request timeout is 30 seconds."},
]
print(build_context(chunks))The output is the numbered block you saw earlier. Three details matter:
- Numbering starts at 1, because that is how people write citations.
enumerate(chunks, start=1)does this. - The number is a position in this request's list. It is not a permanent document ID. It changes if the list changes order. Keep a mapping from the number back to the real chunk for the lifetime of the request.
- The URL travels with the chunk, so code can map any valid marker back to a link.
Common misconceptions
- "If the model cites a source, the answer is true." The citation shows only what the model claims. The source may be wrong, and the claim may misrepresent it.
- "Grounded means correct." Grounded means supported by the supplied sources. Correctness also depends on the sources.
- "A strong prompt makes citations reliable." A prompt reduces errors. It cannot remove them. Validation in code is what gives you a guarantee, and even then only for what the code checks.
- "No citation means a made-up answer." A statement can be supported and still lack a marker. The missing marker means you cannot verify it, which is a problem on its own.
Edge cases to keep in mind
- Claims that combine sources. One sentence may need two chunks, such as "The default timeout is 30 seconds, and failures are retried three times." The model should cite both,
[1][2]. A check that requires one source per sentence will flag this wrongly. - Reasonable inference. A model may add that three retries at 30 seconds each take at most 90 seconds of waiting. This is derived, not quoted. Decide up front whether your system allows inference, and say so in the prompt.
- Questions the sources cannot answer. The right behavior is to say so, not to guess. This is the topic of the abstention chapter.
- Duplicate or overlapping chunks. Two chunks may contain the same text. A model that cites either is correct. Evaluation should not penalize it for picking the one you did not label.
Summary of the working model
Grounding means the answer rests on the supplied sources. Citations make that checkable by pointing from each claim to a source. They are only a claim by the model until your code verifies them. The rest of this tutorial follows that order: give sources stable identifiers, assemble them, ask for citations, validate them, handle the cases where no source answers the question, and measure how well the whole system does.
Exercise
A model answers "The default timeout is 30 seconds [1]." Source 1 says "Failed requests are retried up to three times." Source 2 says "The default request timeout is 30 seconds." The marker [1] is within the range of supplied sources. Is the answer grounded, is the citation correct, and which kind of check would catch the problem?
Show answer
The claim is supported by the sources as a set, because source 2 contains it, but the citation is wrong. This is misattribution, since the marker points to source 1. A range check on the marker passes, so it would not catch the problem. Quote verification would catch it if the model had to quote the supporting text and code checked the quote against source 1. A support check, where a second model or a human reads source 1 and the claim, would also catch it.
Exercise
Your team reports that answers contain valid-looking citations but users often find the claims are not in the linked pages. Name two different stages of the pipeline you would inspect first, and say what you would look for in each.
Show answer
Inspect retrieval and the prompt or output checking. In retrieval, look at whether the chunks returned for the failing questions contain the answer at all. If they do not, the model is likely answering from memory and attaching a nearby citation. In the prompt and validation stage, look at whether the prompt restricts the model to the sources and whether the code checks that cited sources support the claim, for example with quote verification. Marker range checks alone would not reveal this problem.