Papa Programmer
Lesson 12 of 15

Citations and honest refusals

8 min read

Optional

What grounded means

An answer is GroundingMaking an answer rest on supplied sources, so each claim can be traced to a passage the system retrieved. A grounded system also says so when the sources do not hold the answer. when every claim in it can be traced to a passage the system retrieved. RAG makes grounding possible, but it does not guarantee it. A model that receives a good passage can still add a detail from memory, or answer a question the passage never addressed.

Two habits turn possible into likely.

  1. Cite each claim. The model marks every statement with the number of the source it came from.
  2. Admit gaps. When no source covers the question, the model says so.

A CitationA marker in an answer, such as [2], that points to the source passage behind a claim. It lets a reader and a test check the claim against the source. does two things. It lets the reader check the answer in a moment, which builds trust where it is earned. And it gives your tests something to inspect.

Asking for citations

The instruction in the prompt does most of the work. Ask for the format you will check, and say what to do when sources are missing.

text
Answer using only the sources below.
After each claim, add the source number in square brackets, like [2].
If the sources do not answer the question, reply exactly: I could not find that in the documents.

Asking for the exact refusal sentence has a second use. Your program can compare the reply with that sentence and show the user a "no result" screen, with a link to contact support, instead of showing an answer.

Checking citations in code

A model can cite a source that does not exist, or cite a real source that does not say what the answer claims. A few lines of code catch both of the most common faults. The program below checks that every cited number is a real source, and that a short quote the model also returned appears in that source word for word.

python
import re

sources = {
	1: "Return an unused bike within 30 days for a refund.",
	2: "Sale items can be returned within 14 days.",
}

def check(answer, quote):
	problems = []
	cited = [int(number) for number in re.findall(r"\[(\d+)\]", answer)]
	if not cited:
		problems.append("no citation")
	for number in cited:
		if number not in sources:
			problems.append(f"[{number}] is not a source")
		elif quote not in sources[number]:
			problems.append(f"the quote is not in [{number}]")
	return problems or ["ok"]

quote = "within 14 days"
for answer in (
	"Sale items can be returned within 14 days [2].",
	"Sale items can be returned within 14 days [1].",
	"Sale items can be returned within 14 days [3].",
	"Sale items can be returned within 14 days.",
):
	print(check(answer, quote), "<-", answer)

It prints:

text
['ok'] <- Sale items can be returned within 14 days [2].
['the quote is not in [1]'] <- Sale items can be returned within 14 days [1].
['[3] is not a source'] <- Sale items can be returned within 14 days [3].
['no citation'] <- Sale items can be returned within 14 days.

The four answers say the same thing and carry four different citations. Only the first points at a passage that contains the quote. The second cites a real source that does not support the claim. The third cites a source number that was never sent. The fourth cites nothing.

Asking the model for an exact quote is a cheap and strong check. If the quote is not in the source, the claim is not supported, and you can reject the answer or ask the model to try again. The check does not prove the answer is right, because the quote could still be taken out of context, but it removes the most obvious errors.

Saying "I don't know"

An honest "I could not find that" is a feature. A confident wrong answer about a refund policy costs a customer money. A clear gap costs one more click.

You can decide to refuse before the model runs. If retrieval returns nothing, or the best score is low, the sources probably do not hold the answer, and sending them to the model invites a guess.

python
def should_answer(scores, threshold=0.6):
	return bool(scores) and max(scores) >= threshold

for scores in ([0.82, 0.40], [0.31, 0.20], []):
	print(scores, "->", "answer from sources" if should_answer(scores) else "say: I could not find that")

It prints:

text
[0.82, 0.4] -> answer from sources
[0.31, 0.2] -> say: I could not find that
[] -> say: I could not find that

The threshold is not a universal number. Scores from different embedding models and rerankers sit on different scales, so choose the cut-off by testing it on questions you know are answerable and on questions you know are not, as the Lesson Measuring answer faithfulness shows. Too high a threshold makes the system refuse answerable questions, and too low a threshold lets guesses through.

What a helpful refusal says

A bare "I don't know" leaves the user stuck. A better refusal says what was searched and offers a next step: "I could not find that in the returns policy or the shipping guide. You can ask support at help@example.test."

GuessBare refusalHelpful refusal
What the user getsA confident answer that may be wrongNothing to act onWhat was checked and where to go next
RiskHighLowLow
Trust over timeFalls when a guess failsFalls, because the tool seems uselessGrows
Three replies to a question the documents do not cover
Cite every claimA source number lets the reader, and your tests, check the answer.
Verify in codeCheck that each cited source exists and contains the quote.
Refuse when evidence is thinA clear gap is cheaper than a confident wrong answer.

Your reading position is saved

0%

0 of 15 complete

Contents

Learning tools