top of page

Tutorial 8: Reduce Hallucinations

  • Contributor
  • Jun 5
  • 3 min read

LLMs sometimes invent facts confidently. For most production uses, that's unacceptable. This tutorial covers techniques that reduce hallucinations significantly.

What You'll Build

A set of prompt techniques and code patterns that cut hallucination rate.

Step 1: Ground in Source Material (most powerful, 15 min)

The single biggest hallucination reducer: give the model the source material to work from.

You are answering questions about our company policies. Use ONLY 
the information in the policy documents provided below. If the 
information is not in the documents, say "I don't know."

Documents:
{relevant_policy_excerpts}

User question: {question}

The model can still make up things, but the explicit instruction + relevant context dramatically reduces it.

Step 2: Add "Cite Your Source" (10 min)

After your answer, cite which document section you used.

Format:
[Answer]

Source: [section reference]

The citation forces the model to ground in the provided material. If it can't cite, you know to be skeptical.

Step 3: Instruct on Uncertainty (10 min)

When uncertain or when the information isn't available:
- Say so explicitly
- Do not guess
- Do not extrapolate beyond what's stated
- Acknowledge the limits of what's given

Explicit instruction. Models will follow it (mostly).

Step 4: Use Temperature = 0 for Factual Tasks (5 min)

response = client.messages.create(
    model="claude-sonnet-4-6",
    temperature=0,  # Deterministic
    ...
)

Lower temperature = less randomness = fewer creative inventions. For factual Q&A, use 0.

For creative tasks, higher temperature is fine.

Step 5: Verify Output Against Source (varies)

For automated checking:

def verify_answer(answer, source_text):
    # Use the LLM to check itself
    verify_prompt = f"""
    Given this source:
    {source_text}
    
    And this answer:
    {answer}
    
    Is every claim in the answer supported by the source? 
    Respond with: 
    {{ "supported": true/false, "issues": [list of unsupported claims] }}
    """
    
    result = call_llm(verify_prompt)
    return json.loads(result)

Self-checking catches some hallucinations. Not perfect, but helps.

Step 6: Use RAG for Knowledge-Based Tasks (varies)

If the model needs to answer from a corpus of knowledge:

  • Don't rely on its training data

  • Build a retrieval-augmented generation (RAG) system

  • Retrieve relevant passages before answering

  • Ground the response in retrieved content

Step 7: Constrain the Output Space (10 min)

For classification tasks, constrain choices:

Classify this ticket. Choose exactly one:
- billing
- technical  
- account
- other

Only output the category name. Nothing else.

Can't hallucinate a category that's not in the list.

Step 8: Ask for "What Don't You Know" (10 min)

For complex tasks:

Based on the information provided, answer the question. Then list:
- Confidence: high/medium/low
- Uncertain claims: what you couldn't verify
- Missing information: what would improve the answer

The reflection often surfaces what the model is uncertain about.

Step 9: Detect Common Hallucination Patterns (15 min)

Some hallucinations are detectable:

  • Specific dates/numbers the model invents: cross-check

  • Quotes the model fabricates: verify against source

  • Function/library names that don't exist: check

  • URLs the model invents: validate

For these, post-process to flag or reject suspicious content.

Step 10: Measure Hallucination Rate (varies)

Build an eval set of questions where you know the right answer:

  • Questions answerable from source material

  • Questions not answerable (model should say "I don't know")

  • Questions with subtle wrong-but-plausible answers

Score:

  • Correct answers

  • Honest "don't know"s

  • Hallucinations (confident wrong)

Track the rate over prompt versions. Improvements should move it.

What You Just Did

You combined techniques that significantly reduce hallucinations: grounding in source material, citations, explicit uncertainty, low temperature, verification, constrained output spaces. None eliminates hallucinations entirely; together they reduce dramatically.

Common Failure Modes

Trusting "I don't know" rates. Models still hallucinate even with instructions; verify.

No source grounding. Pure generation = highest hallucination rate.

No measurement. Can't tell if techniques are helping.

Temperature = 1 for factual. Creative randomness = more hallucinations.

Over-trust. Treating LLM output as authoritative. It isn't.

Next Tutorial

Now manage costs: Tutorial 9: Token Budgeting.

Related reading

Keep learning. This article is part of the AI in Quality & Delivery path in the ShiftQuality Learning Center. Use AI in delivery — and evaluate it honestly — without the hype.

bottom of page