How Large Language Models Work
🎯 Introduction
You ask AI a question, and it quickly produces a complete response. Where does that response come from? Does the model search the web every time?
Previously, we explored what LLMs can do. This article starts with sentence completion to explain how a model generates answers, how it acquires this ability, and why its answers can be wrong.
🧱 How Models Generate Text Step by Step
Suppose someone says, “It's a nice day. Let's go to the park for a…”
You might continue with “walk,” because that fits naturally with the preceding text. Models also use the content they have already seen to predict what could come next.
A model typically does not write the entire response at once. It generates it over multiple steps, selecting the next token each time.
A token is a basic unit a model uses to process text. For now, think of it as a small piece of text processed at each step. It might be a character, a word, part of a word, or punctuation; it does not always equal one character. The next article covers tokens in detail.
A Simplified Generation Example
Continue with “It's a nice day. Let's go to the park for a…” For this demonstration, we simplify the next candidates to short text fragments.
The diagram uses hypothetical probabilities to illustrate prediction, rather than calculations from an actual model.
Based on the preceding content, the model calculates how likely different next tokens are, then selects one using a particular selection method. Here are two prediction steps:
This example uses words and punctuation with made-up probabilities. Real tokens can be parts of words, and the model can choose from more candidates, including ones that are less likely.
Suppose the first step selects “walk,” and the next generates a period (“.”). Each generated token becomes part of the content available for the next prediction.
Generation continues until a stopping condition is met, such as producing a special end token or reaching the permitted output length.
The complete process is:
- Split the input text into tokens.
- Use the available content, also called context, to calculate probabilities for the next token.
- Select a token and append it to the generated content.
- Repeat until the response ends.
“Context” means the content the model can currently refer to, such as your question and the text it has generated so far. In a real conversation, it may also include earlier messages and application instructions.
The model does not always select the most probable token, so the same question may produce differently worded answers.
🧱 Training and Inference
Why can the model judge that “walk” is a suitable continuation?
Because it has undergone training before we use it.
Training: Learning to Predict
Use “It's a nice day. Let's go to the park for a walk” to understand the process:
- During training, a program provides part of the sentence and asks the model to predict what follows.
- The program compares the prediction with the actual target in the training example to measure how inaccurate it is.
- If the prediction is inaccurate, it adjusts internal values so the model has a better chance of predicting correctly in the future.
- Repeating this process across large amounts of text gradually teaches the model relationships between words and common language patterns.
The internal values in step 3 are called parameters. For now, understand them as numbers used in the model's calculations and adjusted during training; you do not need their mathematical details.
This is a simplified account. Actual training involves far greater scale and more complex computation.
Inference: Using Learned Capabilities
After training, when you enter a question and the model uses its trained parameters to generate a response, the process is called inference.
| Comparison | Training | Inference |
|---|---|---|
| Purpose | Teach the model to make better predictions | Use the model to generate responses |
| Adjusts model parameters? | Usually | Generally not |
| When it happens | During model development and training | During everyday use |
The diagram compares the two:
Asking AI a question usually runs inference; it does not retrain the model each time.
🧱 Transformers and Attention
A model uses context to make predictions. When that context contains many words, how does it use information at different positions?
Consider:
Alex lent an umbrella to Emma. Later, she returned it. Who does “she” refer to?
“She” alone does not identify the person. With the preceding sentence, we can more easily infer that it refers to Emma.
Many modern LLMs use an architecture called a Transformer. For now, “architecture” means how computation is organized inside the model.
One important Transformer mechanism is attention. When processing information at one position, it lets the model refer to other positions and calculate relationships between them.
In the example, the model needs information such as “Alex,” “Emma,” and “lent” to interpret the sentence. Attention is one way it uses that context.
You do not need to learn the internal Transformer structure or formulas here. The key is that models use context rather than processing each character in isolation.
Attention matters, but answering questions also depends on factors such as what the model learned during training.
⚠️ Why Models Hallucinate
If models can use context to generate natural answers, why do they sometimes get things wrong?
Suppose you invent a company and ask, “What was Cloud Screw Company's revenue in 2022?”
Without reliable information, the model should not give a definite number. Sometimes, however, it generates a plausible amount or even invents sources. Generating incorrect or fabricated content without sufficient factual support is commonly called a hallucination.
Recall the generation process: the model predicts what to output using the current content and patterns learned during training. Producing fluent, plausible text does not mean its claims have been verified. This is one reason hallucinations occur.
When building AI applications, accurate order status, inventory, and prices should come from actual business systems. Important factual answers also require reliable sources and appropriate verification.
❓ Knowledge Check
Q1: Does a model search online every time it answers?
No. It can answer directly using capabilities acquired during training. External information is retrieved only when the application provides and actually uses external functions such as search.
Q2: Is the model retrained every time it generates an answer?
No. Everyday conversations generally involve inference: using trained parameters to generate responses, rather than training again for each question.
🧪 Exercise
Enter this prompt in your usual AI chat tool:
I invented a brand called “Cloud Screw” that does not exist. Tell me its sales revenue in 2022 and provide reliable sources. If you cannot find reliable information, say so explicitly.
Observe whether the model clearly states that it cannot verify the information. If it gives a number, consider: why does a plausible answer not establish that the answer is true?
Success criteria: You can explain how models typically generate text step by step, distinguish training from inference, and explain why fluent answers may contain factual errors. The exercise remains useful even if the model correctly says it does not know.
🎉 Congratulations! You now understand the basics of text generation and can distinguish training, inference, and hallucinations. Next, we will look more closely at tokens.
