…
…
AI Fundamentals · Lesson 19 / 31 · Language models
PreviewIn this lesson you will
You can read the explanation without an account. The exercises and quiz at the end of the lesson are for members.
The assistant's trial week is over. Elena's manager looks at the log and asks: "Why does this assistant say wrong things? Is it broken?" Elena puts eight faulty answers in front of you and asks for a short note to the manager: the types of errors, their causes, and how to catch them.
In lesson 4 you learned to accept, fix, or reject the assistant's answers. Now take a step back and answer a harder question: why does the assistant write these mistakes? If you know the cause, you also know where to look for the mistake.
At the end of this lesson you will have a hallucination hunt table. Next to each faulty answer you will write the number of the faulty sentence, the type of error, its cause, and your decision. For now the table is empty; we will fill it in during the lesson. The remaining four answers are waiting for you in the exercises.
| Answer | Student's question | Faulty sentence | Type | Cause | Decision |
|---|---|---|---|---|---|
| 1 | Can guests stay overnight in the dorm? | ? | ? | ? | ? |
| 2 | How many courses can I take in a semester? | ? | ? | ? | ? |
| 3 | How much is the scholarship in total for a year? | ? | ? | ? | ? |
| 4 | How do I get an enrollment letter? | ? | ? | ? | ? |
The document you will compare against is still the Student Handbook. The lines you will use in this lesson are collected below. The Handbook and all of the assistant's answers are fictional examples written for this lesson; the method works the same way in real life.
| Topic | Student Handbook |
|---|---|
| Scholarship | The scholarship is $150 a month and is paid for 9 months a year. Scholarship applications close on October 3. |
| Scholarship payment | Scholarship payments are made on the 15th of each month. |
| Dorm application | Dorm applications close on September 12. |
| Dorm fee | Block A (double room) costs $210 a month; Block B (four-person room) costs $145 a month. |
| Dorm rule | The dorm curfew is 23:00; guests may not stay overnight in the dorm. |
| Course load | Students can take at most 8 courses per semester. |
| Course add/drop | Course add/drop happens in the first two weeks of the semester, with advisor approval. |
| Absence limit | The absence limit is 30% of class hours. |
| Enrollment renewal | Enrollment renewal happens in the two weeks before the semester starts. |
| Enrollment letter | Enrollment letters can be downloaded instantly and free of charge from the student information system. |
When the assistant writes information that is false or does not exist, and writes it fluently and with confidence as if it were true, we call this hallucination. The word may sound like a problem of perception, and the model has no perception; it is simply the established term for this behavior, so we use it.
In this lesson you will:
We can answer the manager's question by combining two pieces from the two previous lessons. In lesson 17 you saw that a language model writes text word by word and picks the next word by probability at each step. In lesson 18 you saw that the model carries its knowledge as patterns left over from the texts it read in pretraining; it does not store books like a shelf. If a piece of information never appeared in the training texts, or appeared once or twice and got lost, the model cannot recall it reliably.
Now put the two together. The model always writes some continuation for every question, because that is the only thing it does. Leaving a gap with "I don't know" is a pattern that is rare in the texts it read; "applications close on … " is a very common one. On top of that there is the adaptation stage: as you saw in lesson 18, when the model is adapted for chat it learns to speak politely, fluently, and with confidence. The result is this: a correct answer and a wrong answer come from the same mechanism, in the same style. The model does not look for the truth; it writes the continuation that fits the context best. "Sounds right" and "is right" often overlap, but not always.
Let's start with a familiar example.
Example 1 (case, toy): Dorm date. As you saw in lesson 4, "September 20" appears very often in the texts the assistant read, while the Handbook says dorm applications close on September 12. An assistant that cannot see the Handbook writes "Dorm applications definitely close on September 20." This answer is not the result of a fault; it is the result of choosing the most frequent continuation.
Example 2 (case): A question the Handbook never covers. A student asks, "When does the club application close?" The Handbook has not a single line about clubs. The assistant again writes a date without hesitating:
Fictional assistant output (prompt: "When does the club application close?"):
OutputClub applications definitely close on September 30.Where did this date come from? Put the steps below in the order they happen.
Put the steps in order to show how the assistant wrote a club date that does not exist.
Click the lines in order; to undo one you placed wrong, click it in the list below.
Shuffled lines
Your order
Let's try this chain on a small toy. The program below imitates two states of the assistant. topic is the subject asked about, and book_in_context is whether the Handbook was given in the prompt (the context window from lesson 18). If the Handbook is in context and the topic appears in it, the assistant writes from it; if the Handbook is in context but the topic is missing, it says "no information"; if the Handbook is not in context, it writes the most common date. You do not need to read the code line by line: change the two values and look at the last two lines.
First change the topic, then change whether the Handbook is in context. In which situations does the assistant confidently write wrong or nonexistent information?
Pick a value; predict the output first, then look.
1topic = "dorm"2book_in_context = False34handbook = {"dorm": "September 12", "scholarship": "October 3"}5most_common = {"dorm": "September 20", "scholarship": "October 15", "club": "September 30"}67if book_in_context and topic in handbook:8 print("Assistant: the", topic, "application closes on", handbook[topic] + ".")9elif book_in_context:10 print("Assistant: the Handbook has no information on this.")11else:12 print("Assistant: the", topic, "application definitely closes on", most_common[topic] + ".")13print("Handbook:", handbook.get(topic, "no information"))
Assistant: the dorm application definitely closes on September 20. Handbook: September 12
Faded lines didn't run with this value. When the Handbook is not in context, the assistant says "definitely" for both topics: a wrong date for the dorm (September 20) and a date that is nowhere in the Handbook for the club (September 30). Same code, same style; the only difference is whether the asked information is at hand. When the Handbook enters the context, the dorm date is fixed and for the club it says "no information". In the toy the error disappears; in a real assistant, giving the document reduces the error but does not remove it.
Look at all four combinations. While the Handbook was not in context, the assistant wrote the same confident "definitely" for both topics: one was a wrong date, the other information that does not exist. Once you gave it the document, the dorm date was fixed and it said "no information" for the club. In this toy, giving the document removed the error. In a real assistant, as you saw in lesson 18, giving the document reduces the risk but does not bring it to zero. The model can also take the wrong line while reading the document or mix up two lines.
Notice one more thing: the same code produced the correct answer and the wrong answer. There is no "lie" branch inside the assistant, and there is no "break down" branch either.
The assistant confidently wrote a club date that does not exist. Which explanation is best?
Here is the answer to the manager's "Is it broken?": no. Hallucination is a side effect of the model's normal way of working. It will not be fixed once and then never happen again, so you need to know how to catch it. To catch mistakes, first learn to tell them apart: there are four types of hallucination.
A fabricated source is an answer that rests on a book, document, article, page, or link that does not exist. Why does it happen? A source name and a page number are just word sequences to the model. It has seen the pattern of citing a source many times, as in "according to page 42 of the … Guide", in official texts, and it may write this pattern as a probable continuation. It has no step that checks whether the source really exists.
Example 3 (case): Guest rule.
Fictional assistant output (prompt: "Can guests stay overnight in the dorm?"):
Output1. According to page 42 of the Campus Life Guide, guests may stay one night on weekends.
2. To bring a guest, it is enough to tell the attendant at the entrance gate.We numbered the sentences because when you mark errors you need to say which sentence. There is no document called "Campus Life Guide" at the school; the only source is the Handbook, and it clearly says "guests may not stay overnight in the dorm." The page number gives the answer an official look, but it is not evidence. The decision is yours.
Answer 1 says that, "according to page 42 of the Campus Life Guide", a guest can stay on weekends. Handbook: "The dorm curfew is 23:00; guests may not stay overnight in the dorm." What do you do?
Pick an option and see what happens. There are no points; you can try the other options too.
A fabricated fact is writing a wrong date, fee, number, or rule with confidence and without citing a source. The cause is the same: the information is either not in training or the numbers of other schools appear more often.
Example 4 (case): Course load.
Fictional assistant output (prompt: "How many courses can I take in a semester?"):
Output1. You can definitely take at most 10 courses.
2. When you build your schedule, make sure not to exceed this limit.Handbook: at most 8 courses per semester. Sentence 1 is a fabricated fact; the word "definitely" does not make the claim stronger, it only makes it look more sure. There is no fabricated basis here, just a wrong number; so the decision is fix: change the number to 8, remove the needless "definitely", and keep sentence 2, which is correct.
The two types are caught in different ways. For a fabricated source you open the source: does that document and that page really exist in the catalog, the official site, or the Handbook? For a fabricated fact you compare the number or date with the Handbook line. Keeping the two apart also decides the decision: an answer with a fabricated source is rejected, because it has no basis; an answer with a wrong fact is fixed, because only one piece is wrong.
We will call these parts of an answer that you need to look at hallucination marks: a source name, an article or page number, numbers and calculations, dates and fees, absolute language ("definitely", "always", "everyone"), and a contradiction inside the same text. One important distinction: a mark is not proof of hallucination, it is a place to look. A date you highlight with a marker may turn out correct; but if you skip it without marking, you cannot notice when it is wrong.
Match each mark with how you check it when you see it.
For each row, pick its match from the list, then check.
The third type is about numbers, and it is the most surprising, because we think a computer will not make mistakes in arithmetic. A calculator really does not make mistakes, because it processes numbers. A language model does not process numbers like a calculator: when it sees the text "150 × 9 =", it writes the number sequence that is most likely to come next. For short and very common calculations (like 2 × 5) that sequence is almost always right; for long, rarely seen calculations it is often wrong.
Example 5 (case): Scholarship yearly total. The answer you saw in lesson 4:
Fictional assistant output (prompt: "How much is the scholarship in total for a year?"):
Output1. The scholarship is $150 a month and is paid for 9 months a year.
2. Yearly total: $150 × 9 months = $1,200.Sentence 1 is exactly the same as the Handbook. In sentence 2 the inputs are right and the result is wrong: 150 × 9 = 1,350. The same thing happens outside school: when you ask an assistant "What is 17 × 23?" it may write "381"; the correct answer is 391. Both results sound plausible, so you cannot catch them by eye. The only way to catch it is to do the calculation yourself (or with a calculator).
In the same answer the assistant says "the scholarship is $150 a month, paid for 9 months" and writes the total as $1,200. What do you do?
Two misunderstandings can be corrected here at once. "If the calculation is wrong, everything is wrong" is not true: this answer has two correct pieces of information. "It is a computer, so it does the calculation right" is not true either: a language model does not calculate, it predicts. For a wrong calculation the decision is usually fix: change only the result.
The fourth type shows up in long answers. The model builds text sentence by sentence; each new sentence is a probable continuation of the text before it. Each sentence is fluent on its own, but there is no separate step that checks the whole text from start to finish. So information at the start and information at the end can contradict each other. We will call this inconsistency: two sentences in the same answer that contradict each other.
Example 6 (case): Enrollment letter.
Fictional assistant output (prompt: "How do I get an enrollment letter?"):
Output1. The enrollment letter is downloaded instantly from the student information system.
2. The letter is free.
3. You use your student number to log in.
4. You can pick up your letter from Student Services three business days after applying.While you read the first three sentences everything looks fine. Because the answer starts well, it is easy to assume the ending is right too; but read to the end.
Which two of the four sentences in Example 6 contradict each other?
The way to catch an inconsistency is to put the sentences that contain numbers, dates, durations, and yes/no claims side by side. When you find a contradiction, the Handbook decides which one is right. The decision is usually fix: the sentence that matches the Handbook stays and the other is removed or corrected.
Inconsistency does not happen only at the help desk.
Example 7 (outside the case): History homework summary. A student asks for a five-sentence summary of the printing press's effect on Europe for homework.
Fictional assistant output (prompt: "Summarize the printing press's effect in Europe in 5 sentences for my homework."):
Output1. The printing press made it possible to print books with movable type instead of copying them by hand.
2. The printing press spread across Europe in the middle of the 15th century.
3. Printed books could be produced much more cheaply than handwritten ones.
4. As a result, the number of people who could read and write grew over time.
5. Because of this, books in Europe were already printed on presses in the 1300s.Sentence 2 says the middle of the 15th century; the 1300s are the 14th century, before the press spread. Sentence 5 starts with "because of this" and looks like a result, but it throws the timeline backwards. A student who copies this summary into their homework would contradict themselves in their own text.
Let's put the four types side by side.
| Type | Mark to look for | How to catch it | Usual decision |
|---|---|---|---|
| Fabricated source | Source name, article or page number, link | Open the source: catalog, official page, Handbook | Reject |
| Fabricated fact | Date, fee, number, rule, absolute language | Compare with the Handbook line | Fix |
| Wrong calculation | "×", "=", total, percentage | Do the calculation yourself | Fix (only the result) |
| Inconsistency | Two numbers, two durations, yes and no in the same answer | Read the sentences side by side | Fix (the correct sentence stays) |
Let's look at one example from outside school too; the first row of the matching below comes from this one.
Example 8 (outside the case): Training advice.
Fictional assistant output (prompt: "I'm preparing for a 10 km run; how many hours a day should I run?"):
Output1. According to a study in issue 2 of the Endurance Running Bulletin, everyone preparing for a 10 km run should run 3 hours a day.
2. If this time is shorter, the preparation is insufficient.This bulletin is fiction; you would not find it in a catalog. On top of that, "everyone, 3 hours a day" is an exaggerated generalization that sets a single rule for everybody. Both marks are in sentence 1: a source name and absolute language.
Match each short output with its hallucination type.
For each row, pick its match from the list, then check.
One more thing to add. Elena will count how many answers were faulty in the trial week; you will calculate this hallucination rate in the unit task. But do not draw a conclusion from a single answer: one correct answer does not show the assistant is reliable, and one wrong answer does not show it is completely useless. Since you cannot know in advance which answer will be faulty, every answer that carries a mark goes through the same check.
Imagine that the answer in Example 3 went out without being checked. The student brings a guest on the weekend, the dorm attendant stops them at the door, and the student says, "But the help desk said it is on page 42." In the attendant's eyes, the help desk gave the wrong information, not the assistant. "The assistant said so" is not a reason; the desk that sent the answer is responsible.
The most common mistake here is relaxing when you see a source name. "It rests on a guide and a page, so it must have looked it up" is a strong feeling. But you saw that citing a source is also a pattern the model learned. A source name does not finish the check; it starts it.
The note for the manager is now ready: "The assistant is not broken; it writes the most probable continuation without looking for the truth. Any answer that contains a date, fee, source, or calculation is not sent before it is compared with the Handbook."
The table at the start of the lesson is filled in too:
| Answer | Student's question | Faulty sentence | Type | Cause | Decision |
|---|---|---|---|---|---|
| 1 | Can guests stay overnight in the dorm? | 1 | Fabricated source | Wrote the pattern of citing a source; the guide does not exist | Reject |
| 2 | How many courses can I take in a semester? | 1 | Fabricated fact | Information not in training, wrote a probable number | Fix: 8 courses |
| 3 | How much is the scholarship in total for a year? | 2 | Wrong calculation | The model does not calculate, it predicts the result | Fix: $1,350 |
| 4 | How do I get an enrollment letter? | 4 | Inconsistency | The whole text is not checked | Fix: remove sentence 4 |
Two calculations are asked of the assistant.
Fictional assistant output (two separate prompts: "What is 6 × 7?" and "What is 17 × 23?"):
Output6 × 7 = 42
17 × 23 = 381The first is correct. The correct answer to the second is 391 (17 × 20 = 340, 17 × 3 = 51, total 391). A short and very common product appears many times in texts, so the most probable continuation is the right number; for a rarely seen product the most probable continuation can be wrong. The decision for both rests on the calculation: you accept the first and fix the second.
The sent versions of the two answers you decided to fix. Only the faulty part changed.
OutputAnswer 2: You can take at most 8 courses. When you build your schedule, make sure not to exceed this limit.
Answer 4: The enrollment letter is downloaded instantly from the student information system. The letter is free.
You use your student number to log in.In Answer 2, "definitely" was also removed: the number comes from the Handbook, so it does not need extra emphasis. In Answer 4, the contradicting sentence 4 was removed; sentence 1, which matches the Handbook, stayed.
This time Elena pastes the dorm section of the Handbook into the prompt and asks the assistant: "How much is a four-person room per month?"
Fictional assistant output:
Output1. According to the Handbook, four-person rooms are in Block B.
2. The monthly fee is $210.Sentence 1 is right, but sentence 2 gives the fee of Block A; Block B costs $145 a month. The document was in context, and the model still mixed up two lines. As you saw in lesson 18, giving the document reduces the risk and does not remove it. This is a fabricated fact; the decision is fix: "The monthly fee is $145."
Treating a source name, an article number, or a good start as a sign of truth. The thoughts "it rests on a guide", "the first sentences are right", and "it writes so confidently, so it must know" all come from the same misunderstanding: believing that the model looks for the truth. The model writes the continuation that fits the context best; a source name, confident language, and a good start are part of that continuation. The right way: find the marks, test each one against the source, the calculation, or the other sentences, and only then make the decision.
Next lesson: Hallucination in text works like this. What happens with images and audio? Since we trust our eyes and ears more, fake content looks more convincing there; in the next lesson we will cover this.
Exercises and quiz are for members
This lesson has 7 coding exercises and a 10-question quiz. Sign up free to solve them, save your progress and move on to the next lesson.