AI Agent Guide Interactive
Module 3 · RAG Retrieval & Grounding 3 Interactive Labs Node.js + TypeScript

Citation Grounding & Evidence Verification

Your reranker already pushed security-001 to rank #1, but a ranked chunk is still just raw evidence—not an answer users can trust. Hand those chunks to an LLM and it can still slip in "common sense" from pretraining, conflate similar APIs, or cite an outdated spec as current policy. This chapter completes the post-reranker anti-hallucination loop: pre-generation threshold gates, numbered evidence injection, citation tracing, faithfulness verification, and the architectural boundaries that prompt tuning alone cannot fix.

30-Second TL;DR Grab the core rules first, then test them in the labs
  1. 01 Never force an LLM to answer when evidence is weak. Gate low-score queries with a rerankScore threshold before calling the model, and inject surviving chunks with [1] [2] indices plus strict out-of-bounds rules.
  2. 02 Seeing [1] in an answer does not mean the claim came from chunk 1. Use Faithfulness checks to decompose answers into atomic claims and verify each against evidence—while watching out for "faithful yet off-topic" traps.
  3. 03 The highest-leverage place to cut hallucinations is retrieval context quality, not the answer prompt. Entity drift, version conflicts, and stale multi-turn evidence require metadata filters and context isolation.

Where Hallucinations Come From & the 4-Stage Defense Loop

"RAG reduces hallucinations" carries a hidden premise: the evidence entering the context window is accurate, and the model actually restricts itself to that evidence.

In production RAG systems, plausible-sounding answers that mislead users almost always trace back to three failure modes:

  1. Entity Drift: Semantically similar chunks about a different entity enter the candidate pool. The user asks about deleteAccount API error codes, vector search retrieves deleteUser API docs, and the model confidently explains the wrong endpoint.
  2. Version Mismatch: Both legacy and current specs live in the knowledge base. The old doc gets retrieved, breaking changes disappear from the answer, and users follow an obsolete runbook.
  3. Fabricated Details: The hardest failure to spot. The chunk only says deleteAccount checks foreign-key references, and the model helpfully adds "you can usually call cleanupReferences API first"—an endpoint that does not exist anywhere in your codebase.

Fabrication happens because an LLM is optimized to produce fluent, helpful text rather than strictly quote retrieved facts. Turning candidate chunks into auditable answers requires four defenses after reranking: a pre-generation threshold gate, numbered evidence injection, citation extraction, and faithfulness verification.

Four-Stage Anti-Hallucination Loop: From Reranked Chunks to Verified Answers Architecture diagram
Reranker Pool Top-K chunks with relevance_score 1. Threshold Gate score ≥ threshold? Drop low-score noise All below threshold Abstain (Skip LLM) State gap & next step Pass 2. Numbered Inject [1] [2] source tags Strict grounding rules 3. Cited Answer Extract [N] markers Map to cited chunks 4. Faithfulness Decompose & verify Faithfulness score Low-score feedback loop: dirty retrieval context or loose constraints?

Pre-Generation Gating & Numbered Evidence Injection

Block under-supported queries with rerankScore first, then pass surviving chunks with explicit index numbers so every claim maps back to a concrete source.

Many RAG pipelines still shove low-scoring chunks into the prompt when nothing relevant exists, forcing the model to stitch together a plausible guess. Gating with EVIDENCE_THRESHOLD delivers two wins at once: you skip a wasted LLM call, and you give the user actionable feedback on what is missing and where to look instead of a vague "I don't know."

Lab 01 · Pre-Generation Gate & Evidence Injection Sandbox

Drag the Rerank threshold to see when chunks pass, get falsely dropped, or trigger abstention

Compare a well-supported query (409 troubleshooting) against a knowledge-base gap (rate-limit policy). Adjust EVIDENCE_THRESHOLD and toggle the injection format to inspect the exact LLM payload or abstention response.

Simulated rerank scores
Query:
Injection Format:
Pre-generation gate EVIDENCE_THRESHOLD (admits only chunks with rerankScore ≥ threshold) 0.40
0.05 (Very loose · leaks noise) 0.40 (Starting baseline) 0.95 (Very strict · false rejections)
Reranker Candidate Pool (red line = threshold) Simulated scores · intuition demo
Actual Prompt Payload Sent to LLM Passed

          
View TypeScript implementation synced to the current threshold (buildEvidenceBlock + pre-generation gate)
answer-with-citation.ts · synced live with the slider above
0.40 is only a starting point—never copy thresholds across rerankers
Always calibrate your threshold on your own evaluation set. A sparse knowledge base may lose answerable questions at 0.40, while compliance-heavy domains often push the bar above 0.60. Score distributions also vary widely across reranker models, so swapping rerankers always requires recalibration.

Citations ≠ Verification: Why You Still Need Faithfulness Checks

Seeing [1] or [2] in an answer only proves the model claims a sentence came from that chunk—not that the chunk actually supports it.

Once numbered evidence enters the prompt, a regex can extract every [N] marker and map it back to the source chunk. Yet two subtle bugs routinely slip past format checks in production: the model slips ungrounded "common sense" right before a valid citation marker, or it misattributes a fact from chunk 2 to [1].

Predict First If every sentence in a RAG answer ends with a valid [1] or [2] marker pointing to real retrieved chunks, does that guarantee the answer is hallucination-free?
Spot on: citation markers are a formatting constraint, not factual verification. As defined in the RAGAS Faithfulness documentation, faithfulness decomposes an answer into atomic factual claims, checks whether each claim can be inferred from the retrieved context, and computes the fraction of supported claims.
Lab 02 · Citation Tracing & Claim-Level Faithfulness Verifier

Click [1] [2] markers and atomic claims to see how faithfulness catches fake citations

Query: How do I troubleshoot a 409 error from deleteAccount API? Switch across 4 model outputs to compare marker syntax checks, Faithfulness, and Answer Relevancy.

Definition: RAGAS Faithfulness
Model Output Sample:
Generated Cited Answer (click markers to trace) Valid Markers
Admitted Source Chunks (highlighted = active citation) Top-2 from candidate pool
Claim-by-Claim Faithfulness Audit Simulated LLM judge output
View TypeScript implementation for citation extraction and Faithfulness verification
extractCitedChunks + checkFaithfulness · answer-with-citation.ts
function extractCitedChunks(answer: string, chunks: EvidenceChunk[]): EvidenceChunk[] {
  const cited = new Set<number>();
  for (const match of answer.matchAll(/\[(\d+)\]/g)) {
    const idx = parseInt(match[1], 10) - 1;
    if (idx >= 0 && idx < chunks.length) cited.add(idx);
  }
  return [...cited].map((i) => chunks[i]);
}

async function checkFaithfulness(
  answer: string,
  citedChunks: EvidenceChunk[]
): Promise<{ score: number; reason: string }> {
  if (citedChunks.length === 0) {
    return { score: 0, reason: "Answer cited zero evidence chunks" };
  }

  const evidenceText = citedChunks.map((c) => c.text).join("\n\n");
  const response = await anthropic.messages.create({
    model: llmModel,
    max_tokens: 300,
    messages: [
      {
        role: "user",
        content: [
          "Check whether every key factual claim in the answer is directly supported by the evidence chunks.",
          "If any claim introduces facts absent from the evidence, score it as unfaithful.",
          "",
          "Evidence chunks:",
          evidenceText,
          "",
          "Answer:",
          answer,
          "",
          'Return JSON only: {"score": number between 0 and 1, "reason": "one-sentence summary"}',
        ].join("\n"),
      },
    ],
  });

  const textBlock = response.content.find((b) => b.type === "text");
  const text = textBlock?.type === "text" ? textBlock.text : "";
  const jsonMatch = text.match(/\{[\s\S]*\}/);
  if (!jsonMatch) return { score: 0, reason: "Failed to parse verifier output" };

  try {
    return JSON.parse(jsonMatch[0]) as { score: number; reason: string };
  } catch {
    return { score: 0, reason: "JSON parse error" };
  }
}
Two Verification Limits You Shouldn't Ignore
1. LLM-as-a-Judge can miss errors too: When answers and evidence grow long, a single holistic judge call easily overlooks subtle fabrications. In production, follow the RAGAS pattern: decompose into atomic claims first, verify each claim in isolation, and aggregate the ratio.
2. Faithfulness is never a standalone KPI: As Lab 02 Case ④ demonstrates, an answer that quotes chunk [2] verbatim while ignoring the user's actual question still scores 1.00 on Faithfulness. Always pair it with Answer Relevancy.

Four Hallucination Classes Prompt Tuning Cannot Fix

Prompts only govern how the model speaks—they cannot change what evidence enters the context window. If the input context is poisoned, stricter wording won't save the answer.

Teams often burn days adding "NEVER hallucinate" and "double-check every fact" to system prompts while the real culprit sits upstream: wrong entities, stale doc versions, or leftover chunks from prior turns. The four failure modes below must be solved at the retrieval and context-orchestration layers.

Lab 03 · Architectural Hallucination Clinic

Four prompt-proof hallucinations: inspect how context gets poisoned and how architecture fixes it

Step through 4 recurring production failures and toggle between "Prompt-only guardrails" and "Retrieval / context architecture fix" to compare what the model sees and what it outputs.

Choice → Consequence → Fix
Failure Mode:
Strategy:
Actual Context Seen by the Model Context Poisoned
Generated Answer & Diagnosis Silent Hallucination

Anti-Hallucination Defense Matrix

RAG splits into retrieval and generation: answer-stage guardrails stop phrasing fabrications, while retrieval and context governance prevent entity drift, version clashes, and cross-turn contamination.

Pre-Gate
Rerank Threshold Gate

Drop chunks with rerankScore < threshold; abstain immediately when zero chunks pass and point users to the right owner or doc, saving an LLM call. Calibrate per eval set and per reranker.

Injection
Numbered Evidence + Strict Bounds

Inject Top-3 to Top-5 high-scoring chunks using clear [1] Source: ... delimiters, forbid extrapolating external business details, and define a deterministic abstention prefix.

Attribution
[N] Markers & Source Extraction

Require inline citation markers after key factual claims and extract cited chunks via extractCitedChunks to power UI source cards and targeted faithfulness checks.

Post-Check
Claim-Level Faithfulness Audit

In high-stakes domains (legal, healthcare, finance, core ops), never treat valid markers as proof. Decompose answers into atomic claims per RAGAS and evaluate alongside Answer Relevancy.

Metadata
Entity Filters & Version Timestamps

Index endpoint names, service IDs, versions, and updatedAt at ingestion time; filter early at retrieval and down-rank expired specs to prevent entity drift and version clashes.

Multi-Turn
Reset Evidence Every Turn

Never persist raw evidence-stuffed user messages into conversation history. Store clean Q&A pairs in history, re-retrieve on each new turn, and inject only the current turn's evidence.