Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Generation and Prompting

Citations and Grounded Answers in RAG

Make RAG answers verifiable: number sources, ask for structured citations, validate them in code, detect unsupported claims and refuse when evidence is missing.

A RAG system that answers without showing its evidence asks users to trust it blindly. Citations fix that in three ways: users can verify an answer in one click, you can debug which chunk drove a claim, and the model itself tends to stay closer to the text when it must point at it. But citations are only valuable if they are correct, and models can invent or misattach them. This lesson shows how to request citations, how to check them in code, and how to design refusals.

What "grounded" means

An answer is grounded when every factual claim is supported by the retrieved context. It is faithful if it does not contradict the context. A model can fail in several ways:

  • Unsupported claim: a plausible statement that appears nowhere in the sources.
  • Wrong attribution: the claim is in the sources, but the citation points to a different chunk.
  • Fabricated citation: a source number that does not exist.
  • Stale or conflicting sources: the answer cites an outdated chunk.

You cannot eliminate these with prompting alone, so the design combines a good prompt, structured output and post-hoc checks in code.

Request structured output

Free text with inline [1] markers is the simplest option. A more robust option is to ask for JSON: an answer split into claims, each with the ids of its supporting sources. Most modern model APIs support a JSON schema or "structured output" mode that guarantees valid JSON, which makes validation straightforward.

JSON
{
  "answer": "Refunds are available within 30 days.",
  "claims": [
    {"text": "Refunds are available within 30 days of purchase.", "sources": [1]}
  ],
  "can_answer": true
}

The can_answer flag gives the model a clean way to refuse, which you can route to a fallback instead of showing an empty answer.

Validate citations in code

Given the answer and the retrieved chunks, check at least two things: (1) every cited id exists, and (2) the cited chunk plausibly supports the claim. The second check can be cheap (word overlap) or strong (a natural-language-inference model or an LLM judge, see generation evaluation).

Python Checking citations against the sources

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "wrong source" case shows low support: the claim exists in the context, but chunk 2 is not where it comes from. A threshold on support (tuned on examples) flags it. Word overlap is crude (it misses paraphrases and accepts shared vocabulary without shared meaning), so treat it as a first filter and use stronger checks for high-stakes use.

The model should cite short ids, never URLs. After validation, your code maps [1] to the title and link stored in the chunk's metadata. That keeps the model from inventing URLs and lets you change URL formats later.

Python Rendering citations as references

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Renumbering in order of first use gives readers a clean list. The example prints the links as text; in a web app they would be anchors, and note that the example URLs use the reserved .test domain so they are safe placeholders.

Quote-level citations

For legal, medical or compliance use, cite the exact passage: ask the model to return a short verbatim quote with each claim, then verify in code that the quote is a substring of the chunk (after whitespace normalization). A verbatim check is strict and cheap, and it is very hard to fool.

Python Verbatim quote verification

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Designing refusals

  • Detect early: if retrieval finds nothing above the threshold, refuse without calling the model.
  • Let the model refuse: the can_answer flag or a fixed phrase makes refusals machine-detectable.
  • Be helpful when refusing: suggest related pages, ask a clarifying question or offer human support.
  • Track the refusal rate. Too high means retrieval or coverage problems; too low may mean the model is guessing.

Never show a citation you have not validated. A confident-looking footnote that points at the wrong page is worse than no footnote: it teaches users to trust false answers.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should the model cite short ids rather than URLs?

    Ids are validated against the retrieved set and resolved to URLs by trusted code.
  2. 2. What is a 'wrong attribution' failure?

    The claim may be supported elsewhere in the context, but the cited source is not where it comes from.
  3. 3. How can you verify a quote-level citation cheaply?

    Verbatim substring checks are strict, fast and hard to fool.
  4. 4. Retrieval returns nothing above the threshold. What should the system do?

    With no evidence, the safe path is an explicit fallback rather than letting the model improvise.

Summary

  • Grounded answers are supported by the retrieved context; citations make that checkable.
  • Ask for structured output with source ids, then validate ids and support in code.
  • Map ids to URLs yourself; renumber for readability.
  • Use verbatim quotes for high-stakes domains.
  • Design refusals deliberately and monitor the refusal rate.

Next: Conversational RAG and Query Rewriting