Skip to content
AgenticAIWiki
  1. Learn
  2. Part 1
  3. 7. Hallucination and grounding
  4. 7.2

Lesson 7.2 · 2 of 4 · 4 min read · Reviewed 24 Nov 2026

Grounding with documents and citations

By the end, you can

  • Ground answers in supplied documents.
  • Ask for quotes and citations that can be checked.

Grounding pattern

<contract> ... </contract>

1. Find the clauses that answer the question. Copy them exactly into <quotes>.
2. Answer using only those quotes. Cite clause numbers.
3. If no clause answers it, say so.

Question: Can either party end the agreement early?

Extracting quotes first makes the model commit to specific text, and makes the answer easy to check.

Citations

Approach How
Ids in the prompt Tag each document or chunk with an id; ask for [id] after each claim
Claude’s citations feature Pass documents as content blocks; responses point to exact passages
Quotes Ask for the supporting sentence for each claim

Checking citations in code

  • Do cited ids exist in what you supplied?
  • Does the cited passage contain the key terms of the claim?
  • For high stakes, a second model call checks each claim against its cited passage (chapter 26).

Retrieval is grounding too

RAGRetrieval-augmented generation (RAG): Retrieving relevant documents at request time and adding them to the prompt so the answer is grounded in them. (Part 4) is groundingGrounding: Basing a model’s answer on supplied, verifiable sources rather than on what it learned in training. at scale: find the right passages, then apply the same quote-and-cite pattern.

On other providers

Other APIs return citationsCitations: References in an answer that point to the exact source passages supporting each claim. in their own shape. OpenAI’s web and file search tools attach annotations (URL or file citations with character offsets) to the output text. Gemini’s Google Search grounding returns grounding metadata that maps spans of the answer to source chunksChunking: Splitting documents into smaller passages before embedding and indexing them.. The design is the same everywhere: keep source ids with each passage, and render citations from the structured data rather than from text the model typed.

Key points

  • Grounding means answering from provided sources rather than from memory.
  • Ask the model to quote the relevant passage first, then answer from the quote.
  • Citations let users and code check each claim against its source.
Hands-on labRuns in the Playground

Check citations against the supplied sources

Verify in code that every cited id exists and that the cited passage actually supports the claim.

You will have: A citation checker that labels each claim supported, weak or invalid, with a summary line.

Steps

  1. Run the starter: every claim is marked supported.
  2. Parse the [id] at the end of each claim.
  3. Mark a claim invalid if its id is not in SOURCES.
  4. Mark it weak if fewer than half of its key words (4+ letters) appear in the cited passage.
  5. Print one line per claim and a summary of how many are supported.

Done when

  • Claim 1 is supported by pol-4.2.
  • The claim citing pol-9.9 is invalid.
  • The script prints 'supported: 2 of 4'.

Open in the Playground No API key needed.

Starter code (Python)
import re
SOURCES = {
    "pol-4.2": "Unworn shoes can be returned within 30 days of delivery for a full refund.",
    "pol-5.1": "Sale items are final and cannot be returned unless faulty.",
}
ANSWER = [
    "Unworn shoes can be returned within 30 days. [pol-4.2]",
    "Sale items cannot be returned unless they are faulty. [pol-5.1]",
    "Refunds arrive within 2 hours by bank transfer. [pol-4.2]",
    "Gift cards never expire. [pol-9.9]",
]

def check(claim):
    # TODO: find the id in [brackets]; return "invalid" if unknown,
    # "weak" if under half the 4+ letter words are in the source, else "supported"
    return "supported"

results = [check(c) for c in ANSWER]
for c, r in zip(ANSWER, results):
    print(f"{r:9} {c}")
print(f"supported: {results.count('supported')} of {len(ANSWER)}")
Show a solution
import re
SOURCES = {
    "pol-4.2": "Unworn shoes can be returned within 30 days of delivery for a full refund.",
    "pol-5.1": "Sale items are final and cannot be returned unless faulty.",
}
ANSWER = [
    "Unworn shoes can be returned within 30 days. [pol-4.2]",
    "Sale items cannot be returned unless they are faulty. [pol-5.1]",
    "Refunds arrive within 2 hours by bank transfer. [pol-4.2]",
    "Gift cards never expire. [pol-9.9]",
]

def key_words(text):
    return {w for w in re.findall(r"[a-z]+", text.lower()) if len(w) >= 4}

def check(claim):
    m = re.search(r"\[([\w.-]+)\]\s*$", claim)
    if not m or m.group(1) not in SOURCES:
        return "invalid"
    words = key_words(claim[: m.start()])
    hits = words & key_words(SOURCES[m.group(1)])
    return "supported" if words and len(hits) / len(words) >= 0.5 else "weak"

results = [check(c) for c in ANSWER]
for c, r in zip(ANSWER, results):
    print(f"{r:9} {c}")
print(f"supported: {results.count('supported')} of {len(ANSWER)}")
Quick checkWhich prompt design most reduces unsupported claims in answers from a long contract?
How sure are you?
Pick an answer and how sure you are