Velamuvi AIAcademy

Lesson 12 of 15 · Advanced Techniques

Reducing Hallucinations

Prompt techniques that cut AI hallucinations: ground answers in sources, allow 'I don't know', quote first, verify citations and avoid false premises.

  • 7 min read
  • Beginner
  • Reviewed
In this lesson you will learn
  • Prompt techniques that make hallucinations less likely
  • How to ground answers in a source and allow “I don't know”
  • The quote-first technique for long documents
  • How to verify citations and quotes, including in code

Prompts can make hallucination more or less likely

A hallucination is a confident, false statement. The Generative AI Fundamentals course explains why models do it: they produce likely text, not checked text. This lesson is about what to do in your prompts. No technique removes hallucinations completely, but together these make them much rarer and much easier to catch.

Prompt

What warranty comes with the Brightmix X200 blender?

Example response

The Brightmix X200 comes with a 2-year limited warranty covering defects in materials and workmanship, plus a 5-year warranty on the motor.

Specific, confident and possibly invented. A small brand's warranty is exactly the kind of rare fact a model blurs or makes up.

1. Give it the source

Paste the document, and say: "Answer using only the information in the document." Answers from material in the context window are far more reliable than answers from the model's memory, and you can check them against the source.

2. Give permission to say "I don't know"

Models lean toward answering. Make not knowing an acceptable, explicit option:

  • "If the document does not contain the answer, say so."
  • "If you are not sure, say what you are unsure about rather than guessing."
  • "It is fine to answer 'not enough information'."

This one sentence is among the cheapest and most effective anti-hallucination techniques there is.

3. Quote first, then answer

For long documents, ask the model to find the exact quotes that matter before answering:

<report>
...
</report>

First, find the quotes from the report that are relevant to the question
and put them inside <quotes> tags. If there are none, write "No relevant quotes".
Then answer the question using only those quotes, inside <answer> tags.

Question: What did the report recommend about staff training?

This forces the answer to rest on specific text, and the quotes are easy to check.

4. Ask for citations, then check them

Ask the model to support each claim with a quote or a reference. Then actually check them, because a model can invent a quote as easily as a fact. When the quotes are exact, checking can be automated:

Try it yourselfPython
import re

source = ("The library is open from 9am to 6pm on weekdays. "
          "On Saturdays it closes at 1pm. It is closed on Sundays. "
          "Members can borrow up to 8 books at a time.")

# The model was asked to support every claim with an exact quote
answer = ('On Saturdays you have until lunchtime: "On Saturdays it closes at 1pm." '
          'You can take plenty home: "Members can borrow up to 10 books at a time."')

quotes = re.findall(r'"([^"]+)"', answer)   # everything between double quotes
for quote in quotes:
    if quote in source:
        print("VERIFIED:     ", quote)
    else:
        print("NOT IN SOURCE:", quote)

The first quote is real. The second one changed "8 books" to "10 books": a small, plausible, wrong detail, exactly the kind of hallucination that is easy to miss by eye.

5. Don't build false facts into the question

A question with a wrong assumption invites a wrong answer.

Leading questionOpen question
"Why did Einstein win the Nobel Prize for relativity?""What did Einstein win the Nobel Prize for?"
"Which function in pandas sorts a list of emails by domain?""Is there a pandas function for sorting emails by domain, or do I need to write one?"

Open questions let the model correct you instead of following you.

6. Use tools and settings

  • Search: an assistant with web search can find current information and link to it.
  • Code: for maths and data, ask it to calculate with code instead of "in its head".
  • Temperature: when you control it (in an API), use a low value for factual tasks.
  • Separate facts from guesses: "Mark anything that is an assumption rather than stated in the source."

Common mistakes

Practice exercise

Check a model's quotes against the source document and report how many are genuine.

Your solutionPython
source = "Our office is at 12 Hill Road. We open at 8am. Parking is free for visitors."
quotes = ["We open at 8am.", "Parking is free for visitors.", "We close at 8pm."]

# Count how many quotes appear exactly in the source, then print:
# 2 of 3 quotes verified
Knowledge check

Which prompt change is MOST likely to stop a model inventing an answer it doesn't have?

Summary

  • Ground the model: give it the source and ask it to answer only from it.
  • Give explicit permission to say "I don't know".
  • For long documents, quote first, then answer.
  • Ask for citations and verify them; exact quotes can be checked automatically.
  • Avoid false premises, use tools for facts and maths, and separate facts from assumptions.

Next: turning good prompts into reusable templates.

Finished reading? Tick it off to track your progress.