Part 3 · Writing the answer · Chapter 12
From documents to an answer
In one sentence: the generation step hands a language model the question and the retrieved documents with clear rules, gets back sentences that each point to their sources, and then checks that output with ordinary code before anyone reads it.
Why you should care
Retrieval decides what the model can know. Generation decides what the reader sees. A good set of documents can still produce a poor answer: one that covers a single part of the question, cites a document that does not exist, or mixes in facts from the model's memory. Most of the work in this step is not the model call. It is the instructions before the call and the checks after it.
Words you need
- Prompt
- Everything sent to the model: instructions, the question, and the evidence.
- System prompt
- The part of the prompt that sets the model's role and standing rules.
- Evidence block
- The part of the prompt that holds the retrieved documents, each with an identifier.
- Sentence-level citation
- Each sentence of the answer lists the documents that support it.
- Structured output
- An answer in a fixed machine-readable form, such as JSON, instead of free text.
- Temperature
- A setting that controls how much chance enters the model's word choices. Zero means as little as possible.
- Post-processing
- Code that cleans, checks and reshapes the model's output.
- Validation
- Checking that the output obeys the required format and rules.
Core The idea
What goes in
A generation prompt has four parts.
- Role and goal. Who the model is writing for and what a good answer looks like.
- Rules. Use only the evidence. Cite every sentence. Do not invent sources.
- The question and the evidence. Each document is labelled with its identifier, so the model can point to it.
- The output format. Exactly what shape the answer must have.
What comes out
For a chat window, free text with footnotes is fine. For a system that must be checked, ask for structure. Here is the shape we asked for: a list of sentences, each with the identifiers of its sources.
{"answer": [
{"text": "Inspectors closed the bridge after finding cracks in two steel beams.",
"cited_docids": ["doc_17"]},
{"text": "A repair of this kind was estimated to add about twenty years of service.",
"cited_docids": ["doc_04", "doc_31"]}
]}
One fact per sentence makes each citation easy to check: a reader, or a program, can open the cited document and look for that one fact.
What happens after
Never pass model output straight to the reader. Check it first, with plain code:
- Is it valid JSON in the right shape?
- Does every cited identifier exist in the evidence that was sent?
- Does every sentence have at least one citation?
- Is the answer within the length limit?
- Is it in the right language?
When a check fails you can repair the output with code, ask the model again, or drop the bad sentence. Decide the rule in advance and log every case.
The TREC RAG 2026 answer format
The track fixes the output shape, which makes answers from all teams comparable. An answer is a list of sentences. Each sentence carries zero to three citations, written as positions in a list of references. The whole answer may have up to 1,024 words.
Watch and read
- VideoBeginner's Guide to RAG with Prof. Tom Yeh AI by Hand. RAG worked through step by step, including how the prompt is put together. Link checked 15 Nov 2026
- VideoMaster Explainable RAG: Citations, Sources & Page-Level Answers LearningHub. Showing sources with each answer. Link checked 15 Nov 2026
- ArticleRetrieval Augmented Generation (RAG) Prompt Engineering Guide. A short overview with a notebook. Read 15 Nov 2026
- PaperEnabling Large Language Models to Generate Text with Citations Gao, Yen, Yu and Chen, 2023. The ALCE benchmark. It found that even strong systems often fail to back their answers fully. Checked 15 Nov 2026
- SiteTREC RAG 2026 track page The official answer format. Read 14 Nov 2026
For the two videos we confirmed that the link opens and the title and channel match. We have not watched them through.
How we did it at TREC 2026
All six of our RAG generation runs use one prompt and one pipeline. They differ only in the model and in how much text of each document the model saw (Chapters 13 and 15).
- 1EvidenceThe 100 documents from BM25 and RRF (Chapter 8), written into the prompt as lines of identifier and text.Ch 13
- 2GenerateOne call per narrative. Temperature 0. JSON output. The prompt asks for 45 to 70 sentences.this chapter
- 3AssembleKeep at most three citations per sentence. Build the reference list in the order documents are first cited.this chapter
- 4FitBring the answer under the word limit.Ch 14
- 5RepairRemove document identifiers that leaked into the prose. Drop sentences without a citation.this chapter
- 6ValidateCheck every file against the track format before upload.Ch 28
Our prompt makes one choice that shaped everything: it is written for coverage. It tells the model that a narrative usually bundles several questions and that the answer is scored by how many key facts it covers. It asks for every part to be answered, with several specific sentences each, and one focused fact per sentence. In the prompt's own words, “breadth of aspect coverage matters as much as depth”.
The code also enforces two rules without trusting the model. A cited identifier is kept only if it is one of the documents that were sent. And after repair, every sentence in every submitted file has at least one citation and no more than three.
We checked the six submitted files against these rules after the fact. In all six: no empty answers, no sentence without a citation, no citation pointing outside the reference list, no reference that is never cited, and no answer above 1,000 words.
What surprised us
- Answers with no usable citations. On four narratives, two models wrote first drafts in which not one citation was valid. We generated those four again.
- Identifiers in the prose. Some sentences carried the document identifier inside the text, as if it were a word. The repair step strips them.
- The wrong language. One model answered one narrative entirely in Chinese: all 81 sentences. The narrative was in English. We chose to keep text in other scripts, so the answer was submitted as written. That narrative was not among those evaluated, so it did not affect our scores. A language check would have been one line of code.
Try it
- Take the ten-line RAG lab from Chapter 2. Change the prompt so the model must return JSON in the shape shown above.
- Write a function that checks an answer: valid JSON, every cited identifier exists, every sentence is cited. Run it on ten answers.
- Remove the instruction “use only the evidence” and ask about something the documents do not cover. What does the model do?
- Count the sentences that state more than one fact. Tighten the prompt and count again.
Check yourself
- Name the four parts of a generation prompt.
- Why ask for one fact per sentence?
- Why is a structured output easier to trust than free text?
- Give three checks you can run on an answer without a language model.
- The model cites a document identifier that was never in the prompt. What should the pipeline do?
Answers
- Role and goal, rules, the question with the evidence, and the output format.
- Each citation then supports one clear claim, which a person or a program can check.
- Code can parse it and test each rule. Free text has to be read.
- Any three of: valid format, cited identifiers exist, every sentence cited, within the length limit, right language.
- Drop that citation. If the sentence then has none, drop or regenerate the sentence, and log it.
Go deeper
- Cite while writing, or after. We asked the model to cite as it wrote. The other way is to write first and attach citations afterwards by searching for support for each sentence. The second way can cite better and may keep unsupported sentences alive.
- Constrained decoding. Some services can force the output to match a JSON schema, token by token. That removes format errors. It does not remove wrong citations.
- Plan, then write. For long answers, a first call can list the parts of the question and a second can write each part. Our prompt folds this into one call and forbids planning sentences in the output.
- Temperature zero is not a promise. Hosted models can still give different outputs on repeated calls. Save every raw output. We could rebuild each submitted file from the saved generations, and that is how the facts in this chapter were checked.
- Prompt injection. A retrieved document can contain text that looks like an instruction. The model should treat evidence as data. Chapter 42 returns to this.
- Project idea. Compare citing while writing with citing afterwards on twenty questions. Measure the share of sentences whose citation truly supports them (Chapter 19).