Agentic AI Notebook
Phase 19

Hallucination Detection

~2 min read

Concept & How It Works

  • Key points are in the visual diagram above.

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.
Loading diagram...

Visual Workflows

What is Hallucination Detection?

Loading diagram...

Example

Scenario

Agent says '30-day return policy' but retrieved doc says 14 days — NLI scorer flags contradiction; agent regenerates with correction.

Solution

In Eval Engineering & 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.

Hallucination Detection
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 spans

Commands to Remember

Commands to Remember

  • pip install langsmith # trace and evaluate LLM runs
  • pip install arize-phoenix # open-source LLM observability
  • pip 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