The idea
A RAG answer without sources asks users for blind trust. With citations, each claim links to the chunk, page or URL it came from, and users can verify in one click.
Implementation: give each retrieved chunk an ID in the prompt, ask the model to cite IDs after each claim, then map IDs back to titles, pages and links in your UI. Some APIs offer native citation features that return exact quoted spans.
Validate citations: the cited ID must exist in what you retrieved, and ideally the claim should be supported by that chunk (a faithfulness check from the Evals area).
Citations turn RAG from "trust me" into "check me", which is what businesses and regulated users need.
Words to know
- AttributionLinking a generated claim to its source.
Try it without code
Ask for sources
- Paste three short labelled passages [1] [2] [3] into a chat app.
- Ask a question and require [n] after every sentence.
- Check each citation by hand.
Use it in code
Numbered sources in the prompt, cited IDs in the answer, validated afterwards.
import re, anthropic
client = anthropic.Anthropic()
def answer_with_citations(question, chunks):
sources = "\n".join(f"[{i}] ({c['doc']} p.{c['page']}) {c['text']}" for i, c in enumerate(chunks, 1))
msg = client.messages.create(model="claude-sonnet-5-5", max_tokens=500, temperature=0,
messages=[{"role": "user", "content":
f"Sources:\n{sources}\n\nAnswer using only the sources. Put [n] after every sentence. "
f"If they do not contain the answer, say so.\nQuestion: {question}"}])
text = msg.content[0].text
cited = {int(n) for n in re.findall(r"\[(\d+)\]", text)}
bad = [n for n in cited if not 1 <= n <= len(chunks)]
return text, [chunks[n - 1] for n in sorted(cited) if n not in bad], badShow citations in your PDF chat as clickable "doc, page" chips and log any invalid citation IDs.
Common mistakes
- Citing documents that were not retrieved.
- One citation at the end covering a whole paragraph of mixed claims.