LLMs and Generative AI › Retrieval-Augmented Generation › Day 270

Day 270: Citations and Grounded Answers

Day 270 of 365 — Citations and Grounded Answers

Eliminate factual hallucinations with verifiable source citations: design system prompts enforcing strict bracketed inline citations, build post-generation claim decomposition validators, and compute precision grounding scores over retrieved passages.

Course
LLMs and Generative AI
Category
Retrieval-Augmented Generation
Reading time
≈ 35 min
Practical time
≈ 50 min
Lesson duration
1h 25m
Last verified
2026-08-30

Hands-on lab for this lesson

Lab files on GitHub: https://github.com/ai-roadmap-365/ai-roadmap-365.github.io/tree/main/labs/sections/llms-and-generative-ai/day-270-citations-and-grounded-answers

  1. Get the hands-on files. Clone the labs repository once (you can reuse this clone for every lesson). This works on macOS, Linux, and Windows (PowerShell or WSL):
    git clone https://github.com/ai-roadmap-365/ai-roadmap-365.github.io.git
    cd ai-roadmap-365.github.io
  2. Open this lesson's lab. Move into the directory for this specific day. Every lab lives at the same predictable path — section / subsection / week / day:
    cd labs/sections/llms-and-generative-ai/day-270-citations-and-grounded-answers
  3. Read the lab guide. Open `README.md` in that directory. It lists the exact commands, what each does, the expected output, and how to check your work — read it before running anything.
  4. Run it and check your work. Follow the README's "How to run" section: run the example first to see the finished result, then complete the numbered exercises in `starter/`, then run the tests. The tests pass (exit 0) only when your work is correct.
    bash tests/run_tests.sh   # or the test command named in the lab README

You can also open the lab as a local page (works offline, shows the file tree and expected output).

Learning objectives

By the end of this lesson you will be able to:

Prerequisites

Why this matters

A model that answers correctly and a model that answers correctly and can show you where are separated by one property, and it is the property that decides whether the system is usable in a regulated setting.

“Prompt caching costs $3.75 per million write tokens” is an assertion. The same sentence followed by [2], where [2] resolves to a specific sentence in a specific document, is a checkable claim. A reader can verify it in seconds. An auditor can verify it later. A test can verify it continuously.

The difference matters because grounded-looking answers are cheap to produce and hard to trust:

  ungrounded   "Write caching costs $3.75/M tokens."
  decorated    "Write caching costs $3.75/M tokens [1]."     ← [1] cites the
                                                               pricing page,
                                                               which says $3.00
  grounded     "Write caching costs $3.75/M tokens [1]."     ← [1] is the exact
                                                               sentence that
                                                               says $3.75

The middle case is the dangerous one. It has the visual signature of rigour and none of the substance, and users trust it more than an uncited answer. Today is about producing the third case and mechanically detecting the second.


The idea in plain language

Diagram: the four steps that turn a claimed citation into a verified one

Treat a citation as a claim about a claim: “sentence S in source D supports statement C”. Written that way, it is something a program can check.

Journalism has the right instinct here. A quote gets a name attached, and if you cannot attribute it you cut it. Not “roughly this, from somewhere in the report” — this sentence, this page.

The unit matters enormously. Citing a document is nearly worthless when the document is 300 pages, because verifying the claim means reading it. Citing a chunk is better. Citing the sentence is what makes verification take seconds rather than an afternoon, and seconds is the difference between a citation people check and a citation people ignore.

The system therefore does something slightly odd: it takes the answer apart into individual claims, and asks of each one separately whether the retrieved context supports it.


Historical background

Diagram: prompted citations compared with verified citation pipelines

The arc runs from perfect attribution with no fluency, through fluency with no attribution, to the current attempt to have both.


What it is — and what it is not

It is a mechanism with three parts: split the answer into atomic claims, map each claim to the retrieved sentences that support it, and refuse — or flag — any claim that maps to nothing.

It is not a fact-checker. Grounding says a claim follows from the retrieved context. If the context is wrong, a perfectly grounded answer is confidently wrong, and no amount of citation machinery detects that.

PropertyWhat it guaranteesWhat it does not
GroundedThe context says thisThe context is correct
CitedA source is namedThe source says what is claimed
VerifiedA person or test checkedIt stays true tomorrow

The middle row is where most systems actually sit while presenting as the third. Distinguishing them is the point of today’s lab.


Why it was created and what problems it solves

Four failure modes, in rough order of how often they appear in practice:

The unsupported sentence. The answer contains a fluent sentence that no retrieved passage supports. The model filled a gap from parametric memory. It may even be true, which makes it harder to spot.

The mis-citation. A claim carries [2], but the supporting text is in [3] — or nowhere. The citation is decorative. Readers who spot-check one citation and find it plausible then trust the rest.

The over-broad citation. The answer cites a whole 800-word chunk for a specific number. Technically the number is in there. Verifying it means reading 800 words, so nobody does.

The stale citation. The source was re-indexed and the chunk boundaries moved. [2] now points at different text than when the answer was generated. The citation is live, resolvable, and wrong.

Each is invisible to a reader skimming for the citation marker. Each is detectable mechanically, which is what makes this worth automating rather than reviewing.


How it works

Inline Citation Attribution Flow diagram

1. Split the answer into atomic claims

Diagram: the four properties of splitting an answer into verifiable claims

A claim is one assertion. “Write caching costs $3.75 per million tokens and read caching costs $0.30” is two claims and needs two citations, possibly to two different sentences.

Sentence splitting is a reasonable first approximation, but conjunctions matter: split on sentence boundaries first, then on ” and ” where both halves contain a number or a named entity. Over-splitting is much safer than under-splitting, because an unsupported half-sentence is exactly what you are hunting.

2. Index the retrieved context by sentence

Diagram: why retrieved context is indexed by sentence rather than by document

The chunks that went into the prompt are split into sentences, each keeping its chunk id and character offsets. This is the citation target set — and note that it is built from what was actually retrieved, not from the corpus. A claim can only be supported by something the model could see.

3. Score each claim against each sentence

Diagram: the three possible relationships between a claim and a source sentence

Two signals, and both are needed:

Take the best-scoring sentence per claim. Where the best score falls below a threshold, the claim is unsupported — and that outcome is the useful one.

4. Enforce, do not decorate

Diagram: the four possible responses to an unsupported claim in a generated answer

Given the mapping, three enforcement options, in increasing strictness:

  annotate   attach [n] to each supported claim; mark the rest
  strip      remove unsupported claims from the answer entirely
  refuse     if any claim is unsupported, return "not in the sources"

Which is right depends on the cost of a wrong answer. A code-documentation assistant can annotate. A system quoting drug dosages should refuse.

5. Report precision and recall separately

Diagram: citation precision compared with citation recall

Citation precision is the share of citations that genuinely support their claim. Citation recall is the share of claims that carry a citation at all.

They fail independently and they trade against each other. A model that cites every sentence with [1] has perfect recall and terrible precision. One that cites only the single most obvious claim has the reverse. A single blended score hides exactly the failure you are looking for, which is why the lab reports both.


An everyday analogy

Diagram: unchecked references compared with verified ones

This is the difference between a student’s essay and a court transcript.

The essay says “the defendant was seen leaving at nine”. The transcript says “the defendant was seen leaving at nine (Witness B, page 14, line 6)”. Both may be accurate. Only one can be checked without re-running the trial.

And the analogy carries the failure mode too: a citation to “the witness statements” — all 200 pages of them — is formally a citation and practically useless. The value is entirely in how narrow the pointer is.


Examples in practice

Claim-Level Grounding Verification Matrix diagram

Splitting an answer into claims

def split_claims(answer: str) -> list[str]:
    sentences = re.split(r"(?<=[.!?])\s+", answer.strip())
    claims: list[str] = []
    for s in sentences:
        parts = s.split(" and ")
        # Only split on "and" when both halves carry their own evidence.
        if len(parts) == 2 and all(re.search(r"\d|[A-Z]{2,}", p) for p in parts):
            claims.extend(p.strip(" .") for p in parts)
        else:
            claims.append(s)
    return [c for c in claims if c]

Scoring a claim against the retrieved sentences

def support_for(claim: str, sentences: list[Sentence]) -> tuple[Sentence, float]:
    claim_tokens = set(re.findall(r"\w+", claim.lower()))
    best, best_score = None, 0.0
    for sent in sentences:
        tokens = set(re.findall(r"\w+", sent.text.lower()))
        lexical = len(claim_tokens & tokens) / max(len(claim_tokens), 1)
        semantic = cosine(embed(claim), sent.vector)
        score = 0.5 * lexical + 0.5 * semantic
        if score > best_score:
            best, best_score = sent, score
    return best, best_score

Enforcement

THRESHOLD = 0.45

def enforce(claims, sentences, mode="annotate"):
    out, unsupported = [], 0
    for claim in claims:
        sent, score = support_for(claim, sentences)
        if score >= THRESHOLD:
            out.append(f"{claim} [{sent.chunk_id}]")
        else:
            unsupported += 1
            if mode == "annotate":
                out.append(f"{claim} [unsupported]")
            elif mode == "refuse":
                return "The sources do not support a complete answer.", unsupported
    return " ".join(out), unsupported

The two metrics, kept apart

precision = correct_citations / max(total_citations, 1)
recall    = cited_claims      / max(total_claims, 1)
# Never average these. A system can be excellent at one and useless at the other.

Implications: security, privacy, performance, scalability, and cost

Diagram: the trust, cost, latency and coverage implications of citation verification

Security. Citations are an exfiltration channel. If a chunk the user is not cleared to see supports a claim, citing it leaks its existence and often its content. Filter the citable set by the user’s permissions, not the indexer’s.

Privacy. A citation is a durable pointer into source material. Stored answer-plus-citation pairs are a second copy of the data, with their own retention and their own deletion obligations — a point Day 328 develops.

Performance. Claim-level verification is claims × sentences similarity computations. For a five-claim answer over twenty sentences that is 100 cheap operations — negligible next to the generation call that produced the answer.

Scalability. Verification cost scales with answer length, not corpus size, which is unusual and welcome. A larger corpus does not make verification slower.

Cost. If verification uses a model rather than arithmetic, it is a second inference per answer. Doing the lexical-plus-embedding version first and escalating only ambiguous claims to a model keeps the cost near zero for the majority.


Alternatives: free, open source, and commercial

ApproachLicenceStrengthWeakness
Prompt-only (“cite your sources”)freeOne line to implementUnenforced; models cite plausibly and wrongly
Post-hoc verification (today’s lab)freeMechanical, testable, model-agnosticThreshold needs tuning per corpus
NLI entailment modelsApache-2.0, freeGenuine entailment, catches paraphraseExtra model to host; slower
RAGAS faithfulnessApache-2.0, freeStandard metric, batteries includedUses an LLM judge; costs per evaluation
Anthropic citations APICommercialProvider-verified spansProvider-specific

Prompt-only is where nearly everyone starts and it is the weakest option on the list, because it produces exactly the decorated citations described above. The distance from “ask nicely” to “check mechanically” is about forty lines.


ConceptQuestion it answersRelationship to citations
FaithfulnessDoes the answer follow from the context?The same property, measured in aggregate
Hallucination detectionDid the model invent something?The complement — an unsupported claim
ProvenanceWhere did this data originally come from?Upstream; about the corpus, not the answer
ExplainabilityWhy did the model produce this?About the model’s reasoning, not its evidence

Faithfulness and citation are the same underlying property at two granularities: faithfulness scores the answer, citation points at the sentence. Tomorrow’s patterns improve what gets retrieved; Day 272 measures whether the answer used it.


When to use it — and when not to

Diagram: when citation verification is warranted

Enforce citations when answers inform decisions with consequences, the domain is regulated, users cannot easily verify claims themselves, or the corpus updates often enough that stale knowledge is a real risk.

Relax them when the task is creative, the user is the domain expert and can spot an error instantly, or latency budget genuinely does not permit the extra pass.

Reconsider the design when citation recall is persistently low. That usually means retrieval is not returning what the answer needs, and the fix is upstream in chunking or ranking rather than in the citation layer. A verification stage that keeps failing is often reporting someone else’s bug.


The AI connection

Knowledge check

quiz:
  question: "What does a grounding check guarantee, and what does it not?"
  options:
    - "That the model used retrieval; not which passage it used"
    - "That the answer is factually true; not that it is well written"
    - "That the source is current; not that it is authoritative"
    - "That the context says this; not that the context is correct"
  answer_index: 3
  explanation: "Grounding is a containment property. A perfectly grounded answer drawn from an out-of-date document is confidently wrong, and no citation machinery detects that."

Hands-on exercise

In this exercise, you will build and evaluate a modular Python RAG pipeline that ingests a sample technical knowledge base, performs hybrid lexical-dense search, constructs a grounded system prompt, and validates answer grounding.

Expected output

When running your test suite, you should observe:

============================= test session starts ==============================
collected 4 items

tests/test_grounded_citations_lib.py::test_document_chunker PASSED              [ 25%]
tests/test_grounded_citations_lib.py::test_sparse_bm25 PASSED                   [ 50%]
tests/test_grounded_citations_lib.py::test_hybrid_ranker PASSED                 [ 75%]
tests/test_grounded_citations_lib.py::test_prompt_synthesizer PASSED            [100%]

============================== 4 passed in 0.04s ===============================

Validate your work

Run the pytest test runner script:

pytest tests/ -v

Troubleshooting

Common mistakes


Practice assignment

Take three answers your Day 268 pipeline produced and verify them by hand.

For each answer, write out every claim on its own line. Beside each, record the sentence from the retrieved context that supports it, or write “unsupported”. Then compute citation precision and recall for the three answers.

Finally, find one claim that is true but unsupported — the model knew it from training rather than from your corpus. Explain why that case is more dangerous than a claim that is simply false, and what your pipeline should do with it.


Extension challenge

Implement citation stability across re-indexing.

Store, with each answer, the chunk id, the source document id, and a hash of the cited sentence. After re-indexing the corpus, check every stored citation: does the chunk id still exist, and does the sentence hash still match?

Report three counts — intact, moved (sentence found under a different chunk id), and lost. Then decide what the system should do about “moved”: silently repoint, or flag for review? Argue your answer using the difference between a citation that is still correct and one that merely still resolves.


Quiz

Q1. What makes a decorated citation more dangerous than no citation at all?

  1. It prevents the answer from being cached by the retrieval layer
  2. It takes longer for the model to generate than an uncited answer
  3. It carries the visual signature of rigour while pointing at the wrong text
  4. It makes the answer fail schema validation before display
Show answer

Answer: C. It carries the visual signature of rigour while pointing at the wrong text

Users trust cited answers more. A citation that resolves but does not support its claim converts that trust into a liability.

Q2. Why does the lesson insist on sentence-level rather than document-level citations?

  1. Most retrieval systems cannot return a whole document
  2. Documents cannot be assigned stable identifiers across re-indexing
  3. Sentence embeddings are cheaper to compute than document embeddings
  4. Verification has to take seconds, or nobody performs it
Show answer

Answer: D. Verification has to take seconds, or nobody performs it

Citing a 300-page document is formally a citation and practically useless. The value of a pointer is entirely in how narrow it is.

Q3. Why are citation precision and recall reported separately rather than averaged?

  1. They fail independently, and a blend hides the failure you are hunting
  2. Precision is measured offline while recall can only be measured live
  3. Averaging them would exceed the range permitted for a percentage
  4. Recall requires a gold answer set and precision does not
Show answer

Answer: A. They fail independently, and a blend hides the failure you are hunting

A model that tags every sentence [1] has perfect recall and terrible precision; one that cites only the obvious claim has the reverse.

Q4. A stored citation still resolves after re-indexing, but chunk boundaries moved. What is the risk?

  1. The answer can no longer be displayed to the user at all
  2. The citation now points at different text than when the answer was written
  3. The retrieval index rejects any query touching that chunk
  4. The citation marker changes from [2] to a different number
Show answer

Answer: B. The citation now points at different text than when the answer was written

A live, resolvable citation can still be wrong. Storing a hash of the cited sentence distinguishes still correct from still resolves.

Q5. What does a grounding check guarantee, and what does it not?

  1. That the source is current; not that it is authoritative
  2. That the answer is factually true; not that it is well written
  3. That the context says this; not that the context is correct
  4. That the model used retrieval; not which passage it used
Show answer

Answer: C. That the context says this; not that the context is correct

Grounding is a containment property. A perfectly grounded answer drawn from an out-of-date document is confidently wrong.

Glossary

Retrieval-Augmented Generation (RAG)
An AI framework combining external knowledge retrieval from databases with generative LLM inference for grounded answers.
Parametric Memory
Knowledge encoded directly into the neural network weights during pre-training and fine-tuning.
Non-Parametric Memory
External knowledge stored in vector stores, databases, and document files accessed dynamically at inference time.
Reciprocal Rank Fusion (RRF)
A rank-based algorithm that aggregates heterogeneous search candidate rankings using reciprocal rank scoring.
Okapi BM25
A non-linear probabilistic ranking function used in sparse keyword search based on term frequency and inverted document frequency.
Cross-Encoder Re-Ranker
A neural model that processes query and document pairs jointly through full cross-attention to compute precise relevance scores.
Bi-Encoder
A neural architecture that encodes queries and documents into separate dense vector embeddings independently.
Inline Citation
A bracketed numerical marker ([1], [2]) linking a generated factual claim directly to a retrieved source document passage.
RAG Triad
An evaluation framework measuring Faithfulness, Answer Relevance, and Context Precision in RAG applications.
Context Precision
The proportion of retrieved context passages that contain ground-truth information relevant to answering the query.

Sources and further reading


Kept in this browser, no account needed. Your progress page turns the whole record into one link you can bookmark or open on another device.