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:
- 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:
- 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
- The model picks "the words likely to come next". A language model looks at the text so far, gives every possible next token a probability, and picks from them to keep writing (sampling). Training teaches it to continue human writing well. That rewards "sounds likely", and nothing in it rewards "is true" on its own. The right answer is often the most likely one, but when you ask about something it does not know, it keeps going with likely-sounding text in the shape of an answer.
- It was not taught to say "I don't know". Predicting the next word does not reward stopping when unsure. "I don't know" is an answer you have to train or ask for.
- Scoring rewards guessing. On a test that only counts right answers, "I don't know" scores 0, and a guess might be right, so guessing always scores the same or higher. In the last line of the lab above, "counting only right answers" is highest at threshold 0 (guess everything). If you pick models and prompts by that kind of score alone, the guessers win.
- You cannot tell by the tone. A smooth sentence means the text is likely, not that it is true.
3. How to reduce it
- Give source documents (RAG). Find documents related to the question, put them in with it, and ask for an answer from those documents only. Limit: if the documents found are wrong or missing, this does not help, so measure the finding step on its own (retrieval metrics). Even with the documents, the model can say something else.
- Ask for citations. If each sentence says which part of which document it came from, people and programs can check it. Limit: citations can be made up too, so check that the cited text really says it.
- Lower the temperature (sampling). Less likely tokens get picked less often, so answers wobble less. Limit: if the most likely answer is wrong, you get that same wrong answer every time.
- Let it say "I don't know". Write "if the sources do not cover it, say you don't know" in the prompt, and when confidence is below a threshold, return "I don't know" instead of the answer (the lab above). You can get a confidence by asking the same question several times and seeing how often the answers agree, from the probability of the answer tokens, or by asking the model to state one. Limit: it only helps if the confidence matches how often answers are actually right (calibration).
- Add a checking step. Check the answer after it is made. Redo math in code, run generated code against tests, compare facts with the source documents. Some systems ask a separate call "is this sentence in the document?". Limit: more calls, so slower and costlier.
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.
- Split into words. Split on spaces. A has 7 words, B has 7.
- 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".
- Precision and recall. Precision = shared ÷ words in the answer, recall = shared ÷ words in the right answer. A:
4 ÷ 7and4 ÷ 6. B:6 ÷ 7and6 ÷ 6(precision and recall). - 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.
| Answer | Meaning | Words | Shared | Precision | Recall | F1 |
|---|---|---|---|---|---|---|
| A a cat was sitting on the mat | same | 7 | 4 | 0.57 | 0.67 | 0.62 |
| B the cat never sat on the mat | opposite | 7 | 6 | 0.86 | 1 | 0.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
| Method | Good | Bad | Use it when |
|---|---|---|---|
| Exact match | Fast, free, always the same score, easy to explain | Calls a right answer wrong if it is worded differently | The answer is short and has one form: numbers, names, multiple choice, labels |
| Overlap scores (F1, BLEU, ROUGE) | Fast, free, gives partial credit | Ignores meaning. One "not" can flip the meaning and still score high | You have a reference sentence and wording will not vary much. Quick checks for big changes |
| Checking with a program | Exact, free, can run on every change | Only for tasks with a fixed form | Code generation (run the tests), JSON and formats, computed answers |
| LLM judge | Cheap and fast, looks at meaning, can grade from a guide without a reference answer | Makes mistakes and leans (position, length, its own answers). A different judge gives different scores | Long answers, summaries, chat quality. After checking it against some human scores |
| Human review | Best at meaning and usefulness | Slow and costly. People disagree too | Setting 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.
- 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 is0.7 × 0.3² + 0.3 × 0.7² = 0.21. In letters that isp(1 − p)² + (1 − p)p² = p(1 − p). - 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. - 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). - 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.
| n | variance 0.21 ÷ n | standard error | 70% ± 2 SE | 75% ± 2 SE | SE of the gap | 5 points ÷ SE of the gap |
|---|---|---|---|---|---|---|
| 20 | 0.0105 | 0.102 (10.2 points) | 49.5% to 90.5% | 55.6% to 94.4% | 14.1 points | 0.4 |
| 100 | 0.0021 | 0.0458 (4.6 points) | 60.8% to 79.2% | 66.3% to 83.7% | 6.3 points | 0.8 |
| 1000 | 0.00021 | 0.0145 (1.4 points) | 67.1% to 72.9% | 72.3% to 77.7% | 2 points | 2.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
- Keep the questions and the expected answers (or the scoring guide) fixed in one file. Make them look like the questions you really get, and include some where "I don't know" is the right answer.
- Every time you change the prompt, the model, the retrieval settings or the temperature, run the same set and compare with the last score. It is the same idea as running unit tests on every commit.
- When a real mistake shows up, add it to the set so you notice if it comes back.
- If answers change from run to run (temperature above 0), run the set several times and average.
- If the score moved only about as much as the wobble in section 7, it may be chance. Look at which questions went from right to wrong and from wrong to right.
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.
- Keep separate sets: one you look at while tuning the prompt, and one you score on once at the end (data splitting). Do not peek at the final set often.
- Do not trust public benchmark scores alone. Keep a private set gathered from your own work.
- Change questions a little (numbers, names, order of choices). If the score drops a lot, suspect memorizing.
- Questions written from material that appeared after the model's training data was collected could not have been in its training.
9. Key properties
| Property | In plain words | In the labs |
|---|---|---|
| You cannot tell by the tone | Wrong 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 zero | Source documents, "I don't know" and checking each stop only some of it | Still 1 wrong at threshold 0.8 |
| "I don't know" is a trade | More "I don't know" means fewer wrong answers and fewer answers. Some right answers go too | The threshold bar in lab ① |
| The scoring rule shapes behavior | Count only right answers and guessing wins. Penalize wrong answers and holding back when unsure wins | The last line of lab ① |
| Scores tighten only with √n | You need 4 times the questions to halve the wobble | The n buttons in lab ② |
Where it is used
- Chatbots that answer from documents (internal docs, customer support): attach source documents and citations, and say "I don't know" when the sources do not cover it. Measure finding and answering separately.
- Summaries: check that each sentence of the summary is in the original.
- Code generation: run it and run the tests.
- Agents: they take many steps, so check the final state with a program (did the file change, did the request go through) and keep a log of the steps in between.
- Deciding whether to switch a model or a prompt: same test set, enough questions, and look at the score gap next to the standard error.
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 here | What books call it |
|---|---|
| making up something that sounds right but is wrong | hallucination |
| not matching the source · not matching the world | faithfulness problem · factuality problem |
| finding source documents and putting them in | retrieval-augmented generation (RAG) |
| confidence matches how often it is right | calibration |
| saying "I don't know" | abstention, selective prediction |
| share of questions answered | coverage |
| same text as the right answer | exact match (EM) |
| word overlap scores | token F1, n-gram overlap (BLEU, ROUGE) |
| right answer text | reference |
| having an LLM grade answers | LLM-as-a-judge |
| judge leans (position, length, its own answers) | position bias, verbosity bias, self-enhancement bias |
| fixed test set | eval set, benchmark |
| test you run on every change | regression test |
| test questions mixed into training | data contamination, leakage |
| how much a score wobbles | standard error (SE) |
Sources
- Ziwei Ji et al., Survey of Hallucination in Natural Language Generation, arXiv:2202.03629. What hallucination is, its kinds, ways to reduce it.
- Lianmin Zheng et al., Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena, arXiv:2306.05685. Position, length and self leans in LLM judges, and ways to reduce them.
- Kishore Papineni et al., BLEU: a Method for Automatic Evaluation of Machine Translation. Chin-Yew Lin, ROUGE: A Package for Automatic Evaluation of Summaries. Overlap scores.
- Tom B. Brown et al., Language Models are Few-Shot Learners, arXiv:2005.14165. The section that measures whether test data was in the training data.