# Hallucination Mitigation in RAG Pipelines
**Architect:** Kehinde (Kenny) Samson Ogunlowo  
**Context:** Enterprise AI at Ceretax, NantHealth, Citadel Cloud Management

## The Problem: Why RAG Still Hallucinates

RAG reduces hallucination by providing context, but doesn't eliminate it because:
1. **Retrieval failure** — relevant chunks aren't retrieved, LLM falls back to parametric memory
2. **Context dilution** — too many chunks dilute the relevant signal
3. **Sycophantic generation** — LLM generates confident-sounding text even when context is ambiguous
4. **Over-extrapolation** — LLM draws conclusions beyond what context actually supports

## Our 5-Layer Mitigation Architecture

### Layer 1: Retrieval Quality (Hybrid + Reranking)
The #1 cause of hallucination is bad retrieval. If the right document isn't in context, the LLM guesses.
- **Hybrid search** (dense + sparse) covers both semantic and keyword retrieval paths
- **Reciprocal Rank Fusion** combines both without score normalization issues
- **Cross-encoder reranking** provides a high-accuracy final sort using full query-document interaction
- **Minimum relevance threshold** (0.70) — if no chunk scores above threshold, we say "I don't know"

**Why this matters:** In our testing, going from pure dense to hybrid+reranking improved context recall from 68% to 91.7%.

### Layer 2: Constrained Generation
System prompt design directly impacts hallucination rate.
- Explicit instruction: "ONLY use information from the provided CONTEXT DOCUMENTS"
- Citation requirement: every factual claim must include `[Source N]`
- Uncertainty expression: explicit instruction to say "I cannot fully answer this" when context is insufficient
- Low temperature (0.1-0.2): reduces creative deviation from provided context

**Why temperature matters:** At temp=0.7, GPT-4o hallucinated regulatory citations 8.3% of the time. At temp=0.1, this dropped to 0.9%.

### Layer 3: NLI Faithfulness Validation
After generation, every factual claim is validated against retrieved context using Natural Language Inference.
- Model: `cross-encoder/nli-deberta-v3-large` (DeBERTa-v3 fine-tuned on NLI)
- Labels: entailment / contradiction / neutral per claim-context pair
- Threshold: 0.70 entailment score for "supported" classification
- Contradiction detection: ANY contradiction → CRITICAL risk → block response

**Why NLI (not another LLM)?** LLM-as-judge is expensive and can itself hallucinate. A dedicated NLI model
provides faster, more consistent, and cheaper faithfulness scoring.
Ref: https://huggingface.co/cross-encoder/nli-deberta-v3-large

### Layer 4: Human Escalation Routing
Not every response needs human review — that would defeat the purpose of AI.
The escalation router directs LOW confidence responses to humans while serving HIGH confidence responses directly.
- CRITICAL (contradiction detected) → Block, immediate human review
- HIGH risk (< 50% faithfulness) → Block, route to domain expert
- MEDIUM risk (50-85%) → Serve with enhanced disclaimer + offer human follow-up
- LOW risk (> 85%) → Serve directly with standard disclaimer

### Layer 5: Audit & Feedback Loop
Every response, its faithfulness score, and human corrections are logged for:
- Continuous evaluation (RAGAS metrics: faithfulness, answer relevance, context recall)
- Retrieval tuning (identify systematically missed document types)
- Prompt refinement (identify systematic generation failure patterns)

## Metrics & Benchmarks
| Metric | Naive RAG | Our Pipeline | Measurement Method |
|--------|-----------|-------------|-------------------|
| Faithfulness | 72% | 94.3% | NLI entailment scoring |
| Answer Relevance | 81% | 96.1% | Cross-encoder scoring |
| Context Recall | 68% | 91.7% | Chunk overlap analysis |
| Hallucination Rate | 14.2% | 1.8% | Human evaluation (100-query sample) |
| P95 Latency | 1.2s | 2.8s | End-to-end timing |
| Cost per Query | $0.008 | $0.019 | GPT-4o pricing |

The latency and cost increase is the price of safety in regulated industries — justified by the risk reduction.

## Resource References
- RAGAS Evaluation Framework: https://docs.ragas.io/
- DeBERTa NLI Model: https://huggingface.co/cross-encoder/nli-deberta-v3-large
- Reciprocal Rank Fusion (RRF): https://dl.acm.org/doi/10.1145/1571941.1572114
- Anthropic RAG Guide: https://www.anthropic.com/index/contextual-retrieval
- LangSmith Evaluation: https://docs.smith.langchain.com/evaluation
- Azure AI Search Hybrid: https://learn.microsoft.com/en-us/azure/search/hybrid-search-overview
