Area 02 // RAGLesson 41 / 14013 min read
Citations
Citations link each claim in an answer to the exact source passage that supports it, so users can check the answer and your system can automatically catch claims that have no real support.
Think of it like this
Analogy
A good student essay has footnotes. Each fact points to the book and page it came from. A teacher can check any claim in seconds, and a made-up "fact" with no footnote stands out immediately.
Citations are footnotes for AI answers.
The problem it solves
Even with retrieval, a model can mix facts from different sources, add something from its own memory, or invent details. Users can't tell which sentences are backed by your documents and which aren't, and in regulated areas (legal, medical, finance, HR) "the AI said so" isn't acceptable.
Without citations you also can't automate quality checks: there's nothing to verify a claim against.
How it works, step by step
- Number the sources in the prompt:
[1] (policy.pdf p.3) Annual leave is 25 days…,[2] …. Include title and page so the model and the UI can refer to them. - Instruct the model: "Answer only from the sources. After every claim, add the source number in square brackets, like [2]. If the sources don't answer the question, say so."
- Or use a provider citation feature: as of November 2026, Anthropic's API can return structured citations when documents are passed with citations enabled, giving the exact cited text and its location (character range or page), so you don't have to parse brackets.
- Parse the answer: split into sentences or claims and extract their citation IDs.
- Validate:
- Every claim has at least one citation.
- Every cited ID exists.
- The cited source actually supports the claim: exact or fuzzy quote matching for quoted text, or an LLM judge for paraphrases (faithfulness).
- Handle failures: regenerate, remove the unsupported sentence, or show it with a warning, depending on the risk.
- Show citations in the UI: clickable numbers with the source title, link, page and highlighted passage.
See it in code
- Needs
- Nothing: plain Python 3.
Parsing citations per sentence and flagging claims that cite nothing or cite a source that doesn't exist.
import re
sources = {
1: "Annual leave is 25 days per year, plus public holidays.",
2: "Unused leave can be carried over, up to 5 days.",
3: "Sick leave requires a doctor's note after 7 days.",
}
# The prompt numbers each source and asks for [n] after every claim.
prompt_sources = "\n".join(f"[{n}] {text}" for n, text in sources.items())
model_answer = ("You get 25 days of annual leave [1]. You can carry over up to 5 unused days [2]. "
"Leave can be cashed out at any time [4].")
def check_citations(answer: str, valid_ids: set[int]):
sentences = [s.strip() for s in re.split(r"(?<=\.)\s+", answer) if s.strip()]
for s in sentences:
ids = [int(n) for n in re.findall(r"\[(\d+)\]", s)]
status = "ok" if ids and all(i in valid_ids for i in ids) else "UNSUPPORTED"
print(f"{status:11} {s}")
check_citations(model_answer, set(sources))ok You get 25 days of annual leave [1].
ok You can carry over up to 5 unused days [2].
UNSUPPORTED Leave can be cashed out at any time [4].The third claim cites a source that doesn't exist: a fabricated policy. A second check (does source [2] really mention "5 days"?) catches real IDs attached to unsupported claims; see the quote check in hallucinations.
A real example
Example
A clinical guidelines assistant that doctors trust
A hospital built an assistant over its clinical guidelines. Doctors refused to use the first version: answers sounded right, but there was no way to check them quickly during a shift.
The second version showed every sentence with a superscript number; tapping it opened the guideline section, highlighted the exact passage, and showed the guideline's revision date. Behind the scenes, sentences without a valid, supporting citation were removed before display, and if more than one was removed the assistant said "I couldn't find a clear answer in the guidelines". Adoption went from a handful of curious users to most of the department.
The easy way
Start here
Number your retrieved chunks, add "cite sources like [1] after each claim" to the prompt, and run the 10-line check above on every answer. Show the cited chunks under the answer.
When you grow: the production setup
Production systems use provider-native citations where available, verify support with quote matching plus an LLM judge on a sample, log citation coverage and validity as metrics, render citations with deep links to the exact page or paragraph, and include unsupported-claim rate in RAG evals.
Trade-offs
What you gain
- Users can verify answers in seconds, which builds trust.
- Unsupported claims can be detected and removed automatically.
- Makes debugging easy: you see exactly which source drove each claim.
What it costs you
- Longer outputs and slightly more prompt complexity.
- Verifying that a source truly supports a paraphrased claim needs an extra model call.
- Models sometimes cite the right ID for the wrong claim.
When not to use it
- Creative or conversational features where there are no sources to cite.
Common mistakes
- Trusting citations without checking that the IDs exist and support the claims.
- Not numbering sources clearly, so the model invents its own reference style.
- Showing only "[2]" without a link or the passage, so users can't verify.
- Dropping page and section metadata at ingestion, making precise citations impossible.
Interview questions
Try answering out loud first, then open the model answer.
How do you implement citations in a RAG system?
Model answer
Number retrieved chunks with source metadata in the prompt and require bracketed citations per claim, or use provider-native citation features that return cited spans. Parse claims and IDs, validate that IDs exist and that sources support the claims (quote matching or an NLI/LLM judge), handle failures by regenerating or removing claims, and render citations linked to the original document location.
How do you measure citation quality?
Model answer
Citation coverage (share of claims with citations), citation validity (cited IDs exist), citation precision (cited source supports the claim) and recall (claims supported by some retrieved source are cited), using an eval set with human labels and a calibrated LLM judge, plus production sampling.
A model cites a real source that doesn't support its claim. How do you catch it?
Model answer
Check entailment between the claim and the cited text: for quotes, exact or fuzzy string matching; for paraphrases, an LLM judge or NLI model asked whether the source supports the claim, calibrated on human-labelled examples. Flag or remove failing claims and track the rate over time.
Quiz
Pick an answer to see whether it’s right and why.
0 of 5 answered
Key terms
- Citation
- A reference in an answer that points to the exact source passage supporting a claim.
- Grounding
- Giving the model trusted source material and instructing it to answer only from it, so answers are based on facts rather than guesses.
- Faithfulness
- How well every claim in an answer is supported by the provided sources. Unsupported claims are hallucinations.
- Hallucination
- A confident model output that is false, made up or not supported by the given sources.
Before and after
Progress is saved in this browser only.