Generate answers grounded in sources
Build the prompt, order the context, demand citations, and let the model say “I don’t know”.
- Write a prompt that separates instructions, sources and question
- Order context so the model actually uses it
- Check that every claim is cited and supported
Retrieval found good chunks - now the model has to use them faithfully. A solid RAG prompt has clear parts:
- Instructions: answer only from the sources; cite the source ID after each claim; if the sources don’t answer the question, say so.
- Sources: each chunk with its ID, clearly delimited (for example in
<source id="returns#2">…</source>tags), and treated as data - never as instructions to follow. - The question, at the end.
Abstaining is a feature. “I couldn’t find that in our documentation” is far better than a confident guess, and it tells you which documents are missing.
Where you put evidence matters too. The “Lost in the Middle” study (Liu et al., TACL 2024) found that models use information at the start and end of a long context far better than information in the middle - accuracy dropped by more than 30% in some tests. So:
- send fewer, better chunks (that’s what reranking is for), and
- place the strongest evidence at the beginning and end.
Try it
Up to 15 XPFind the unsupported claims
Read the sources, then check the generated answer. Click every claim that isn’t supported by the source it cites - or that has no source at all.
Click every suspicious section. There are 3.
sources = {"returns#2": "Opened electronics: 14 days, 10% fee."}
blocks = "\n".join(f'<source id="{key}">{text}</source>' for key, text in sources.items())
print(blocks)<source id="returns#2">Opened electronics: 14 days, 10% fee.</source>
Key takeaways
Separate instructions, delimited sources and the question; ask for citations and allow “I don’t know”.
Put the strongest evidence at the start and end; send fewer, better chunks.
A citation must actually support its claim - check them.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Python
Write Python in the editor and run it against sample inputs. Python runs locally in your browser using a WebAssembly runtime.
Put the best evidence at the edges
Each input line lists chunk IDs in relevance order (best first). Reorder them so the best chunks sit at the start and end: 1st goes first, 2nd goes last, 3rd second, 4th second-to-last, and so on. Print each reordered list.
- Five and four chunks
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Check an answer’s citations
Lines before --- are sources: id|text. Lines after are answer sentences, each possibly ending with citations like [s1]. For each sentence (with the citations removed), print:
UNCITED: sentenceif it has no citation;BAD SOURCE id: sentenceif it cites an unknown source;UNSUPPORTED n: sentenceif a number in it appears in none of its cited sources (first such number);- otherwise
ok: sentence.
- Five sentences
Python runs in a sandboxed browser worker with a 60 second time limit. Its runtime loads from the Pyodide CDN; your code stays in this browser.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…