0% completed
What Happens Inside an LLM When I Ask a Question?
On This Page
Step 1: The Application Builds the Full Input
Step 2: The Text Becomes Tokens
Step 3: The Model Gives Every Possible Next Token a Chance
Step 4: One Token Is Chosen and Added
Step 5: The Tokens Become Text Again
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways
Short answer: A large language model (LLM) does not search a database, the web, or any other place for the answer to your question. The application sends your question to the model as a list of numbers. The model then calculates a chance for every possible next piece of text, and a program chooses one piece and adds it to the answer. This step repeats, one piece at a time, until the answer is complete.
Look at the arrow under steps 3 and 4. Those two steps run again for every small piece of the answer, and that loop is the most important part of the picture.
The rest of this lesson follows one question through the five steps. A user opens a chat application and types this:
What does a load balancer do?
A few seconds later, the application shows this answer:
It spreads requests across several servers.
Step 1: The Application Builds the Full Input
The model never receives your question alone. The application first places the question inside a larger text. That text usually starts with instructions written by the company that built the application (the system prompt), and users do not see them. If the chat already has earlier messages, the application adds all of them as well.
For the first question of a new chat, the input can be as small as this:
[ { "role": "system", "content": "You are a helpful assistant. Answer in one sentence." }, { "role": "user", "content": "What does a load balancer do?" } ]
This step explains two facts that surprise many engineers. First, the model keeps nothing between requests. A chat seems to have memory only because the application sends the earlier messages again with every new question. Second, the model can use only this input and what it learned from a very large amount of text before its release (its training). It can read your files, your database, or the web only when the application puts that content into the input.
Step 2: The Text Becomes Tokens
A model calculates with numbers, so it cannot read letters. A separate program (the tokenizer) cuts the input into small pieces of text called tokens. A token is often one whole word, and a long or less common word is cut into several tokens.
Notice that the word "balancer" became two tokens, "bal" and "ancer". The picture shows how one widely used tokenizer cuts this question. That tokenizer has a fixed list of about 200,000 tokens (its vocabulary), and every token in the list has a number called its token ID. The model receives only the ID numbers.
Step 3: The Model Gives Every Possible Next Token a Chance
The model is a very large mathematical function. It holds billions of numbers called parameters, and all of them were set during training, before your question arrived. Answering a question does not change them.
The model reads all the input tokens together and performs one long calculation with its parameters. The result is a list and not a word. The list gives every token in the vocabulary a number between 0 and 1, which is the chance (the probability) that this token comes next.
For this question, the list could start as follows: "It" gets 0.48, "A" gets 0.37, and "The" gets 0.09. All the chances in the list together equal 1, so all the other tokens share the remaining 0.06. These numbers are illustrative, which means that they are invented for this example, but a real model produces a list of the same kind.
The chances come only from this calculation. The model does not search for anything in this step, and it does not fetch a stored answer.
Step 4: One Token Is Chosen and Added
The program that runs the model then chooses one token from the list. A token with a higher chance is chosen more often, but the token with the highest chance is not always chosen. In this example, the program chooses "It".
The program adds the chosen token to the end of the text, and steps 3 and 4 run again on the longer text. Each run is called a pass in this lesson. The diagram below shows the passes for this answer.
Look at the column on the right. Each pass adds exactly one token, and the model cannot change a token after it has been added. The loop ends when the chosen token is a special stop signal. This answer has 7 tokens, so the model ran 8 times: once for each token and once for the stop signal.
Step 5: The Tokens Become Text Again
The tokenizer turns each chosen token ID back into text, and the application displays that text. Most applications display each token as soon as it is chosen, which is called streaming.
Streaming is the reason an answer appears on the screen a few words at a time. In most applications this is not a visual effect, because each piece appears at the moment the model produces it.
Common Wrong Beliefs
| Belief | What actually happens |
|---|---|
| The model searches the web or a database for the answer. | The model calculates the answer from its parameters and the input. A search happens only when the application adds a search tool, and the application then places the search results in the input. |
| The model writes the whole answer first and then shows it slowly. | The answer does not exist until it is produced. Each token is calculated, chosen, and displayed, one after another. |
| The model remembers my earlier chats. | The model keeps nothing between requests. The application sends the earlier messages again each time. If an application has a memory feature, that feature belongs to the application and not to the model. |
| The model receives only the text that I typed. | The input also holds the instructions of the application and the earlier messages of the chat. |
What This Means for Your Work
- A long answer is slow and costs more. Every output token needs one more pass through the model, but the model reads all the input tokens together in one pass. This is one reason the companies that run models (providers) usually charge more for an output token than for an input token.
- Everything the model should use must be in the input. If the answer depends on your code, your logs, or a document, your program has to send that content.
- A well-written answer is not a checked answer. The model produces likely tokens, and it does not verify them against any source. Check the facts that matter, such as names, numbers, and links.
- Your code controls the loop. A request can set a maximum number of output tokens. If the answer reaches that limit before the stop signal, it usually ends in the middle of a sentence.
Try It Yourself
The program below is a small, simplified version of steps 3 and 4. A real model calculates the chances from the whole text so far. The simplified program reads them from a small table and uses only the last token, so that the loop is easy to see. Run it and compare the output with the diagram above.
The first run prints 8 passes and the answer "It spreads requests across several servers." Now change SEED to 5 and run the program again. The answer becomes "A load balancer distributes traffic across several machines." That answer has 10 tokens and needs 11 passes. The question did not change, and only the choices in step 4 were different.
Check Your Understanding
An application asks a model for an answer, and the answer is 200 tokens long. How many times does the model calculate a list of chances for the next token?
Key Takeaways
- The model calculates an answer and does not search for a stored one. Its parameters were set during training, and it uses only those parameters and the input.
- The input is more than your question. The application adds its own instructions and the earlier messages, and the tokenizer turns all of it into token ID numbers.
- An answer is produced one token at a time. In each pass, the model gives every possible next token a chance, and the program chooses one token and adds it. The loop repeats until a stop signal.
- The model keeps nothing between requests. The application must send again anything that the model should use in the next request.
The next lesson, Does an LLM Understand My Question or Only Predict the Next Word?, asks whether a model that works this way understands what it reads.
Reading Progress
0%
On This Page
Step 1: The Application Builds the Full Input
Step 2: The Text Becomes Tokens
Step 3: The Model Gives Every Possible Next Token a Chance
Step 4: One Token Is Chosen and Added
Step 5: The Tokens Become Text Again
Common Wrong Beliefs
What This Means for Your Work
Try It Yourself
Check Your Understanding
Key Takeaways