Courses / RAG Systems / Module 9
Module 9: Grounding, citations and the 'not found' path
6 h · theory 2 · hands-on 2 · project 1 · revision 1
You will be able to
- Make the model answer only from retrieved context
- Attach and verify inline citations
- Detect unanswerable questions and say 'not found'
- Measure citation precision
Capstone increment: Answers with verified citations and an explicit 'not found' response, tested on unanswerable questions.
Job-description skills: RAG pipelines, LLM prompting · Lab tools: openai, llama-index
1. Grounding is a contract
A grounded assistant makes three promises: it answers only from the retrieved sources, it shows which source supports each claim, and it says “not found” when the sources don’t cover the question. Each promise is enforced in code and measured, not left to a polite instruction in the prompt.
The pipeline below gates on retrieval strength, drafts an answer that cites numbered chunks, verifies each cited claim, and takes the honest path when either check fails.
2. Prompt and schema for grounded answers
Number the retrieved chunks and include their document and page: “[3] (travel-policy.pdf, p. 1) Hotel bills above…”. Put the context before the question, tell the model to cite [n] after every sentence, and ask for structured output: a status (answered or not_found) and a list of sentences, each with its citation ids. A quote-first prompt, where the model extracts the supporting quotes before answering, makes unsupported sentences rarer and easier to check.
Several providers now offer citation features that return the exact cited spans, such as Anthropic’s Citations API, and frameworks offer citation query engines. They save work, but you still verify and you still measure.
3. Verify before you show
Run cheap checks first: every cited id exists and was retrieved; every number, date and name in a sentence appears in its cited chunk. Then run a support check per sentence with a small natural-language-inference model or a judge prompt that asks only “does [n] support this sentence: yes or no?”.
Decide in advance what happens to a sentence that fails: drop it, mark it as unverified, or refuse the whole answer. For HR, legal and medical assistants, refusing is usually right. Build source links from chunk metadata in code, never from URLs the model wrote; a model-written link is an injection and phishing risk.
4. The “not found” path
Retrieval scores alone make a poor “not found” detector: questions the documents don’t answer often retrieve near-identical text (a policy for employees when the question is about contractors). Combine a retrieval gate with the claim check, then tune both on a set of questions you know the documents can’t answer.
Make the refusal useful: say what you searched, what the closest document covers, and who can answer (“HR policies don’t cover contractors; ask HR Ops”). Measure two rates and show both on your dashboard: unsupported answers and needless refusals.
Correct answers 16 · correct “not found” 3 · unsupported answers 5 · needless refusals 0
| How many sick days do I get? | 0.95 | answered |
| What is the daily allowance in Mumbai? | 0.93 | answered |
| Who approves business class? | 0.91 | answered |
| Notice period after confirmation? | 0.90 | answered |
| Can I carry over annual leave? | 0.88 | answered |
| How long is maternity leave? | 0.87 | answered |
| Deadline for expense claims? | 0.85 | answered |
| Is alcohol reimbursed? | 0.84 | answered |
| Remote work days per week? | 0.83 | answered |
| I lost a taxi receipt, what now? | 0.79 | answered |
| When is my full and final paid? | 0.76 | answered |
| Do I need the VPN at a café? | 0.72 | answered |
| Can part-timers take annual leave? | 0.69 | answered |
| How early do I book a flight to Delhi? | 0.66 | answered |
| We adopted a baby. What leave do I get? | 0.64 | answered |
| Can I swap leave for my last month? | 0.55 | answered |
| What is the sick leave policy for interns? | 0.81 | answered, but nothing supports it |
| What is the per diem in London? | 0.77 | answered, but nothing supports it |
| Can I fly business on a 5-hour red-eye? | 0.74 | answered, but nothing supports it |
| How much parental leave do contractors get? | 0.62 | answered, but nothing supports it |
| Is there a gym reimbursement? | 0.58 | answered, but nothing supports it |
| What is the bonus payout date? | 0.47 | said “not found” |
| How do I change my bank account for salary? | 0.33 | said “not found” |
| What's the office Wi-Fi password? | 0.21 | said “not found” |
Guided experiments
- Without the claim check, find the lowest threshold with zero unsupported answers. How many good questions does it refuse? Would users trust this assistant, or give up on it?
- Why do “sick leave for interns” and “per diem in London” score so high when the documents don’t answer them? What does that tell you about using retrieval scores as a confidence measure?
- Turn on the claim check and lower the threshold until something slips through. Which question is it, and why does a near-identical policy for employees fool the checker?
- Pick your production setting. Which error is worse for an HR assistant: a confident wrong answer or a “not found” that sends someone to HR?
5. Measure citation quality
Citation precision is the share of citations that support their sentence. Citation recall is the share of sentences needing support that cite a supporting chunk. Track the unsupported-sentence rate alongside them. Label 50 answers by hand to calibrate any automatic checker you use, exactly as in Module 8.
Add these metrics to the CI gate. A prompt change that makes answers longer often lowers citation precision without touching retrieval metrics; without the measurement you would never see it.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 9.1: Grounded output
Change the capstone generator to return the GroundedAnswer schema with numbered, page-referenced chunks and a quote-first prompt.
Expected: Every answer sentence carries at least one citation; not_found returns no sentences.
Lab 9.2: Verifier and metrics
Add id, number and support checks. Label 40 answers by hand and compute citation precision, citation recall and unsupported-sentence rate before and after the verifier.
Expected: A before-and-after table and the policy you chose for flagged sentences.
Lab 9.3: Not found, tested (capstone increment)
Tune the retrieval gate and verifier on 15 unanswerable and 45 answerable questions, return citations as page links built from metadata, and add the unanswerable set to CI.
Expected: /ask returns not_found with a useful next step for the unanswerable set; the CI check fails if any is answered.
Go deeper
Videos
- RAG Agents in Prod: 10 Lessons We Learned — Douwe Kiela, creator of RAG · AI Engineer · ~17 min. Ten production lessons from the creator of RAG, many about grounding, evaluation and admitting what the system doesn’t know.
- Stop RAG Hallucinations: Citations + a Verifier That Fails Closed · SystemDR - Scalable System Design · ~3 min. A short, concrete pattern: citations plus a verifier that fails closed.
Further reading
- Paper Enabling Large Language Models to Generate Text with Citations (ALCE) · Gao et al., 2023. Benchmarks and metrics for citation recall and precision.
- Docs Citations · Anthropic docs. API-level citations that point at exact passages of the documents you supplied.
- Paper FActScore: Fine-grained Atomic Evaluation of Factual Precision · Min et al., 2023. A method to check each claim against sources, the core of a verification step.
GitHub repositories
- run-llama/llama_index: CitationQueryEngine: how sources are split into numbered chunks and cited in the answer.
- anthropics/claude-cookbooks: The citations examples: passing documents and reading back the cited spans.
- Azure-Samples/azure-search-openai-demo: A production-style app that renders citations as clickable source links; read its prompt and UI code.
- infiniflow/ragflow: How an open-source RAG engine shows grounded citations with document previews.
Checkpoint quiz
Interview questions
- How do you make a RAG system cite its sources reliably?
- How would you detect that a question can’t be answered from the documents?
- Design a verifier for answer citations. What do you check cheaply, and what needs a model?
- What do you show the user when part of an answer is unsupported?
- How do you measure and monitor citation quality in production?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).