How to stop LLM hallucinations with evidence
Practical ways to prevent LLM hallucinations when extracting or classifying data. Make the model quote its evidence, check the quote in code, and reject what fails.
You can’t stop a language model from ever making things up. You can build a system where a made-up answer doesn’t survive long enough to reach anyone. That is a more useful goal, and for extraction and classification tasks it is very achievable.
The core idea is simple: never accept a claim from the model without evidence, and check the evidence with ordinary code. I used this pattern in Morpheus, a lead generation engine where every lead has to survive a sales conversation, and it is the design rule behind Raven, an SEO agent that can only report what it can cite.
Why models hallucinate in these tasks
When you ask a model to read a job post and say what problem the company has, it will always give you an answer, because producing a plausible answer is what it is trained to do. If the post is vague, the answer is a guess dressed as a fact. Turning the temperature down to zero makes the guess consistent. It doesn’t make it true.
So the fix isn’t a better prompt alone. It’s a system around the model that separates what the source says from what the model inferred.
1. Ask for a verbatim quote with every claim
Make the evidence a required field. If the model says a company has a problem, it must copy the exact sentence that shows it.
{
"problem": "manual reporting",
"evidence": "Our weekly reports are currently built by hand in spreadsheets.",
"score": 8
}
If the model can’t quote the problem, it hasn’t found one. That single rule removes most invented claims, because the model now has to point at something rather than describe it.
2. Check the quote in code
A quote field only helps if you check it. The model can invent a quote as easily as a claim, so confirm it actually appears in the source:
import re
from pydantic import BaseModel, Field, ValidationError
class Finding(BaseModel):
problem: str | None
evidence: str | None # must be copied word for word from the source
score: int = Field(ge=1, le=10)
def normalise(text: str) -> str:
return re.sub(r"\s+", " ", text).strip().lower()
def accept(raw_reply: str, source: str) -> Finding | None:
try:
finding = Finding.model_validate_json(raw_reply)
except ValidationError:
return None # wrong shape: reject it, don't repair it
if finding.problem and not finding.evidence:
return None # a claim with no proof
if finding.evidence and normalise(finding.evidence) not in normalise(source):
return None # the quote isn't in the source, so treat it as invented
return finding
This is the step I’d add first to any extraction pipeline. In Morpheus the quote was required but not yet checked in code, which left the door open to a fluent invented sentence.
3. Use structured output and reject bad replies
Ask for JSON that matches a schema, validate it, and throw away anything that doesn’t fit. Most model APIs now support a JSON or structured output mode, and libraries such as Pydantic make validation one line.
The important habit is rejecting rather than repairing. If you patch a malformed reply by guessing what the model meant, you have reintroduced the guessing you were trying to remove.
4. Make “I don’t know” a valid answer
Models fill gaps because the format demands an answer. Allow null for every field that might be missing, and say plainly in the prompt that an empty field is better than a guess. In Morpheus, the step that looked for a contact person was told to return a name only if it appeared in the search results. In one run, 30 of 48 leads came back with a name. The other 18 stayed blank, which is exactly what should happen.
5. Grade against a fixed rubric
“Rate this from 1 to 10” invites the model to make up its own scale. Define what each band means instead: a 10 needs a named problem, an active project, urgency and a clear fit; 4 or 5 is weak; 1 to 3 is noise. Apply hard rules, such as automatic disqualifiers, before the model scores anything.
6. Keep the source next to the claim
Wherever the result ends up, in a sheet, a report or an app, keep the quote and a link to the source beside it. People trust a claim they can check in one click, and they spot a wrong one immediately. Raven takes this furthest: every finding has to point at a source a tool fetched in the same run, or it is rejected before it reaches the report.
7. Log every rejection with a reason
Rejections are your best evaluation data. Record why each one failed: wrong format, missing evidence, quote not found, or below threshold. If one reason dominates, you know whether to fix the prompt, the source or the threshold. My own early logs used one generic label for every rejection, and I couldn’t tell the reasons apart. Don’t repeat that.
What this doesn’t solve
These checks work for tasks where the answer should come from a source you have: extraction, classification, summarising a document. They don’t make open-ended questions safe, where the model draws on its own memory. For those, retrieve the sources first and apply the same rule: no claim without a quote you can check.