Hallucination and evaluating LLMs

Ask a language model something it does not know, and it still answers in smooth sentences. You cannot tell a wrong answer from a right one by its tone. Move a threshold that turns low-confidence answers into "I don't know", then work out how many test questions you need before a score difference means anything.

Short answer. A hallucination is when a model makes up something that sounds right but is wrong. It happens because the model learned to pick "the words likely to come next", not to stop when it does not know. You reduce it (you cannot remove it) by giving source documents, asking for citations, letting the model say "I don't know" when its confidence is low, and checking answers in a separate step. You measure how much it helped by running a fixed test set every time you change something. Small test sets give shaky scores, so check that a difference is well above the standard error √(p(1 − p) ÷ n).

The 20 questions come from a random number generator with a fixed seed, so they are the same every time you open the page. Each has a confidence (0.3 to 0.95), and whether its answer is right was drawn with that same chance (confidence 0.8 means right 80% of the time). This is a small toy, not a real model. A real model's confidence may not match like this (calibration). Drag across the picture, or use the slider or the number box.

Definitions

The text below explains with pictures and labs. These are the textbook definitions.

hallucination
A language model giving content that is not true, or not in the sources it was given, in the same confident tone as a right answer. Going against the sources it was given is a faithfulness problem, and going against facts about the world is a factuality problem.
exact match (EM)
A score that is 1 if the answer is exactly the same text as the right answer (the reference) after case, spaces, punctuation and so on are evened out by set rules, and 0 otherwise. Over an eval set, the average is used.
token F1
Split the answer and the right answer into words and count the words they share. From that, compute precision P and recall R, and take their harmonic mean. With a words in the answer, g words in the right answer, and c shared words:

P=ca,R=cg,F1=2PRP+R

LLM-as-a-judge
A way to evaluate where another LLM is given the question, the answer, and (if there is one) the right answer and a scoring guide, and is asked to give a score or to pick the better of two answers.
standard error (SE)
How much an estimate (here, the share of right answers measured on an eval set) moves from sample to sample, that is, the standard deviation of that estimator. If the n questions are treated as independent and each is answered right with probability p, the standard error of the share of right answers is:

SE=p(1−p)n

data contamination
When the questions or answers used for evaluation end up in a model's training data, so the model can get them right from memory instead of skill. The evaluation score then comes out higher than it really is.

1. What a hallucination is

A hallucination is when a language model gives you something false, or something its sources do not say, in the same confident tone as a true answer. Common examples: a paper title and authors that do not exist, a function or option that does not exist, a number that is not in the original text. The model gives no separate sign that it is wrong.

Two kinds are worth telling apart, because you catch them differently. Not matching the source means saying something the documents or the conversation you gave it do not say, or say the opposite of (a summary with a number that is not in the original). You catch it by comparing with the source. Not matching the world means making up a false fact when there was no source document. You need an outside right answer to catch it.

2. Why it happens

3. How to reduce it

Try this. ① At the start (threshold 0) all 20 are answered: 12 right, 8 wrong. Right among the answered is 60%. ② Move the threshold to 0.6. Only 13 are answered (65%) and wrong drops to 3. Of the 7 turned into "I don't know", 5 would have been wrong and 2 would have been right. Right among the answered is 77%. ③ At 0.8 only 5 are answered (25%), and one wrong answer is still left. High confidence can still be wrong. ④ Watch the scores in the last line. Counting only right answers, threshold 0 wins with 12. Counting each wrong answer as −1, threshold 0.6 wins with 7. The scoring rule decides how far the model should go before it stops answering.

4. How to measure it

There are four main ways to turn "does it answer well?" into a number: match the text against a right answer, check it with a program, have an LLM grade it, or have people look at it.

Matching the text

Exact match checks whether the answer is the same text as the right answer, usually after fixing case, spaces and punctuation. It works when the answer is short and has one form (a number, a name, a multiple choice letter). Overlap scores count how many words the answer shares with the right answer. Word overlap F1, BLEU (from machine translation) and ROUGE (from summaries) are in this family. They are fast, free and always give the same score, but they do not look at meaning. A right answer worded differently loses points, and a wrong answer that shares words gets points (section 5).

Checking with a program

When the answer has a fixed form, check it in code. Run generated code against tests, validate JSON against a schema, compare a computed answer as a number. Exact and free, but only for tasks with a fixed form.

Using an LLM as a judge

Give another LLM the question, the answer, the right answer if you have one, and a scoring guide, and ask for a score or for the better of two answers. It is much cheaper and faster than people and looks at meaning to a degree. But judges make mistakes and lean. The LLM-as-a-judge paper (sources) reported a lean toward the answer in a certain position (first or second) when comparing two, a lean toward longer answers, and a lean toward answers the judge model wrote itself. So ask twice with the order swapped, give a concrete scoring guide, and first compare the judge with a few dozen answers people scored to see how often they agree. A judge cannot check a fact it does not know either.

Human review

People score answers with a guide (is it right, is it in the sources, is it useful), or pick the better of two. This sees meaning and usefulness best, but it is slow and costly, and people disagree too. Have two people score the same answers separately and see how often they agree. That tells you whether the guide is clear.

5. Work it by hand: word overlap F1

The right answer is "the cat sat on the mat" (6 words). Answer A, "a cat was sitting on the mat", means the same. Answer B, "the cat never sat on the mat", means the opposite. Both score 0 on exact match.

  1. Split into words. Split on spaces. A has 7 words, B has 7.
  2. Count shared words. Count each word as many times as it appears in both. A shares "cat", "on", "the" (once, since A has one "the") and "mat": 4. B shares "the" twice, "cat", "sat", "on", "mat": 6. "sitting" does not match "sat".
  3. Precision and recall. Precision = shared ÷ words in the answer, recall = shared ÷ words in the right answer. A: 4 ÷ 7 and 4 ÷ 6. B: 6 ÷ 7 and 6 ÷ 6 (precision and recall).
  4. F1 = 2 × precision × recall ÷ (precision + recall). A: 2 × (4/7) × (4/6) ÷ (4/7 + 4/6) = 32/52 ≈ 0.62. B: 2 × (6/7) × 1 ÷ (6/7 + 1) = 12/13 ≈ 0.92.
AnswerMeaningWordsSharedPrecisionRecallF1
A a cat was sitting on the matsame740.570.670.62
B the cat never sat on the matopposite760.8610.92

B, which means the opposite, scores higher. An overlap score only counts words, so one "never" can flip the meaning without it noticing.

6. Which one to use when

MethodGoodBadUse it when
Exact matchFast, free, always the same score, easy to explainCalls a right answer wrong if it is worded differentlyThe answer is short and has one form: numbers, names, multiple choice, labels
Overlap scores (F1, BLEU, ROUGE)Fast, free, gives partial creditIgnores meaning. One "not" can flip the meaning and still score highYou have a reference sentence and wording will not vary much. Quick checks for big changes
Checking with a programExact, free, can run on every changeOnly for tasks with a fixed formCode generation (run the tests), JSON and formats, computed answers
LLM judgeCheap and fast, looks at meaning, can grade from a guide without a reference answerMakes mistakes and leans (position, length, its own answers). A different judge gives different scoresLong answers, summaries, chat quality. After checking it against some human scores
Human reviewBest at meaning and usefulnessSlow and costly. People disagree tooSetting the first standard, checking an LLM judge, the last check before release

Most teams mix them. The test you run on every change uses exact match, program checks and an LLM judge because they are cheap, and people check that the judge is right and make the final call.

7. How the formula is built: few questions, shaky scores

A score from a test set (the share answered right) also moves by chance. With the same skill, the score goes up or down depending on which questions happened to be in the set. The size of that wobble is the standard error √(p(1 − p) ÷ n). p is the chance of getting a question right (the skill) and n is the number of questions. Step through it, and switch n between 20, 100 and 1000.

The green bars are the scores a model with a 0.7 chance per question would get if it took an n-question test many, many times (computed from the binomial distribution, heights scaled to the tallest bar). In step ④ the blue bars are a model with a 0.75 chance.

  1. One question: p(1 − p). It scores 1 if right, 0 if wrong. With p = 0.7 the mean is 0.7 and the variance is 0.7 × 0.3² + 0.3 × 0.7² = 0.21. In letters that is p(1 − p)² + (1 − p)p² = p(1 − p).
  2. The average of n questions: p(1 − p) ÷ n. If each question goes up and down on its own, the variance of the sum is n times bigger. The score is the sum divided by n, and dividing by n divides the variance by n². n times ÷ n² = ÷ n.
  3. Square root: SE = √(p(1 − p) ÷ n). Variance is a squared value. To get back to the same unit as the score (percentage points), take the square root. That is the standard error (SE).
  4. Comparing: p ± 2 × √(p(1 − p) ÷ n). With many questions the score's distribution gets close to a normal distribution, and about 95% of it falls within ±2 standard errors (confidence interval). With few questions, or p close to 0 or 1, this shortcut gets rough.
nvariance 0.21 ÷ nstandard error70% ± 2 SE75% ± 2 SESE of the gap5 points ÷ SE of the gap
200.01050.102 (10.2 points)49.5% to 90.5%55.6% to 94.4%14.1 points0.4
1000.00210.0458 (4.6 points)60.8% to 79.2%66.3% to 83.7%6.3 points0.8
10000.000210.0145 (1.4 points)67.1% to 72.9%72.3% to 77.7%2 points2.5

With 20 questions, 70% versus 75% is not a difference you can claim. The range for 70%, 49.5% to 90.5%, easily holds 75%. If one score sits inside the other's ±2 standard error range, do not call it a difference. If it sits outside, compare the gap with the standard error of the gap itself. If the two scores move on their own, that is the square root of the two standard errors squared and added. If you test both models on the same questions, easy and hard questions move both scores together, and the gap can wobble less than this.

Try this. ① With n = 20, press "Next" three times. 70% gives 49.5% to 90.5%, 75% gives 55.6% to 94.4%, and the two groups of bars almost cover each other. ② Press n = 100. The range for 70% narrows to 60.8% to 79.2%, but 75% is still inside. ③ With n = 1000 it is 67.1% to 72.9%, so 75% is outside, and 5 points is 2.5 times the gap's standard error of 2 points. ④ On step ③, switch n from 20 to 1000. One standard error goes from 10.2 points to 1.4 points. With 50 times the questions, the wobble shrinks only √50 ≈ 7.1 times.

8. Fix the test set and run it on every change

When test data leaks into training

Public test questions and their answers are on the web, and large models are trained on web text. If a model saw the test questions during training, it can answer from memory rather than skill, and the score comes out higher than it should. This is called data contamination. The same thing can happen in your own hands. If you tune the prompt while looking at the test set and then score it on that same set, you get a prompt fitted to that set.

9. Key properties

PropertyIn plain wordsIn the labs
You cannot tell by the toneWrong answers are smooth and confident too. Check against something outside (documents, code, a right answer)The cells look the same until you score them
You can reduce it, not reach zeroSource documents, "I don't know" and checking each stop only some of itStill 1 wrong at threshold 0.8
"I don't know" is a tradeMore "I don't know" means fewer wrong answers and fewer answers. Some right answers go tooThe threshold bar in lab ①
The scoring rule shapes behaviorCount only right answers and guessing wins. Penalize wrong answers and holding back when unsure winsThe last line of lab ①
Scores tighten only with √nYou need 4 times the questions to halve the wobbleThe n buttons in lab ②

Where it is used

Frequently asked questions

Does temperature 0 stop hallucinations?

No. A lower temperature only means less likely tokens get picked less often. If the answer the model rates as most likely is wrong, it gives that same wrong answer every time. What you gain is less wobble, so test runs are easier to compare.

Do source documents (RAG) remove hallucinations?

They reduce them but do not remove them. The documents found may be wrong or off topic, and even with the right document the model can add things the document does not say. Measure finding (did the right document come back?) and answering (did the answer stick to it?) separately.

Can I trust scores from an LLM judge?

Only as far as you have checked it. First compare the judge with a few dozen answers that people scored and see how often they agree. When it compares two answers, ask twice with the order swapped, and give it a concrete scoring guide. Judges make mistakes too, and tend to lean toward longer answers or answers in a certain position.

Words

Word used hereWhat books call it
making up something that sounds right but is wronghallucination
not matching the source · not matching the worldfaithfulness problem · factuality problem
finding source documents and putting them inretrieval-augmented generation (RAG)
confidence matches how often it is rightcalibration
saying "I don't know"abstention, selective prediction
share of questions answeredcoverage
same text as the right answerexact match (EM)
word overlap scorestoken F1, n-gram overlap (BLEU, ROUGE)
right answer textreference
having an LLM grade answersLLM-as-a-judge
judge leans (position, length, its own answers)position bias, verbosity bias, self-enhancement bias
fixed test seteval set, benchmark
test you run on every changeregression test
test questions mixed into trainingdata contamination, leakage
how much a score wobblesstandard error (SE)

Sources

← Fine-tuning and LoRARAG →