How do you handle hallucination detection and prevention in RAG systems?
Hallucination in RAG occurs when the LLM generates information not supported by retrieved context. Prevention strategies: explicit grounding instructions in the system prompt (only answer based on provided context, say "I don't know" when context is insufficient), constrain generation temperature (0.0-0.3 for factual answers), and implement citation requirements (model must reference specific source chunks). Detection methods: NLI-based (run natural language inference model to check if each generated claim is entailed by the context), self-consistency (generate multiple answers and check agreement), source attribution verification (check that cited sources actually contain the claimed information), and LLM-as-judge (use a separate model to evaluate faithfulness). Implementation: post-generation pipeline extracts claims from the answer, each claim is verified against retrieved context using an NLI model or LLM evaluator, unverified claims are flagged or removed, and a confidence score is attached to the response. Architectural approaches: implement a verification layer that checks facts before returning to the user, use chain-of-thought prompting that shows reasoning steps (making hallucinations more visible), and implement retrieval-augmented verification (retrieve additional sources to cross-check claims). Monitoring: track hallucination rate using automated detection, analyze patterns (which topics or question types produce more hallucinations), and implement user feedback mechanisms for reporting incorrect answers. Root cause analysis: hallucinations often indicate retrieval failure (relevant context not retrieved), context insufficiency (the knowledge base lacks the information), or prompt design issues (instructions not clear enough about staying grounded in context).