How does a chatbot “think”?
Chatbots like ChatGPT, Gemini or Claude write answers that sound as if a person wrote them. How do they do it? At the core is a surprisingly simple idea: predicting the next word. In this lesson you will learn how a language model is made, why it sounds convincing – and why it sometimes confidently makes mistakes.
Game: which word comes next?
Complete the sentences:
- “After the rain, out came the …”
- “Two plus two is …”
- “I'm sitting at my school …”
You probably thought quickly: sun, four, desk. Based on everything you have read and heard, your brain “knew” which word was the most likely. A language model does something similar – except that it has “read” a huge amount of text.
Predicting the next word
- The model gets the beginning of a text (your question and what it has already written).
- For every possible next word it calculates how likely it is.
- It chooses one of the likely words (not always the most likely one – that is why the same chatbot doesn't always give the same answer to the same question).
- It adds the word to the text and repeats the process – word by word – until it finishes the answer.
More precisely, models don't work with whole words but with tokens – parts of words (for example “program” + “ming”). To understand the idea, it is enough to think of words.
Try it: the smallest language model
The smallest “language model”: the program has “read” only ten short sentences and remembered which word comes after which. Then it writes by randomly choosing, after each word, one of the words it has already seen there.
The text it “learned” from
today is a nice day . today is a day for a walk . at school we learn programming . at school we learn math . programming is fun . math is fun and useful . artificial intelligence learns from examples . a computer learns from data . today we learn how a computer learns . a walk is nice when the day is sunny .
Starting word:
Click several times. Do the sentences sometimes make sense and sometimes sound funny (for example “today is a nice when the day is fun.”)? This program looks at only one previous word and knows only ten sentences. Real language models look at thousands of previous words, learn from a huge amount of text and have billions of weights – so they sound much better. But the basic idea is the same: based on the previous text, predict the next word.
How a chatbot is made
| Phase | What happens |
|---|---|
| 1. pre-training | the model “reads” a huge amount of text (books, websites, articles, program code) and learns to predict the next word; this way it learns the language, but also many facts and patterns |
| 2. learning to be an assistant | people write examples of good answers and rate the model's answers; the model learns to answer in a useful, clear and polite way, and to refuse dangerous requests |
| 3. use | the model answers questions; some chatbots can also search the internet or use other tools |
After training, the model doesn't learn automatically from every conversation with you. What it “knows”, it learned during training – that is why it may not know about the latest events.
Why a chatbot sometimes makes mistakes
- Convincing ≠ correct. The model chooses words that “sound” likely. If there was no correct answer in the data, it will write something that only looks like an answer – this is a hallucination.
- Its knowledge is out of date. Training ends at some point; the model doesn't know about later events unless it has access to search.
- Calculation. The model doesn't calculate like a calculator, but “guesses” digits like words; with long calculations it can make mistakes (that is why newer models use tools).
- The data has errors and biases – and the model inherits them.
That is why the rule from the lesson How to use AI tools applies: check important answers.
Does a chatbot understand?
This question divides even scientists. Some say the model only predicts words very skillfully and understands nothing (remember the “Chinese room” from the lesson about the Turing test). Others think that to predict so well, the model must build some kind of internal “model of the world”. The answer is not yet known – more about this in the module AI and society.
Students play a “human language model”: each adds one word to a story that goes around the class, looking only at the last word (like the demo) – then they repeat it looking at the whole sentence. Compare which stories make more sense, and why.
Common misconceptions
- “A chatbot looks up the answer in a database of ready-made answers” – the answer is created word by word, anew each time.
- “A chatbot remembers everything you tell it and learns from it” – the model doesn't change during the conversation; some apps save conversations, but that is not the same as the model learning.
- “If a chatbot writes something confidently, it must be true” – confidence doesn't mean correctness.
Practice tasks
Try it yourself first, and only then open the solution.
- Explain in your own words how a language model creates an answer (3–4 sentences).
- Why does the same chatbot sometimes answer the same question with different words?
- In the demo “the smallest language model” the sentence “today is a nice when the day is fun.” appeared. Why can't this program write a sensible longer story?
Solution 1: an example answer
The model gets my question. For every possible next word it calculates how likely it is, chooses one and adds it. It repeats this word by word until it has written the whole answer. It learned the probabilities from a huge amount of text during training.
Solution 2: answer
Because it doesn't always choose the most likely word, but one of the likely ones – with a little randomness. A small difference at the beginning leads to a different continuation.
Solution 3: explanation
It looks at only one previous word and knows only ten sentences. It doesn't “remember” what it wrote before that, so the topic easily changes. Real models look at a lot of previous text and learn from a huge number of examples.
What's next
You have finished the module How a machine learns. In the next module we write all this in Python: the nearest neighbor algorithm, the perceptron and a real neural network. If you don't know Python yet, start with the guide Python (SR).
Check your knowledge
Open the quiz
Quiz: how chatbots work
1. What does a language model basically do?
2. What is a token?
3. Why may a chatbot not know about the latest events?
4. What is a chatbot's hallucination?
