AI engineering / Core / 02.16
Grounded generation
How to make a model answer only from what you retrieved, cite its sources, say "I don't know", and how to check the answer before a user sees it.
13 min readFree
From pieces to an answer
The last lesson, Retrieval Process, ended with a promise: next, generation, turning retrieved pieces into a grounded answer. Here it is.
Think of an open-book exam. Retrieval is the part where you flip to the right pages. Generation is the part where you write your answer. A good student writes only what the pages say, points to the page, and writes "not in the book" when it is not there. A model needs the same rules, and we have to give them in writing.
By the end you will know how to write a prompt that keeps a model to its sources, what format makes the answer checkable, and how a few lines of plain code can catch a bad answer before a user sees it. You will also read two small programs.
Key words
- Generation
- The step where a model writes the final answer. In RAG it comes after retrieval.
- Grounded
- An answer is grounded when every claim in it can be found in the sources you gave the model.
- Chunk
- One retrieved piece of text. The Chunking lesson showed how documents are cut into these.
- Citation
- A pointer from a claim to the source it came from, such as a chunk id.
- Hallucination
- A made-up answer that sounds sure of itself. The model invents a fact instead of finding it.
- Faithfulness
- How well the answer sticks to the sources. This is different from whether it answers the question.
RAG stands for retrieval-augmented generation. It lets a model refer to documents you choose before it answers. The Wikipedia page on it adds a warning we will keep in mind: it quotes Ars Technica saying RAG "is not a direct solution because the LLM can still hallucinate around the source material in its response." So a good prompt is not enough. We will also check the answer.
The grounded prompt
A grounded prompt has two parts. The rules say how to behave. The sources are the pieces you retrieved, each with an id, a short label like c1. The ids matter: they are what the model will point back to.
- 1
Put the sources in, with ids
Write each chunk on its own line, with its id in square brackets in front. The model can now name a source in two characters.
- 2
Say "answer only from these"
Anthropic's guide to reducing hallucinations suggests telling the model to use only the documents you provide and not its general knowledge.
- 3
Ask for citations
Tell it to give the id of every source it used. An answer with no ids cannot be checked, so it is not allowed.
- 4
Give it permission to say "I don't know"
The same guide says that explicitly letting the model admit uncertainty can reduce false information. Give it an exact sentence to use, so your code can spot it.
Here is the prompt builder for a shop's return policy. It is plain Python and calls no model, so it runs anywhere for free. It prints the two messages a chat model would receive: the rules, then the sources and the question.
import json
chunks = [
{"id": "c1", "text": "Items can be returned within 30 days of delivery if they are unworn and have the tags attached."},
{"id": "c2", "text": "Refunds go back to the original payment method within 5 to 7 business days after we receive the item."},
{"id": "c3", "text": "Sale items can be exchanged but not refunded."},
]
RULES = (
"Answer ONLY from the sources below. "
"Cite the id of every source you use. "
'If the sources do not contain the answer, reply exactly: "I don\'t know."\n'
'Reply as JSON: {"answer": "...", "citations": [{"id": "...", "quote": "..."}]}'
)
def build_messages(question, chunks):
sources = "\n".join(f"[{c['id']}] {c['text']}" for c in chunks)
return [
{"role": "system", "content": RULES},
{"role": "user", "content": f"Sources:\n{sources}\n\nQuestion: {question}"},
]
messages = build_messages("How long do I have to return a jacket?", chunks)
print(messages[0]["content"])
print("---")
print(messages[1]["content"])Output
Answer ONLY from the sources below. Cite the id of every source you use. If the sources do not contain the answer, reply exactly: "I don't know."
Reply as JSON: {"answer": "...", "citations": [{"id": "...", "quote": "..."}]}
---
Sources:
[c1] Items can be returned within 30 days of delivery if they are unworn and have the tags attached.
[c2] Refunds go back to the original payment method within 5 to 7 business days after we receive the item.
[c3] Sale items can be exchanged but not refunded.
Question: How long do I have to return a jacket?Tip
Put the question after the sources, and keep the "I don't know" sentence fixed and exact, so your code can spot it later. A real model can still ignore your rules, which is why the checks below exist.
Retrieval does not always find something useful. If it finds nothing, do not call the model at all. Show "I don't know" yourself. It is free, instant and always right.
An answer you can check
A paragraph of text is hard to check. A structured answer is easy. The Structured Output lesson covered asking a model for JSON, a plain text format that code can read. Here we ask for an answer plus a list of citations, and each citation has two fields.
answer
The words the user will read. Short and direct.
id
Which source the claim came from, for example c1. One id per citation.
quote
A few words copied exactly from that source. This is the proof.
Anthropic's guide describes the same idea. It suggests asking for word-for-word quotes first, then building the answer from them. It also describes a second pass where the model finds a supporting quote for each claim and, if it cannot, retracts the claim. Our version gives your own code the quotes, so you can run the second pass yourself, with no extra model call.
Checking the answer
Think of a teacher marking homework with the textbook open. For every note in the margin, "see page 12", the teacher turns to page 12. We do the same, three ways.
Does every id exist?
The model may cite c9 when you only gave it c1, c2 and c3. That is a made-up source. Check that every cited id is on the list of ids you retrieved.
Is the quote really there?
Even with a real id, the quote might not be in that chunk. Check that the quote appears inside the text of the chunk it names. Ignoring upper and lower case is fine. Changing numbers is not: in our example a quote that says 60 days instead of 30 must fail.
This check is cheap and strict. It only proves the quote is real, not that the answer means what the quote says. That second job belongs to the scores in the next section.
What if nothing was retrieved?
If the list of chunks is empty, any answer the model gives is made up. So the rule is simple: no chunks, no answer, show "I don't know."
Running the checker
The program below is the whole loop. The chunks are the same three. The model is a stand-in, named fake_model. It ignores the prompt and returns three fixed answers, one after another: a good one, one citing an id that does not exist, and one with a quote that is not in any source.
A stand-in is not a real model. We use it so the code runs free, with no key and no network. A real model is slower, costs money for every token, can give a different answer each time you ask, and can fail or refuse. Your checker has to handle all of that. Here we can see it work on answers we control.
import json
chunks = [
{"id": "c1", "text": "Items can be returned within 30 days of delivery if they are unworn and have the tags attached."},
{"id": "c2", "text": "Refunds go back to the original payment method within 5 to 7 business days after we receive the item."},
{"id": "c3", "text": "Sale items can be exchanged but not refunded."},
]
# STAND-IN, not a real model: it ignores the prompt and returns fixed text.
ANSWERS = [
{"answer": "You have 30 days from delivery.",
"citations": [{"id": "c1", "quote": "within 30 days of delivery"}]},
{"answer": "You have 30 days, and refunds take a week.",
"citations": [{"id": "c9", "quote": "within 30 days of delivery"}]},
{"answer": "You have 60 days from delivery.",
"citations": [{"id": "c1", "quote": "within 60 days of delivery"}]},
]
def fake_model(messages, n):
return json.dumps(ANSWERS[n])
def check(raw, chunks):
"""Return a list of problems. An empty list means the answer passed."""
by_id = {c["id"]: c["text"] for c in chunks}
try:
data = json.loads(raw)
except ValueError:
return ["not valid JSON"]
if not data.get("citations"):
return ["no citations"]
problems = []
for cite in data["citations"]:
if cite["id"] not in by_id:
problems.append(f"cited id {cite['id']} was never retrieved")
elif cite["quote"].lower() not in by_id[cite["id"]].lower():
problems.append(f"quote not found in {cite['id']}: '{cite['quote']}'")
return problems
def decide(chunks, problems, tries_left):
if not chunks:
return "I DON'T KNOW"
if not problems:
return "SHOW"
return "RETRY" if tries_left > 0 else "I DON'T KNOW"
print("nothing retrieved:", decide([], [], 1), "(model never called)")
for n, tries_left in enumerate([1, 1, 0]):
problems = check(fake_model(None, n), chunks)
print(f"answer {n + 1}: {decide(chunks, problems, tries_left)} {problems}")Output
nothing retrieved: I DON'T KNOW (model never called) answer 1: SHOW [] answer 2: RETRY ['cited id c9 was never retrieved'] answer 3: I DON'T KNOW ["quote not found in c1: 'within 60 days of delivery'"]
Read the three lines under the first one. Answer 1 cites c1 and the quote is really in c1, so it is shown. Answer 2 cites c9, which was never retrieved. It has one retry left, so the decision is RETRY: ask the model again. Answer 3 has a real id but a quote that says 60 days when c1 says 30. It has no retries left, so the user gets "I don't know" instead of a wrong number. Showing nothing is better than showing something false.
- 1
Parse the reply
If it is not valid JSON, or it has no citations, it fails straight away.
- 2
Check each citation
The id must exist, and the quote must appear in that chunk.
- 3
Decide
No problems means show. Problems and a retry left means ask again. Problems and no retry left means "I don't know."
Quick check
The model cites chunk c1 and gives a quote. The quote is not in c1's text. What should the checker do?
Tip
Keep the retry count small, one or two. Every retry on a real model costs time and money. Two tries is plenty before you give up and say "I don't know".
Faithful is not the same as relevant
The checker above looks at one thing: do the claims come from the sources? There is a second, separate question: does the answer actually answer what was asked? Mix them up and you will fix the wrong problem.
The RAGAS paper (RAGAS stands for Retrieval Augmented Generation Assessment) lists the dimensions to look at. Its abstract names three: how well the retrieval finds relevant and focused passages, "the ability of the LLM to exploit such passages in a faithful way, or the quality of the generation itself." It also says its metrics work "without having to rely on ground truth human annotations", meaning you do not need a person to write the right answer for each test.
The RAGAS docs define the two scores like this.
Faithfulness
How factually consistent the answer is with the retrieved context, from 0 to 1. An answer is faithful if all its claims can be supported by that context.
Answer relevancy
How relevant the answer is to the user's question, from 0 to 1. The docs say it focuses on how well the answer matches the question, "without evaluating factual accuracy".
Both together
A good answer needs both. High faithfulness with low relevancy is a true answer to a different question. High relevancy with low faithfulness is the right question with an invented answer.
The docs give faithfulness a formula: count the claims in the answer, check which ones the context supports, and divide.
In words: the share of the answer's claims that the sources back up. With made-up numbers, if an answer makes 4 claims and the sources back up 3, the score is 3 divided by 4, which is 0.75.
An example of the other score. The user asks how long they have to return a jacket. The model answers "Refunds take 5 to 7 business days." That sentence is in c2, so it is faithful. It does not answer the question, so it is not relevant. Our citation checks would pass it. Only a relevance check, or a person, would catch it.
Quick check
An answer is true to the sources but answers a different question. Which score is low?
Tip
The RAGAS scores are computed with models and embeddings, so they are rough measures. Treat them as a way to compare two versions of your pipeline, not as a promise about any single answer. Our code checks are exact but narrow. Use both.
Showing sources to the user
Citations are not only for your code. The Wikipedia page on RAG notes that it allows models to include sources in their responses, "so users can verify the cited sources." A source the user can open turns "trust me" into "check for yourself". That is good design, not just good safety.
Small numbered markers
Put a tiny marker like [1] after the claim. It stays out of the way of the text and tells the reader where the proof is.
Open the source on tap
Tapping a marker should show the quote in its chunk, and a link to the full document. Highlight the quoted words.
An honest empty state
When the answer is "I don't know", say so plainly and offer a next step: search the documents yourself, or ask a person.
Notice that the ids a model writes are not what users see. Your code maps c1 to a title and a link. If the checker has already proven the id exists, that mapping cannot fail.
Tip
Even with all of this, Anthropic's guide says these techniques "don't eliminate them entirely", so always validate critical information, especially for high-stakes decisions. For those, put a person in the loop.
What to take away
Ground it
Sources with ids, "answer only from these", cite ids, and an exact "I don't know" sentence.
Make it checkable
Ask for an answer plus citations with ids and word-for-word quotes.
Check it
Ids exist, quotes appear, nothing retrieved means no answer. Then retry once or say "I don't know".
Next: build RAG by hand, every piece in plain code.
Sources
Last updated on 16 November 2026
Learn with me
Every lesson is free. I add new ones as I learn. Have a question, or want help with your own work? Book a short call.