AlgoMaster Logo

Exercise: RAG with Citations and Grounding

3 min readUpdated June 22, 2026
Listen to this chapter
Unlock Audio

Exercise 1: Answer with Inline Citations

Given a question and a set of numbered context chunks, have the model answer the question and cite the chunk numbers it used inline, so every claim can be traced back to its source.

Citations are what make a RAG answer auditable. Without them, a user has no way to tell which parts of an answer came from the documents and which the model produced on its own. Numbering the chunks and asking the model to reference those numbers turns the answer into something a reader can verify against the original sources.

Requirements

  • Number the context chunks [1], [2], [3], and so on.
  • Instruct the model to answer using only the chunks and to cite the relevant chunk number in brackets after each claim.
  • Print the answer with its inline citations.
main.py
Loading...

Expected Output

The answer reads naturally and carries bracketed citations that point at the chunks each claim came from.

Solution

main.py
Loading...

Numbering the chunks gives the model a stable label to point at, and the system prompt makes the citation a required part of the format rather than an afterthought. The result is a grounded answer where each claim names its source, so a reader can jump straight to chunk [2] to confirm the recommendation rather than taking the model's word for it.

Exercise 2: Extract and Validate Citations

Generating citations is only useful if you can check them. After the model answers with bracketed chunk numbers, parse those citations out and verify each one points at a real chunk. This is the programmatic grounding check that catches a model citing a chunk that does not exist.

Requirements

  • Build numbered context and ask the model to answer with inline [n] citations.
  • Parse the cited numbers from the answer with a regex.
  • Print the answer, the set of cited chunk numbers, and whether every citation is a valid chunk index.
main.py
Loading...

Expected Output

The answer carries citations, and the validation confirms each cited number refers to a real chunk.

Solution

main.py
Loading...

The regex \[(\d+)\] pulls every bracketed number out of the answer, and checking each against the valid range turns citations from decoration into something you can audit automatically. A citation pointing at a nonexistent chunk is a clear hallucination signal worth flagging or rejecting. This parse-and-verify step is what lets you trust citations at scale instead of spot-checking them by hand.