Topic 1 of 5 · 5 min read
Prompting for answers grounded in sources
Goal: Write a grounded-answer prompt or API call that returns claims tied to source ids and quotes.
A grounded answer is one where every claim points to a source id and, for the important ones, a verbatim quote, so the model has to find evidence before it is allowed to assert anything. Retrieval only puts text in front of the model. Nothing makes it use that text and nothing else.
Check yourself · 1 of 3
Why does a grounded-answer prompt give each retrieved chunk an id?
Why it matters
Models fill gaps from training data and sound just as sure when they do. If your support bot is asked about a refund window and the retrieved chunk says 30 days, a model may still answer "14 days" because that is common elsewhere. Without ids and quotes, nobody can tell which answer came from your documents and which from memory.
How to do it
- Wrap each chunk in a tagged block with a stable id. The id is what the model cites and what your code resolves later.
- Say what the sources are for. Tell the model to answer only from them, and what to do when they do not contain the answer.
- Ask for the quote first. Having the model copy the supporting sentence before it writes the claim makes it commit to evidence. It also gives you something to check with a plain string search.
- Use structured output. Ask for JSON with
claims, each holdingtext,source_idandquote. Your code can then verify that the quote really occurs in that chunk. - Use the provider's citation feature when there is one. The Claude API can take documents with citations enabled and return cited spans, with character, page or block locations, produced by the API rather than typed by the model. That removes one way to invent a quote. Check your provider's docs for the equivalent.
Before and after
Weak:
Here are some documents. Answer the question: How long do customers have to return an item?
Better:
<sources>
<source id="policy-v3#returns">Items may be returned within 30 days of delivery...</source>
<source id="faq#shipping">Standard delivery takes 3 to 5 working days...</source>
</sources>
<rules>
Answer only from the sources. For each claim give the source id and the exact quote.
If the sources do not contain the answer, reply {"status":"not_found"}.
</rules>
<question>How long do customers have to return an item?</question>
The second version can be audited: the claim, the id and the quote travel together.
What goes wrong
- Fabricated quotes. Models sometimes paraphrase and present it as a quote. Check each quote is a substring of the cited chunk, ignoring whitespace.
- Citing the wrong id. The quote exists, but in another chunk. Resolve the id and search in that chunk only.
- Evidence buried in the middle. Long contexts are used unevenly; passages in the middle are used less than those at the start or end. Keep the chunk count small and put the best ones at the edges.
- Ids that change per request. If chunk numbers are reassigned on every call, logs and saved answers can no longer be traced. Use document ids and chunk positions that stay fixed.
Examples
A claims schema your code can check
{
"status": "answered",
"claims": [
{
"text": "Returns are accepted within 30 days of delivery.",
"source_id": "policy-v3#returns",
"quote": "Items may be returned within 30 days of delivery"
}
]
}
A verifier loads policy-v3#returns, normalises whitespace and confirms the quote is present. A failure means the claim is rejected or regenerated, not shown.
Native citations in the API call
response = client.messages.create(
model=MODEL,
max_tokens=1024,
messages=[{
"role": "user",
"content": [
{"type": "document",
"source": {"type": "text", "media_type": "text/plain", "data": policy_text},
"title": "Returns policy v3",
"citations": {"enabled": True}},
{"type": "text", "text": "How long do customers have to return an item?"},
],
}],
)
The response text blocks carry citation entries with the cited text and its location in the document, so the quote is not typed by the model.
Key points
- Wrap each retrieved chunk in a tagged block with a stable id that your code can resolve later.
- Tell the model to answer only from the sources and give it an explicit not-found outcome.
- Ask for the supporting quote before the claim, then verify the quote is a substring of the cited chunk.
- Prefer structured output or a provider citation feature so claims, ids and quotes are machine-checkable.
- Keep the chunk count modest, because evidence in the middle of a long context is used less reliably.
Common mistakes
- Pasting chunks into the prompt without ids, so no claim can be traced to a source.
- Telling the model to "use the documents" without saying what to do when they lack the answer.
- Trusting a quote because it looks exact instead of searching for it in the cited chunk.
- Renumbering chunks on every request, which breaks logs and saved answers.
Check yourself · 2 of 3
A model's answer cites a quote that sounds right but you cannot tell if it is real. What is the cheapest reliable check?
Check yourself · 3 of 3
Your API returns cited spans with character locations taken from the document you supplied. What does this remove compared with asking the model to type quotes?
Practice items, not counted towards your mastery.
Start here
- Citations (Claude API docs) (opens in a new tab)DocsFree
How to pass documents with citations enabled and read back cited text with character, page or block locations.
- Enabling Large Language Models to Generate Text with Citations (ALCE) (opens in a new tab)PaperFree
A benchmark that scores answers on fluency, correctness and citation quality, and shows where models still fail.
- Lost in the Middle: How Language Models Use Long Contexts (opens in a new tab)PaperFree
Shows models use evidence at the start and end of a long context better than evidence in the middle.