Prompt Engineering Mastery

Course Content

Prompt Engineering Mastery

6 sections · 32 lessons

How can you fix hallucinations or inaccurate answers using prompts?


Quote first, then answerContract first,question lastCopy every relevantsentence as a quoteAnswer onlyfrom the quotesCode checkseach quote isin the sourceNo supportingquote: NOT_FOUNDWithout the escape hatch the model filled the gap with a typical 30-day notice period.
The model invents when silence is not an allowed answer; a quote requirement makes every unsupported claim visible.

What you need to know

Models hallucinate because they are trained to produce plausible text. When they lack a fact, the most plausible continuation is often a confident guess, and nothing in normal generation checks it.

Prompt techniques that help

  1. Ground in context. "Answer only from the text in <contract>." The model now has a source to copy from instead of memory.
  2. Give an escape hatch. "If the answer is not in the text, reply NOT_FOUND." Without permission to say "I don't know", the model fills the gap.
  3. Quote first, then answer. For long documents, ask the model to pull out the exact relevant sentences first, then answer using only those quotes.
  4. Require citations. A quote or source ID per claim makes unsupported claims visible — and checkable in code. Some APIs have built-in citation features that return exact source spans.
  5. Narrow the scope. "List the termination notice period" hallucinates less than "summarise all risks".
  6. Verify. A second call checks each claim against the source and flags unsupported ones.

What helps less than people think

  • Lowering temperature — it makes the top guess more consistent, not more correct.
  • "Do not hallucinate" — the model cannot tell when it is doing it; an escape hatch and grounding work better.

Outside the prompt

Retrieval (RAG) so the right documents are present, code that checks quotes appear in the source, evaluation sets that measure groundedness, and human review where errors are costly.

A real-life example

A legal-clause summariser is asked "What is the notice period for termination?" on a vendor contract that does not mention one. The original prompt:

Text
Read this contract and answer the question.Contract: <42 pages>Question: What is the notice period for termination?

The model answers "30 days' written notice" — a common value in contracts, but not in this one. The improved prompt:

Text
<contract>...42 pages...</contract>Question: What is the notice period for termination?First, copy every sentence from the contract that mentions terminationor notice, inside <quotes>. Then answer using only those quotes.If the quotes do not state a notice period, answer exactly:NOT_FOUND - no termination notice period is stated.

Now the output lists two quotes about termination for breach, neither with a period, and answers NOT_FOUND. The team's code also checks that each quote appears word for word in the contract. On 80 test questions, unsupported answers fall from 11 to 1.

Follow-up questions to expect

  • "Do reasoning models hallucinate less?" — They are often better at multi-step questions, but they still invent facts they do not have. Grounding and citations remain necessary.
  • "How do you measure hallucination?" — A groundedness metric: the share of claims supported by the source, scored by rules (quote matching) or a validated LLM judge.
  • "Where do you put the question in a long prompt?" — After the document. Models tend to follow a query at the end of long input better than one buried at the top.