Hallucinations in LLMs

Hallucinations in LLMs

Minimize fabricated facts by designing prompts, validation steps, and guardrails that demand evidence.

Introduction

Hallucinations occur when a model generates plausible-sounding but incorrect information.

  • Definition: Statements or citations that are not grounded in supplied data or real-world facts.
  • Why it happens: Models extrapolate patterns; without grounding, they fill gaps with guesses.
  • Where noticed: Knowledge assistants, regulatory responses, customer support, analytics summaries.

Syntax

[
  {"role": "system", "content": "Answer only using the provided context. If the answer is missing, reply 'Not in dataset.'"},
  {"role": "user", "content": "Context: {{retrieved documents}}\nQuestion: {{user query}}\nInclude citations like [Doc#]."}
]

Example

Example 1 — Cited Response
[
  {"role": "system", "content": "You are a compliance analyst. Use bullet points and cite the source doc ID."},
  {"role": "user", "content": "Context: Doc12 states 'encryption at rest is mandatory'. Doc19 says 'key rotation every 90 days'.\nQuestion: What security controls are required?"}
]
Output
- Encrypt all stored customer data using approved ciphers. [Doc12]
- Rotate encryption keys at least every 90 days through the key vault process. [Doc19]
Example 2 — Double Check Strategy
def ask_with_verification(question: str, context: str) -> str:
    base = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Answer using context only. Admit if unsure."},
            {"role": "user", "content": f"Context: {context}\nQuestion: {question}"}
        ]
    )
    answer = base.choices[0].message.content

    verifier = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "You are a fact checker. Respond PASS if every statement is supported, otherwise list issues."},
            {"role": "user", "content": f"Context: {context}\nAnswer: {answer}"}
        ]
    )
    return answer, verifier.choices[0].message.content

Explanation

  • Grounding ensures the model references only retrieved documents or databases.
  • Explicit fallback language (“Not in dataset”) encourages honesty when information is missing.
  • Verification step catches unsupported statements before they reach end users.
  • Difference from bias: Hallucination is factual error; bias is skewed framing or tone.

Real-World Use Case

An insurance firm deploys a policy assistant. Each answer must cite the clause ID. Responses failing the automatic citation check trigger human review, reducing hallucinated policy advice by over 80%.

Key Notes / Tips

  • Always log citations and confidence scores for auditing.
  • Use retrieval augmented generation (RAG) or tool calls to pull authoritative data.
  • Set temperature low for factual workflows to decrease creative deviations.
  • Implement evaluation suites with known right/wrong answers to monitor drift.
  • Escalate unanswered questions to humans instead of guessing.

Practice Exercise

  1. Write a prompt that forces the model to return “Insufficient evidence” whenever citations are missing.
  2. Implement a secondary verification call that flags sentences without supporting context.
  3. Challenge: Design a logging schema capturing question, answer, sources, and verification score for audits.

Summary

Hallucinations erode trust. Counter them with grounded prompts, citation requirements, and automated fact checking so users always know where answers come from.

© ItTechGenie