How do you implement RAG guardrails to prevent hallucination?
RAG hallucination prevention requires multiple guardrail layers. Input guardrails: classify query intent to reject out-of-scope questions, detect prompt injection attempts, and validate query complexity. Retrieval guardrails: set minimum relevance thresholds (reject results below cosine similarity 0.7), require minimum number of corroborating sources, and flag when no relevant context is found. Generation guardrails: use constrained prompting ("Answer ONLY based on the provided context. If the context doesn't contain the answer, say so"), implement citation requirements (every claim must reference a specific chunk), and apply post-generation verification using a separate LLM call that checks each claim against the source context. Output guardrails: confidence scoring (flag low-confidence answers for human review), factual consistency checking comparing the response against retrieved context, and format validation. Production monitoring: track hallucination rate using RAGAS faithfulness metric, maintain a feedback loop where flagged responses are reviewed and added to test sets. Aim for faithfulness scores above 0.9 in production.