Back to list

Tech Blog · AI Agents

[AI Agent Basics 1] How does a language model predict the next word?

The starting point of an AI agent is "predicting the next word." We explain how tokens, probabilities and the transformer work, and the limits that follow from them, such as made-up answers and arithmetic mistakes, using everyday process work as examples.

Jun 2, 2026 · OPTI-X

Basics series “How AI Agents Are Built” · Part 1

AI agents today write reports, fix code and analyze data. Yet the starting point for all of these abilities is one surprisingly simple task: "Predict the word that comes after the text so far."

Whether it is summarizing an operating log or asking about the cause of an equipment fault, the same thing happens inside the model. Knowing this principle lets you anticipate where a language model is strong and where it goes wrong.

Cutting text into tokens

A language model does not read text as it is. It first cuts the text into small pieces called tokens. A token can be a word or part of a word. Frequent words become a single piece, while rare words are split into several pieces.

The 2015 study that brought this approach to translation gives the example of splitting the German word for "sewage water treatment plant" (Abwasserbehandlungsanlage) into the pieces Abwasser | behandlungs | anlage. Even a long word the model has never seen can be handled as a combination of pieces it knows. The English-German translation model in the transformer paper used a list of about 37,000 tokens, and each token is turned into a list of numbers (a vector) for calculation.

From a sentence to an answer

A language model produces one token at a time

01Sentence input'The neutralization tank pH is'
02TokenizationCut the text into small pieces and convert them to numbers
03Model calculationCalculate the relationships among preceding tokens (transformer)
04Next-token probabilitiesAssign a probability to every possible token

How much a model can read at once is also set in tokens. In 2020, GPT-3 could read up to 2,048 tokens at a time. An operating log longer than that could not be fed in all at once.

Not one right answer, but a probability distribution

What the model outputs is not "the correct word" but a probability for every candidate token. It picks one, appends the chosen token to the input, and predicts the next token again. A long answer is the result of repeating this process hundreds or thousands of times.

Example next-token probabilities

What comes after 'The neutralization tank pH is' (illustrative values)

lower
46%
higher
38%
slightly
9%
Other
7%
Source: Hypothetical values for explanation

In the figure, the gap between "lower" and "higher" is small. The model does not choose by looking at the actual pH measurement. It chooses based on what tended to follow sentences like this in the text it learned from.

This is also why the same question gets a slightly different answer each time. A 2019 study (Holtzman et al.) showed that always picking the most probable token makes text bland or makes it repeat itself. It therefore proposed cutting off the very unlikely tail of candidates and picking at random among the rest.

Transformer: seeing all preceding words at once

The transformer architecture, published in 2017, calculates all at once how much each token in a sentence should refer to every other token (attention). For example, the token "pH" draws heavily on the preceding "neutralization tank." When generating text, the later positions are masked so that each token refers only to the tokens before it. The model cannot look ahead at words it has not yet written.

Recurrent neural networks, the mainstream approach before it, processed tokens one at a time from the start. The paper pointed out that this sequential computation blocks parallel training. The transformer removed this constraint and trained a translation model to the best quality of its time in 12 hours on 8 GPUs; even the large model needed only 3.5 days. This architecture made it possible to train huge models with large-scale parallel computation.

Why is predicting the next word so powerful?

To predict the next word well across internet-scale text, knowing grammar is not enough. The model also has to pick up, to some degree, facts, logic, arithmetic and the rules of code. By repeating a simple task at a very large scale, the model learns the knowledge needed to solve that task as a by-product. This is pre-training.

In numbers, GPT-3 was a model with 175 billion parameters trained on 300 billion tokens. Without any further training, it solved new tasks such as translation and question answering just by being shown a few examples in its input. Showing two or three examples of an operating-log summary and asking for the same format is this approach.

Three things to remember

Concepts that come up again throughout this series

TokenThe smallest unit a model reads and writes
Length, cost and memory limits are all counted in number of tokens
ProbabilityAnswers are drawn from probabilities
That is why you can get plausible but wrong answers
Pre-trainingNext-word prediction at enormous scale
Knowledge builds up as a by-product of this process

Limits in process work

Some limits follow directly from the principle. The model does not run a calculator; it reads numbers as tokens, too, and predicts the next token. In the GPT-3 paper's arithmetic tests, it got every two-digit addition right, but accuracy fell as the number of digits grew.

GPT-3 addition accuracy

Accuracy falls as the digits increase · With a few examples shown, 2,000 problems per task

2-digit addition
100.0%
3-digit addition
80.4%
4-digit addition
25.5%
5-digit addition
9.3%
Source: Brown et al., Language Models are Few-Shot Learners (NeurIPS 2020), Table 3.9, redrawn

In process work, this means the numbers can be wrong when you ask for the total or average of a day's chemical dosing from an operating log. When you ask about an equipment fault, the model likewise does not look up the manual; it continues with a plausible next phrase. So it can make up alarm codes or part names that do not exist. The GPT-3 paper itself notes that long passages can lose coherence or contradict themselves.

There is also a limit in the training objective itself. The GPT-3 paper states that current pre-training weights every token equally and has no notion of what is more important to predict. A word like "lower" versus "higher" in "the pH is lower," where one word flips the required action, is learned with the same weight as any other token.

Language models started out as machines that produce a plausible next phrase. So for work that needs "accuracy and evidence" rather than "plausibility," such as equipment setpoints, a separate mechanism is needed on top of this machine. In the next part, we look at how this autocomplete machine became a conversation partner.

References

Could this work for your process?

Talk to us