Cite the Passages Behind an Answer
An answer built from retrieved passages is only as useful as your ability to check it. A citation is a small marker in the answer that says which passage a statement came from, so a reader can open that passage and compare. For that to work, each passage needs a label the model can write, the model needs an instruction to use it, and your code must confirm that every label it writes names a passage that was really sent. Even then, a checked citation only tells the reader where to look.
Why an answer should name its sources
A language model writes fluent text whether or not that text is right. When it answers from passages placed in its prompt, it usually stays close to them, but it can still mix in something it learned in training, merge two passages into a claim neither makes, or get a number wrong. An answer on its own gives the reader no way to tell these cases apart: they can only trust it or ignore it.
A citation changes that. If the sentence "Report a leak within 24 hours" ends with a marker that points to a section of a tenant handbook, the reader can open that section and read the rule for themselves. The answer becomes a claim with an address. Research on answering with citations names exactly this goal: making the model's output verifiable, because model outputs are prone to statements that sound right and are not.
- A statement in the answer
- Its citation [2]
- The passage document and heading
- The reader compares
- A statement to Its citation
- Its citation to The passage: points to
- The passage to The reader compares: opened by
Citations also help the person who maintains the system. When an answer is wrong, the citation shows which passage the model leaned on, which narrows the search for the cause: a passage that says something else points at the model, and a cited passage that is itself out of date points at the documents.
Give every passage a short label
A model can only point at something it can name. If the passages arrive in the prompt as one block of text, the model has no handle for any single one of them. So each passage gets a short label when the prompt is built, and the program keeps a record of what each label stands for.
The simplest label is a number in square brackets, [1], [2], [3], in the order the passages were retrieved. A number is short, so the model can write it after every sentence without cluttering the answer, and a program can find it again in the text by its regular shape. Beside the number, the prompt can also show where the passage came from, so the model sees the source too. Suppose a search for "How do I report a leak?" returned three passages from a tenant handbook:
| Label | Document | Heading |
|---|---|---|
| [1] | repairs.md | Reporting a repair |
| [2] | repairs.md | Water leaks |
| [3] | contacts.md | Out-of-hours emergencies |
The label is local to one prompt: [2] means "the second passage sent with this question", not a permanent name for that section. What makes it useful is the mapping the program keeps from each label back to the document and heading. Building the labels and the mapping in the same loop guarantees the two never drift apart:
passages = [
{"doc": "repairs.md", "heading": "Reporting a repair", "text": "..."},
{"doc": "repairs.md", "heading": "Water leaks", "text": "..."},
{"doc": "contacts.md", "heading": "Out-of-hours emergencies", "text": "..."},
]
sources = {} # label -> passage
blocks = []
for number, passage in enumerate(passages, start=1):
label = str(number)
sources[label] = passage
blocks.append(
f"[{label}] ({passage['doc']}, {passage['heading']})\n{passage['text']}"
)
context = "\n\n".join(blocks)The labels are kept as strings, "1", "2", because that is what the program will later read back out of the answer's text. The context string goes into the prompt where the passages are marked off as context. The only new thing is the label in front of each passage.
Ask the model to cite each statement
Labels in the prompt do nothing until the instructions say how to use them. The instruction has to be specific: which marker to write, where to put it, and what to do when a sentence draws on more than one passage. A vague request such as "mention your sources" invites a list of document names at the end, which tells the reader nothing about which sentence came from where.
Answer the question using only the numbered passages below.
End every sentence with the number of the passage it comes from,
in square brackets, like [2].
If a sentence uses two passages, cite both, like [1][3].
Cite only the numbers shown below.
[1] (repairs.md, Reporting a repair)
...
[2] (repairs.md, Water leaks)
...
[3] (contacts.md, Out-of-hours emergencies)
...
Question: How do I report a leak?Three choices in these instructions matter. Citing every sentence ties each claim to a passage, instead of leaving some unsupported sentences mixed among cited ones. Showing the exact format, [2] and [1][3], makes the markers regular enough for code to find. And saying "only the numbers shown" names the valid labels outright. A published benchmark for answers with citations, ALCE, asks for citations in the same way. Its instructions use bracketed numbers, say "Always cite for any factual claim", and allow at most three citations per sentence, because more usually do not help.
An answer that follows these instructions reads like this, each statement carrying the label of its passage:
- Report leaks in the tenant portal ... [1][2]
- Turn off the valve under the sink ... [2]
- At night, call the emergency line ... [3]
- [1] repairs.md Reporting a repair
- [2] repairs.md Water leaks
- [3] contacts.md Out-of-hours emergencies
- Report leaks in the tenant portal to [1] repairs.md
- Report leaks in the tenant portal to [2] repairs.md
- Turn off the valve under the sink to [2] repairs.md
- At night, call the emergency line to [3] contacts.md
True or false?
Once the instructions say to cite only the numbers shown, every label in the answer will match a passage that was sent.
False. An instruction makes valid labels more likely, not certain. The labels are ordinary text the model writes, and nothing in a plain model call checks them, so the program has to.
A list of document names at the end of an answer tells the reader less than a label after each sentence.
True. A list says which documents were involved, but not which statement rests on which one, so the reader cannot check a single claim without reading everything.
Check the citations in code
The model's citations arrive as characters in its reply, exactly like the rest of the answer. A model can write [4] when only three passages were sent, copy a number it saw in a passage's own text, or leave a sentence uncited. Each of these looks just as convincing to a reader as a correct citation, and a citation to a passage that was never sent points nowhere: the reader has nothing to open.
So the program reads the labels back out of the answer and compares them with the labels it actually sent. Two tools from Python's standard library do the work.
The first is re.findall, from the re module for regular expressions (patterns that describe text). It returns every match of a pattern in a string, in order. When the pattern has one group in parentheses, it returns only the text inside that group, so the brackets are left out.
The second is a set. Putting the labels in a set removes repeats, and it turns the comparison into a single subtraction: the labels cited minus the labels sent are the ones that match nothing.
import re
cited = set(re.findall(r"\[([0-9]+)\]", answer))
unknown = cited.difference(sources)
if not cited:
print("Rejected: the answer cites no passage.")
elif unknown:
print("Rejected: the answer cites passages that were not sent:", sorted(unknown, key=int))
else:
print(answer)
print()
print("Sources:")
for label in sorted(cited, key=int):
passage = sources[label]
print(f"[{label}] {passage['doc']}, {passage['heading']}")A few details in this code are easy to get wrong:
- The group
([0-9]+)makesre.findallreturn'1'and'2'rather than'[1]'and'[2]'. These are strings, which is why the labels insourcesare strings too. Comparing them with the integers 1 and 2 would find no match at all. - The pattern uses
[0-9]instead of\d. In a Python string pattern,\dalso matches digits from other scripts, and those are not labels. differenceaccepts the dictionary directly, because looping over a dictionary gives its keys.
Rejecting means the answer is not shown as if it were backed by its sources: the program can print a warning, ask the model again, or decline. An answer with only valid labels is printed with its sources underneath. Each label is resolved to the document and heading it stands for, which is the address the reader needs to open it. For the leak answer above, with labels 1, 2 and 3 cited, the sources print like this:
Sources:
[1] repairs.md, Reporting a repair
[2] repairs.md, Water leaks
[3] contacts.md, Out-of-hours emergenciesOnly the passages actually cited are listed, not every passage that was sent. A passage the model received but did not use is not a source of the answer, and listing it would send the reader to text that supports nothing they read.
True or false?
`re.findall(r"\[([0-9]+)\]", "See [2] and [3].")` returns `[2, 3]`, a list of integers.
False. It returns `['2', '3']`. With one group, `re.findall` returns the text of that group, and the text of a match is always a string.
An answer that cites `[5]` after five passages were sent passes this check even if passage 5 says nothing about the statement.
True. The check only asks whether the label matches a passage that was sent. Whether the passage supports the statement is a different question, which the code does not answer.
What a valid citation proves
A citation that passes the check proves one thing: the statement claims to come from a passage the model really received. It does not prove that the passage says what the statement says. The model may have cited the right passage and misread it, attached a label to a sentence it wrote from its own training, or cited a passage that is only about the same topic. Behind every citation sit three different questions, and code that checks labels answers only the first:
| Question | How it is answered |
|---|---|
| Was the cited passage sent with the question? | Code: compare the label with the labels sent |
| Does the passage say what the statement says? | A person reads the passage beside the statement |
| Did the model actually write the statement from that passage? | Not visible in the answer at all |
The gap between the first question and the second is large in practice. A 2023 audit of four generative search engines, which answer web questions with citations, found that only about three quarters of their citations supported the sentence they were attached to:
The third question is harder still. Later research on answers with citations found that a model can write a statement from what it already believed and then attach a citation that happens to fit, so even a citation that does support its sentence may not be where the sentence came from. Asking for citations reduces made-up answers, but it does not end them.
So the check in code and the reader's own reading do different jobs. The code removes citations that point nowhere, so every label the reader sees leads to a real passage. The reader then opens that passage and decides whether it says what the answer claims. A citation is what makes that second step possible; it is never a reason to skip it.
True or false?
If every label in an answer matches a passage that was sent, every statement in the answer is supported by its sources.
False. A valid label shows only where a statement claims to come from. The passage may say something different, so the reader still compares the two.
A citation is useful even though it does not prove the statement, because it tells the reader exactly which passage to read.
True. Without it the reader would have to search every document; with it they open one passage and compare it with one sentence.