Strategies for Reducing Hallucinations
Implement methods to minimize factual inaccuracies and fabricated information generated by LLMs.
Strategies for Reducing Hallucinations is a free AI Prompt Engineering lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Prompt Engineering learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What are LLM Hallucinations?
Welcome! In this lesson, we'll tackle a common challenge with Large Language Models (LLMs): hallucinations.
An LLM hallucination occurs when the model generates information that is factually incorrect, nonsensical, or completely fabricated, yet presents it as if it were true. It's like the AI is confidently making things up!
Why LLMs 'Hallucinate'
LLMs are trained to predict the next most probable word based on patterns in vast amounts of text data. They don't 'know' facts in the human sense.
- Sometimes, they lack sufficient or relevant information.
- They might extrapolate beyond their training data.
- They prioritize sounding coherent and confident over being factually accurate.
Understanding this helps us design better prompts to guide them.
Strategy 1: Grounding with Context
One of the most effective ways to reduce hallucinations is to ground the LLM with relevant information. Instead of asking it to recall facts from its training, provide the necessary context directly in your prompt.
Think of the LLM as a very smart reader of the text you give it, not a search engine that knows everything.
Example: Providing Context
Here’s how you can provide specific context to an LLM. The model will then base its answer solely on the provided text.
def simulate_llm_response(prompt):
print(f"LLM received prompt:\n---\n{prompt}\n---")
context = "Marie Curie was a Polish and naturalized-French physicist and chemist who conducted pioneering research on radioactivity. She was the first woman to win a Nobel Prize."
query = "Tell me about Marie Curie's nationality based on the provided text."
prompt_with_context = (
f"Context: {context}\n\n"
f"Question: {query}\n\n"
f"Answer (only use the context provided):"
)
simulate_llm_response(prompt_with_context)Strategy 2: Instructing for Uncertainty
LLMs often sound confident even when they are wrong. You can mitigate this by explicitly instructing the model to admit when it doesn't know the answer.
This encourages the LLM to provide a 'don't know' response instead of fabricating information to fill a gap.
Example: Uncertainty Prompt
Add a clear instruction like 'If you don't know, state that you don't have enough information.'
def simulate_llm_response(prompt):
print(f"LLM received prompt:\n---\n{prompt}\n---")
query = "What is the capital city of the planet Mars?"
prompt_with_uncertainty = (
f"Answer the following question. If you don't know the answer, "
f"please state 'I don't know' rather than guessing or fabricating information.\n\n"
f"Question: {query}\n\n"
f"Answer:"
)
simulate_llm_response(prompt_with_uncertainty)Strategy 3: Requesting Sources
Another helpful strategy is to ask the LLM to cite its sources or explain its reasoning process. While it might not always provide real URLs, this can:
- Force the model to retrieve information more carefully.
- Help you identify when it's making things up (if it can't provide a plausible source or reasoning path).
Strategy 4: Encourage Step-by-Step
For complex questions, asking the LLM to 'think step-by-step' or 'explain your reasoning' before giving the final answer can significantly reduce hallucinations.
This allows the model to break down the problem, which often leads to more accurate intermediate steps and a more reliable final answer.
Iterate and Refine Prompts
Prompt engineering is an iterative process. If an LLM hallucinates, don't give up!
- Rephrase: Try different wording for your prompt.
- Add more context: Provide even more specific information.
- Specify format: Ask for answers in a structured format (e.g., bullet points, JSON) to constrain output.
- Combine strategies: Use grounding, uncertainty instructions, and step-by-step thinking together.
Quick Check: Hallucination Fixes
Which of the following are effective strategies to reduce LLM hallucinations?
Recap: Combatting Hallucinations
Great job! You've learned key strategies to minimize LLM hallucinations:
- Grounding: Provide explicit context.
- Uncertainty: Instruct the LLM to state when it doesn't know.
- Verification: Ask for sources or reasoning.
- Step-by-Step: Encourage detailed thinking.
- Iteration: Continuously refine your prompts.
By applying these techniques, you can make your LLM interactions more reliable and accurate!
Frequently asked questions
Is the “Strategies for Reducing Hallucinations” lesson free?
Yes — the full text of “Strategies for Reducing Hallucinations” is free to read here on the web, and the AI Prompt Engineering course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Prompt Engineering course, upgrade to CoddyKit PRO.
What will I learn in “Strategies for Reducing Hallucinations”?
Implement methods to minimize factual inaccuracies and fabricated information generated by LLMs. You practise AI Prompt Engineering with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start AI Prompt Engineering?
No prior experience is required. AI Prompt Engineering on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Strategies for Reducing Hallucinations” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this AI Prompt Engineering lesson?
Yes. Every AI Prompt Engineering lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Identifying and Reducing Bias
- Strategies for Reducing Hallucinations
- Ethical Considerations in Prompting