Hallucination Detection
~2 min read
Concept & How It Works
Why Does It Exist?
Hallucinations in support, legal, or medical agents cause real harm. Detection layers gate outputs or trigger retrieval retries.
Real-World Analogy
Hallucination detection is a fact-checker with highlighter — every claim must be tied to a source line or flagged red.
Visual Workflows
What is Hallucination Detection?
Example
Scenario
Agent says '30-day return policy' but retrieved doc says 14 days — NLI scorer flags contradiction; agent regenerates with correction.
Solution
In Agent Evaluation & Observability, apply Hallucination Detection to this scenario: Agent says '30-day return policy' but retrieved doc says 14 days — NLI scorer flags contradiction; agent regenerates with correction. Identify the inputs, run the technique, validate the output, and note one thing you would monitor in production.
Practice Task
Do this before moving to the next module — reading alone is not enough.
Open the Code Walkthrough below and run it locally. Change one parameter related to Hallucination Detection (e.g. model, temperature, top_k, or tool name), observe the difference in output, and write 2–3 sentences explaining what changed.
Code Walkthrough
Highlighted lines show where Hallucination Detection happens in the code.
1def groundedness_score(answer, context): # define a reusable function2 prompt = f"Context: {context}\nAnswer: {answer}\nList unsupported claims."3 return judge_llm.invoke(prompt) # returns list of unsupported spansCommands to Remember
Commands to Remember
pip install langsmith # trace and evaluate LLM runspip install arize-phoenix # open-source LLM observabilitypip install opentelemetry-api opentelemetry-sdk # distributed tracing
Common Mistakes
- Treating Hallucination Detection as a black box without evaluation
- Ignoring cost and latency in production
- Skipping error handling for hallucination detection
Cheat Sheet
Quick recap — the most important points from this module.
Cheat Sheet
quick ref- •Hallucination Detection
- •NLI
- •Groundedness
- •Attribution