Back to the map

Module 07 · The RAG pipeline · Lesson 5 of 6

Prompt assembly and citations

Building the generation prompt: binding instructions, numbered sources, chunk order and lost-in-the-middle, structured citations validated in code, a post-check that spans support claims, abstention, history and the token budget.

CoreMust-knowMid 12 minPro Updated Nov 2026

Why it matters

Retrieval found the passages. The prompt decides whether the model uses them, invents around them or quotes the wrong one. Assembly is where grounding is enforced or lost: the instruction that binds the answer to the sources, the order of the chunks, the ids that make a citation checkable, the history that keeps a follow-up coherent. A citation is the user's only evidence that the answer came from somewhere, and an interviewer will ask how you know a citation is real.

The idea

Binding instructions

The system instruction states the contract: answer only from the sources; cite the source id after every factual claim; if the sources do not contain the answer, say so. Each clause does work: "only from the sources" suppresses recall from weights, "cite every claim" makes the output checkable, "say so" gives the model a way out other than invention. Keep it short and put it first.

Labelled sources

Give each chunk a stable id, a source title with section path, and the text. The id is what the model cites; the title and path are what the user sees. Short numbers in brackets work.

SYSTEM: Answer only from the sources. After each claim write the
source id in brackets. If the sources do not contain the answer,
reply: The sources do not say.

[1] returns-policy > Window
Items may be returned within 30 days of delivery for a full refund.

[2] returns-policy > Sale items
Sale items may be returned within 14 days; refunds are store credit.

HISTORY: user asked about the standard window; assistant: 30 days [1].
USER: And for sale items?

Ordering and lost in the middle

Models attend more reliably to the start and end of a long context than to the middle. With five chunks this barely matters; with twenty it does. Put the highest-ranked chunk first and the second-highest last, or order by relevance and keep k small enough that nothing is buried.

Asking for citations in a structured form

Free-text citations ("according to the returns policy") cannot be checked. Ask for the id inline after each claim, or for a JSON answer with a claims array where each claim carries source_ids. Either way, validate in code that every cited id exists in the prompt: a citation to a source that was not provided is a hallucination caught by a set lookup.

Rendering

Turn ids into links to the cited section, or into footnotes with the title and a short quoted span, so the user can land on the passage. For high-stakes domains, show the quoted span beside the claim so verification needs no click.

The post-check

Existence is not support. A second cheap model call, or an entailment model, takes each claim and its cited chunk and answers one question: does this passage support this claim? This is the faithfulness metric run online, per answer, and it catches the most dangerous failure: a real citation under a wrong claim.

"The sources do not say"

Make abstention a first-class output: instruct it, test it with questions whose answers are absent from the corpus, and render it well, with the nearest sources offered so the user can judge. Monitor the abstention rate: too high and retrieval is failing or the instruction is too strict; too low and the model is answering from memory.

History and budget

A follow-up needs the recent turns, but history competes with sources for tokens. A workable split for a 6,000-token input budget: 600 for instructions, 3,000 for six or seven chunks, 1,500 for recent turns with older ones summarised, and the question. Count with the model's tokeniser, and drop the lowest-ranked chunk before the instruction or the question.

promptInstructions600 tok[1] source400[2] source400[3] source400History, 2 turns500Question60≈ 2,400 tokens in, best source firstModelAnswer[3]one id per claimmust supportpost-check: does source 3support the claim?
Instructions, numbered sources, recent history and the question are stacked into one prompt; the answer carries an id per claim and a post-check confirms the cited span supports it.
 Inline citationsFootnote idsQuoted spans
VerifiabilityGood; one id per claim, checked by set lookupGood; same check, claims grouped at the endBest; the evidence is on the screen
Token costLowest; a few tokens per claimLow; ids plus a listHighest; the span is repeated in the output
User trustReads naturally; a click to verifyClean prose; verification one step awayImmediate; users see the words behind the claim
Use whenChat answers, streamingReports and summariesLegal, medical, finance
Prompt assembly and citations · Backplane