AI chatbots work by predicting the next word. Behind a chatbot sits a large language model (LLM). Its basic job is simple: given a text, estimate which word, or more exactly which word piece called a "token", is likely to come next. The model picks one likely piece, adds it to the text, and predicts again. A long, fluent answer comes from repeating this step hundreds or thousands of times.
You can understand the core idea with a model small enough to fit in six lines of Python.
How do chatbots predict the next word?
What word follows "The capital of France is"? Most likely "Paris". You do not need a map to know it. You have read this pattern many times. A language model works in a similar way. During training, it reads a huge amount of text and learns which words appear together in which contexts.
Here is a tiny "model" that counts which word follows which in a short text:
from collections import Counter, defaultdict text = "coffee is hot . tea is hot . coffee is sweet . coffee is hot".split() following = defaultdict(Counter) for before, after in zip(text, text[1:]): following[before][after] += 1 print(following["is"].most_common())
The output is [('hot', 3), ('sweet', 1)]. The tiny model learned that after "is", the word "hot" comes three times out of four and "sweet" one time out of four.
A real language model does not count word pairs. It uses a neural network, looks at thousands of words of context instead of one, and has billions of adjustable numbers (parameters). The core idea is the same: prediction based on probability.
Why does a chatbot give different answers to the same question?
The model usually does not take the single most likely piece. It draws from the probabilities. In our tiny model, that means writing "hot" three times out of four and "sweet" once. This is why asking the same question twice can give different sentences, and sometimes different content.
How does a chatbot remember the conversation?
Each time it writes a reply, the model reads the whole conversation so far again. That is what "remembering" means here. It can only read a limited amount of text, called the context window. In a very long chat, details from the start can get lost. A new chat does not know your old chats unless the tool has a separate memory feature.
How is a large language model trained?
1. Pretraining: the model learns to predict the next piece on a very large collection of text. Grammar, facts, and reasoning patterns are learned in this stage.
2. Fine-tuning: the model is guided to act like an assistant. People show it example questions and answers and rate which replies are more helpful.
Why does predicting the next word work so well?
To predict the next word well, you need to know a lot. To finish "Water boils at sea level at ... degrees Celsius", the model needs a fact. To write the next step of a math problem, it needs patterns of reasoning. When the model and the data are large enough, this skill turns into broad abilities: summarizing, translating, writing code, explaining.
Why do chatbots sometimes get things wrong?
The same idea explains the weak points:
- Made-up answers: the model writes text that looks likely. That does not guarantee it is true. On a topic it knows little about, it can write a believable but wrong answer, such as a source that does not exist. More in [AI hallucinations](/blog/ai-hallucinations).
- Old knowledge: the model knows what was in its training data up to a date. Many tools search the web to fill this gap. Then the answer rests on the pages found, but the model can still summarize a page wrongly.
- Math mistakes: numbers are handled as text pieces, so long calculations can go wrong. Some tools run code in the background to calculate, which is more reliable.
- Confident tone: the tone of an answer tells you nothing about whether it is correct.
What are three rules for using a chatbot well?
1. Treat the output as a draft. It is great for ideas, summaries, and first drafts. Check important facts in a reliable source.
2. Give context. Say what you want, who it is for, and what format you need. Examples are in [prompt writing basics](/blog/prompt-writing-basics).
3. Do not share private information. If you do not know where your text is stored or how it is used, think before you paste it.
Common mistakes
- Using a chatbot as a search engine. A search engine lists pages that exist. A language model writes new text. A chatbot with search still rewrites what it finds in its own words.
- Mixing up fluent and correct. A well-written answer can be wrong.
- Assuming the model understands you like a friend does. It cannot know a detail you did not say, so it fills the gap with a guess.
What to read next
- The AI course explains this in the lesson [next word](/learn/ai/next-word) and in [training and limits](/learn/ai/training-and-limits). See the whole [Artificial Intelligence Foundations course](/courses/ai).
- Start with the big picture in [what is artificial intelligence](/blog/what-is-artificial-intelligence).
- The counter in this post uses a dictionary. Learn more in [Python dictionary tutorial](/blog/python-dictionary-tutorial).
- #chatbots
- #large language model
- #ai basics