Sub topic 14
Generating grounded answers with citations
This chapter assumes you can write basic Python, have prompted an LLM before, and have already built a retriever that returns the top-k passages for a question. Here you turn those passages into an answer that the reader can check.
What "grounded" means
A grounded answer makes claims that the retrieved passages support. The model may still use its own language skills to combine and paraphrase. It should not add facts that are missing from the context. Citations make this checkable, because each claim points to the passage it came from.
Grounding is a request to the model, not a guarantee. The model can still ignore the instruction, so you also verify the output.
Prompt rules that produce grounding
The prompt from the previous chapter carries three rules:
- Answer only from the numbered context.
- Cite passages with markers such as
[1]. - Say "I don't know" when the context lacks the answer.
def build_prompt(question, passages):
context = "\n\n".join(f"[{i}] {p}" for i, p in enumerate(passages, start=1))
return (
"Answer using only the context below. "
"Cite passages like [1]. "
"If the context does not contain the answer, say you don't know.\n\n"
f"Context:\n{context}\n\n"
f"Question: {question}\nAnswer:"
)The numbering matters. The model can only cite what is labeled, and you can map each number back to a source document and chunk in your own records.
Checking the citations
Do not trust the markers blindly. A model can cite [4] when you sent three passages, or omit citations entirely. This function extracts the markers and flags the bad ones.
import re
def check_citations(answer, num_passages):
cited = {int(n) for n in re.findall(r"\[(\d+)\]", answer)}
invalid = {n for n in cited if not 1 <= n <= num_passages}
return {
"cited": sorted(cited),
"invalid": sorted(invalid),
"has_citations": bool(cited),
}
answer = "Refunds take 5 days [1]. Shipping is free over $50 [4]."
print(check_citations(answer, num_passages=3))This prints {'cited': [1, 4], 'invalid': [4], 'has_citations': True}. Passage 4 does not exist, so you can reject the answer or retry.
A valid marker only proves the passage exists. It does not prove the passage supports the claim. Checking support needs a second step, such as asking a model to judge whether each sentence follows from its cited passage. The evaluation chapter covers this.
The full flow
Common problems
- Refusal too eager. The model says "I don't know" although the answer is present. Check that retrieval returned the right chunk before changing the prompt.
- Answer from memory. The model answers correctly but without citations, using its training data. This hides stale knowledge, so treat uncited answers as suspect.
- Lost in the middle. Models often use passages at the start and end of a long context better than those in the middle. Keep k small and put the best passages first.
- Temperature. Use a low temperature, such as 0, for factual answers. Higher values increase paraphrase drift.
Exercise
Your model returns "The warranty lasts two years [2]" and you sent three passages. The check reports no invalid citations. Why might the answer still be wrong, and what would you add?
Show answer
A valid marker only shows that passage 2 exists. Passage 2 might not mention the warranty length, or might say something different. Add a support check, for example a second model call or entailment check that tests whether the cited passage backs the claim, and refuse or retry when it does not.