Most hallucination testing guides start with "install DeepEval, set your OpenAI key, pick a threshold." That is a fine place to end up. It is a poor place to start, because on day one you do not yet know what kind of wrong answers your app gives, and every judge call costs money.
So this post goes the other way round. Below are two checks that need no API key and no judge model: a grounding check for numbers and a self-consistency check. Both are plain Python and pytest. I ran them before writing this (Python 3.13, pytest 9.2.0): 4 passed. They will not catch every hallucination. They will catch two of the most common and most embarrassing ones, and they tell you where a judge model is worth paying for.
The failure we are testing for
There are three kinds of hallucination worth separating:
- Factual: the model states something false about the world ("the Eiffel Tower was finished in 1820").
- Contextual: the model contradicts the documents you gave it. Your policy says refunds within 5 days; the bot says 14.
- Citation: the model invents a source, a study or a link.
For a support bot or a RAG app, contextual hallucinations are the expensive ones. A customer who is told "14 days" will hold you to it. The good news: a large share of them involve numbers (days, prices, limits, versions), and numbers are easy to check without an LLM.
Check 1: every number in the answer must appear in the context
The idea is simple. Pull every number out of the answer and out of the retrieved context. Any number the answer states that the context never mentions is suspicious.
import re def numbers_in(text): """Every number in a string, e.g. '5', '14', '24'.""" return set(re.findall(r"\d+(?:\.\d+)?", text)) def ungrounded_numbers(answer, context): """Numbers the answer states that never appear in the context.""" return numbers_in(answer) - numbers_in(context) CONTEXT = "Refunds are accepted within 5 days of delivery. Shipping takes 3 to 7 days." def test_grounded_answer_passes(): answer = "You can ask for a refund within 5 days of delivery." assert ungrounded_numbers(answer, CONTEXT) == set() def test_contextual_hallucination_is_caught(): answer = "You can ask for a refund within 14 days of delivery." assert ungrounded_numbers(answer, CONTEXT) == {"14"} In a real suite you would call your app instead of hard-coding the answer, and assert that ungrounded_numbers(...) is empty.
Be honest about the limits. It misses "five" written as a word. It misses a number that exists in the context but is attached to the wrong thing (answering "3 days" for refunds when 3 belongs to shipping). It will flag harmless numbers like "24/7". Treat a failure as "a human or a judge should look at this", not as proof.
Check 2: ask the same question five times
A model that knows a fact tends to give the same fact every time. A model that is guessing drifts. Self-consistency testing uses that: run the same prompt several times at a normal temperature, extract the fact you care about, and measure how often the runs agree.
The trick that makes this work is comparing the extracted fact, not the raw text. Five correct answers can be worded five different ways.
import re from collections import Counter def agreement(answers, extract): """Share of answers that agree with the most common extracted fact.""" facts = [extract(a) for a in answers] top, count = Counter(facts).most_common(1)[0] return top, count / len(facts) def year(text): m = re.search(r"\b(1[89]\d\d|20\d\d)\b", text) return m.group(1) if m else None def test_self_consistency_stable(): runs = ["Completed in 1889.", "It opened in 1889.", "1889", "The tower was finished in 1889.", "In 1887 work began; done 1889."] fact, share = agreement(runs, year) assert fact == "1889" and share >= 0.8 def test_self_consistency_unstable(): runs = ["1889", "1820", "1889", "1901", "1820"] fact, share = agreement(runs, year) assert share < 0.8 # 2 of 5 agree = 0.4, so the gate fails Notice the fifth "stable" answer mentions two years. The regex takes the first one it finds, so that run reads as 1887, and the stable case still passes at 4 of 5 (0.8). That is exactly the kind of extractor bug you only find by running the test, and a good reminder to keep extractors narrow.
The 80% gate is a starting point, not a standard. Pick yours by running the check on questions where you already know the answer.
Where these checks stop, and judge models start
Both checks are cheap enough to run on every commit. Neither one understands meaning. They cannot tell you that "you have a week to return it" contradicts "5 days", or that a long answer quietly adds a policy that does not exist.
That is the job of LLM-as-a-judge metrics:
- Faithfulness (DeepEval, Ragas): is every claim in the answer supported by the retrieved context?
- HallucinationMetric (DeepEval): how much of the provided context does the answer agree with? One gotcha from our own checks against DeepEval 4.3.3: in 4.x this score is higher-is-better, and the metric passes when the score is at or above the threshold. Old tutorials use thresholds like 0.1 or 0.2, which would pass almost anything in 4.x.
- Answer relevancy: does the answer address the question at all?
A sensible order for a new project:
- Start with the two free checks above on a small golden dataset of real questions, including some trap questions where the right answer is "I don't know".
- Add a judge-based faithfulness metric on a 10 to 20 case smoke suite with a cheap judge model, and read your provider's usage page to see the real cost before you scale up.
- Run the full judge suite before releases, and sample a small slice of production traffic for review.
One more tip from experience: do not reach for BLEU or ROUGE as a hallucination metric. They measure word overlap. An answer that copies the reference but changes "5" to "14" overlaps almost perfectly.
The full guide
This post covers the cheapest first step. The full article on the site walks through all seven methods (DeepEval faithfulness, Ragas for RAG pipelines, adversarial prompts, self-consistency, BERTScore, temperature sensitivity and human spot checks), with a golden dataset template and a GitHub Actions quality gate:
Hallucination Testing: 7 Methods and Best Tools for LLMs
If you have found a cheap check that catches hallucinations in your own app, I would like to hear about it in the comments.
