Citations and Source Attribution

Learn how to track sources through the pipeline, generate citations, build evidence-based answers with provenance, and detect unsupported claims.

20 minIntermediateCode Examples
The Core Idea: An answer without a source asks the reader to trust the system. An answer with citations lets the reader check it. In RAG you already know which chunks the model was given, so you can show where each claim came from. Good attribution also exposes failures: a claim with no matching source is a warning sign you can detect automatically.

1. Beginner: Why Citations Matter

Citations are not decoration. They serve four jobs.
Trust
Readers can verify an answer instead of believing it.
Debugging
A wrong answer with a visible source shows whether retrieval or generation failed.
Compliance
Regulated fields such as legal, medical and finance often require a traceable basis for every statement.
Learning
Users discover the underlying documents, not only the summary.

2. Beginner: Source Tracking Through the Pipeline

A citation is only possible if the source information survives every step. That is why metadata matters from the first ingestion lesson. Each chunk should carry these fields from the moment it is created until the answer is displayed.
FieldExampleUsed to
Chunk idc-2041Link a claim to the exact passage
Document id and titleEmployee HandbookShow a readable source name
Locationpage 4, section 2Let the reader find the passage
URL or pathA link to the file or pageOpen the source
Version and datev3, 2025-06-10Show how current the source is
Do not lose it: any step that rewrites, merges or compresses chunks must keep the ids of the chunks it came from.

3. Intermediate: Generating Citations

The usual method gives each context passage a short number in the prompt and asks the model to attach the numbers of the passages it used to each statement. After generation, the system replaces the numbers with titles and links.
Label. Add numbers such as 1 and 2 to the passages in the prompt.
Instruct. Ask for the supporting numbers after every claim.
Validate. Reject any number that does not exist in the context, because models sometimes invent citations.
Render. Convert numbers to readable source names and links.
A stricter form asks the model to quote the exact supporting sentence. Quotes are easy to verify mechanically by checking that the quoted text really appears in the cited chunk.

4. Intermediate: Evidence-Based Answers and Provenance

An evidence-based answer makes only claims that some retrieved passage supports. Provenance is the recorded history behind the answer: which question was asked, which chunks were retrieved and with what scores, which prompt was built, and which answer came out.
Storing that trail for every answer costs little and pays back quickly. It lets you reproduce a bad answer, audit sensitive uses, and learn from user feedback. It also means that when a document is corrected you can find every answer that relied on the old version.
Show uncertainty honestly: if only some claims in an answer are supported, show the supported ones with their sources, and say clearly that the rest could not be verified.

5. Advanced: Preventing Unsupported Claims

Asking for citations does not guarantee faithful answers. A model can attach a real source number to a statement the source never made. So add a verification step that checks every sentence against the passages it cites.
Verification methodHow it worksTrade-off
Word overlapShare of the claim content words found in the cited passageCheap, but fooled by paraphrases and negations
Entailment modelA trained model judges whether the passage supports the claimMore accurate, extra model to run
Language model judgeA model is asked to verify each claim against its sourceFlexible, costs a call per answer
Citation quality can be measured. Citation precision is the share of citations that really support their claim, and citation recall is the share of claims that have a supporting citation:
citation precision=supported citationsall citationscitation recall=claims with a supporting citationall claims\text{citation precision} = \dfrac{\text{supported citations}}{\text{all citations}} \qquad \text{citation recall} = \dfrac{\text{claims with a supporting citation}}{\text{all claims}}
When a claim fails verification, the options are to remove it, to flag it, or to regenerate the answer with a stricter instruction. In high-stakes settings, prefer removing or abstaining.

6. Visual Explanation: From Chunk to Checked Claim

Panel A follows source information through the pipeline. Panel B shows the verification results from the code below. Panel C is an explorer of citation failures.
Panel A · The Provenance ChainIds travel from chunk to answer
Chunk with id→Prompt label [1]→Claim cites [1]→Verify the claim→Show source link
Panel B · Verification ResultsFrom the code in section 7
ClaimCitationResult
You receive 20 vacation days per year[1]supported, 0.83
Unused days carry over for 3 months[2]supported, 1.00
Managers must approve every request in writing[1]unsupported, 0.00
Remote work is allowed on Fridaysnoneuncited
Panel C · Citation FailuresClick a failure to open it
Click to see why citations can still mislead▼
A citation shows where to look, not that the statement is true. The source itself may be wrong or outdated, and the model may have misread it, so verification and source quality both matter.
How to read it: Panel A shows how a citation is made, Panel B shows how it is checked, and Panel C lists what can go wrong anyway.

7. Python Implementation: Citing Sources and Verifying Claims

The program holds two numbered sources with their labels. A scripted answer plays the part of a model output: each sentence lists the source numbers it relies on, and one sentence has none. A simple verifier measures the share of the content words of each claim that appear in the cited source, and labels the claim as supported, unsupported or uncited. The program then prints a source list for the reader.
verify_citations.pyPython 3.10+ · Standard library only
sources = [    ("1", "handbook.pdf, page 4", "Employees receive 20 vacation days per year."),    ("2", "faq.md, section 2", "Unused vacation days carry over for 3 months."),] stop_words = set(["the", "a", "an", "of", "for", "per", "in", "is", "are", "to", "they", "can", "after", "your"]) def content_words(text):    cleaned = "".join(ch if ch.isalnum() else " " for ch in text.lower())    return set(w for w in cleaned.split() if w not in stop_words) def support(claim, source_text):    claim_words = content_words(claim)    return len(claim_words & content_words(source_text)) / len(claim_words) # A pretend model answer: every sentence ends with the source numbers it relies onanswer = [    ("You receive 20 vacation days per year.", ["1"]),    ("Unused days carry over for 3 months.", ["2"]),    ("Managers must approve every request in writing.", ["1"]),    ("Remote work is allowed on Fridays.", []),] lookup = dict((number, text) for number, label, text in sources)labels = dict((number, label) for number, label, text in sources) for claim, cited in answer:    if not cited:        print("UNCITED     :", claim)        continue    best = max(support(claim, lookup[n]) for n in cited)    status = "SUPPORTED  " if best >= 0.6 else "UNSUPPORTED"    print(status, ":", claim, "[" + ",".join(cited) + "]", "support", round(best, 2)) print("")print("Sources:")for number, label, text in sources:    print("  [" + number + "]", label)
Expected output
SUPPORTED   : You receive 20 vacation days per year. [1] support 0.83SUPPORTED   : Unused days carry over for 3 months. [2] support 1.0UNSUPPORTED : Managers must approve every request in writing. [1] support 0.0UNCITED     : Remote work is allowed on Fridays. Sources:  [1] handbook.pdf, page 4  [2] faq.md, section 2
Two claims pass. The claim about managers approving requests cites source 1 but shares no content words with it, so it is marked unsupported. The remote work claim has no citation at all. Word overlap is only a rough check, since it can be fooled by paraphrases and by negations, which is why production systems use entailment models or a language model judge. The structure, however, is the same: cite, verify, and then show, fix or remove.

Knowledge Check

Q1. Why must a system validate citation numbers returned by a model?
Q2. A claim cites source 1, but source 1 does not say it. What kind of error is this?
Q3. What does citation precision measure?

Key Points

✓
Citations build trust, help debugging, support compliance and let users reach the underlying documents.
✓
Source ids, titles, locations, links and versions must survive every pipeline step.
✓
Label passages with numbers in the prompt, ask for numbers per claim, validate them, then render readable sources.
✓
Provenance records the question, retrieved chunks, prompt and answer for audit and reproduction.
✓
A citation shows where to look, not that the statement is true, so verify claims against the cited text.
✓
Verification can use word overlap, entailment models or a language model judge, with growing accuracy and cost.
✓
Measure citation precision and recall, and remove or flag unsupported claims.

The Big Picture

Retrieval finds the evidence, context construction presents it, and attribution proves where each statement came from. Together they turn a fluent answer into an accountable one, and complete the Query and Context phase of your RAG system.
Track sources → Cite them → Verify claims → Show or remove → Keep the provenance
Up Next: RAG Evolution: Naive, Advanced and Modular. You will see how RAG systems evolve from simple pipelines to advanced and modular designs, where each approach fails, and when to use which.