Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 14/ 160
01. LLM Fundamentals

Hallucinations

Why models invent facts, how to detect them, and mitigation via grounding and abstention.

BeginnerGrounding · Abstention3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

The model sometimes makes up answers that sound right, so we give it the facts to use and let it say "I don't know".

Think of it like…

A confident new colleague who never says "I'm not sure": they answer every question smoothly, and when they don't know, they fill the gap with something that sounds right. Giving them the source documents and a rule to cite them, and a way to say "not in the documents", changes the outcome.

Where you’ve seen it

A policy assistant told a customer that a plan "includes free international roaming", which didn't exist. The fix had four parts: answer only from retrieved policy text, require a citation (a quote and document id) for each claim, return "I couldn't find that in our policies" when retrieval is weak, and run a nightly check that sampled answers are supported by their cited passages. Unsupported-claim rate on the review set fell from 9 percent to under 2.

Words you’ll hear

Grounding
Constraining answers to provided, trusted sources (retrieval, tools, databases).
Abstention
Declining to answer when evidence is missing or confidence is low.
Citation / attribution
Pointing each claim to the passage that supports it.
Faithfulness
Whether an answer is supported by the provided context.

Visual diagram

Loading interactive diagram

Theory & deep dive

Why models hallucinate

Pretraining teaches a model to predict plausible text, so when a fact is rare or absent from its weights it still produces a plausible-sounding answer. Fine-tuning and evaluation incentives often reward confident answers over honest uncertainty. Other triggers include questions about recent events beyond the training cutoff, obscure entities, long documents where relevant details are buried, ambiguous prompts that invite guessing, and prompts that presuppose a falsehood ("Why did the 2019 treaty fail?" when there was none).

Models are also poor at knowing what they know: stated confidence correlates only loosely with correctness, so asking "are you sure?" is weak verification. This is why defences focus on external evidence and checks, not on the model's self-assessment alone.

A false-premise trap

"Summarise the findings of the 2021 Mumbai Metro safety audit by the WHO." A model without grounding may produce a plausible summary of an audit that never happened. A grounded system searches, finds nothing and says so.

Key takeaway: Models generate plausibility, not truth; confidence is not a reliable signal.

Mitigation and measurement

Ground the answer in retrieved documents, database results or tool outputs, and instruct the model to use only that material. Require citations (document id plus a short quote) and check programmatically that the quote exists in the source. Enable abstention: define an explicit "not enough information" response and trigger it when retrieval scores are low or no passage supports the answer. Verify: a second pass or separate model checks each claim against its citation; for structured work, validate against schemas, databases or run the code.

Measure it. Build a labelled set of questions with known answers, including unanswerable ones and false-premise ones, and track faithfulness (supported by context), answer correctness, abstention rate on unanswerable questions, and citation accuracy. Sample production traffic for human review and use user feedback signals. For high-stakes domains, add human approval and design the UI to show sources so users can verify. Lowering temperature and choosing stronger models help at the margin but don't replace grounding.

A citation check in code

For each claim the model returns {"claim": ..., "doc_id": ..., "quote": ...}. Reject the answer if the quote doesn't appear verbatim in that document, or run a small entailment check to confirm the quote supports the claim.

Key takeaway: Ground, cite, abstain, verify, then measure with unanswerable and false-premise cases.

How it works, step by step

Step 1 of 4
Retrieval / tools

1. Ground the answer

Fetch trusted documents or data and give them to the model as the only source of facts.

top-5 policy passages + document ids
Key numbers at this step
Source
trusted, current

When to use it & trade-offs

What it is

A hallucination is fluent output that is false or unsupported: an invented citation, a made-up API parameter, a wrong date stated confidently. It happens because an LLM generates the most plausible continuation, not a verified fact, and training rewards producing an answer over saying "I don't know". Two kinds matter: intrinsic (contradicts the provided source) and extrinsic (adds claims the source can't verify). You reduce them with grounding, abstention, verification and good evaluation, but you can't eliminate them.

Why it matters: Hallucinations are the main reason users and companies distrust AI features, and in legal, medical, financial or support settings they cause real harm and liability. Engineers need practical defences and a way to measure them, not a vague hope that a better model fixes it. This is a staple topic for AI engineering interviews.

When to reach for it

  • Policy, legal and medical-adjacent assistants
  • Any answer that needs citations
  • Evaluation sets with unanswerable questions

When to avoid or be careful

  • Trusting "are you sure?" as verification
  • Measuring only on questions the system can answer
  • Showing citations without checking they support the claim

Mitigation techniques

Retrieval grounding + citations
Best for: Knowledge-base and policy assistants

Strengths: Answers tied to checkable evidence.

Watch out: Only as good as retrieval; can still misread sources.

Verification pass or tool checks
Best for: High-stakes outputs, code and structured data

Strengths: Catches unsupported claims before users see them.

Watch out: Extra latency and cost.

Lower temperature / stronger model
Best for: Marginal improvements everywhere

Strengths: Easy to apply.

Watch out: Doesn't remove hallucination; not a substitute for grounding.

Interview signals & numbers to know

When this topic comes up

  • “The bot made up a policy”
  • “How do you reduce hallucinations?”
  • “How do you measure faithfulness?”
  • Grounding and abstention
  • Citation checking

Benchmarks & rules of thumb

9% → 2% -
unsupported claims in the policy assistant example
4 layers -
ground, constrain, verify, measure
0 -
guarantees from any single technique

Common mistakes & follow-ups

Common interview mistakes

  • Adding "do not hallucinate" to the prompt and calling it done.
  • Evaluating only answerable questions, so abstention behaviour is never tested.
  • Displaying a source link next to an answer that the source doesn't actually support.

Likely interviewer follow-ups

  • How would you design a RAG system that rarely makes things up?
  • How do you measure hallucination rate?
  • Why isn't lowering temperature enough?
Interview tip: Name the four layers (ground, cite and abstain, verify, measure) and mention unanswerable questions in the eval set. Few candidates test the model's ability to say "I don't know".
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply Hallucinations with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Token Pricing
Next topic
Multimodal Inputs