Generative AI

How Large Language Models Predict Text

How large language models learn to predict the next word, how repeating that step produces fluent writing, and why fluency is not the same as understanding.

On this page
  1. The core idea
  2. How the model learns
  3. What the model sees: tokens
  4. Inside the network
  5. From probabilities to sentences
  6. Why it sounds fluent
  7. What fluency does not guarantee
  8. After the basic training
  9. The takeaway

The core idea

A large language model, often shortened to LLM, is a program trained to answer one question over and over: given the text so far, what is likely to come next? If the text so far is “The cat sat on the”, the likely continuations include “mat”, “sofa” and “floor”, and unlikely ones include “democracy”. Everything a chatbot writes comes from applying that single skill repeatedly.

It sounds too simple to produce essays, code and conversation. The surprise of the last several years is that with enough data and a large enough model, it does produce text that is remarkably fluent and often useful.

How the model learns

Training starts with a very large collection of text, such as books, articles, web pages and code. The model is shown a passage with the next piece hidden and asked to guess it. At first the guesses are poor. Each time, the system measures how far off it was and nudges its internal numbers, called parameters, to do slightly better. Repeated across an enormous amount of text, this gradually builds a rich statistical picture of how language works.

To get good at predicting, the model has to pick up many regularities along the way:

  • grammar and sentence structure;
  • facts that appear frequently in the text, such as that Paris is the capital of France;
  • styles and formats, like how a recipe, a legal notice or a poem is usually laid out;
  • common patterns of reasoning that appear in written explanations.

No one writes these rules in by hand. They emerge from adjusting the network’s numbers so predictions improve. This is the same basic principle described in machine learning basics, applied to text at a very large scale, and the quality of the training data strongly influences what the model can and cannot do.

What the model sees: tokens

Models do not read letters or whole words directly. Text is cut into chunks called tokens, which can be whole words, parts of words or punctuation marks. Each token is represented by a list of numbers. The model’s job is to produce a probability for every possible next token. The size of the set of tokens it can handle and how much it can look at is discussed in tokens and context windows.

Inside the network

The model is a neural network, and most modern ones use a design called a transformer. Its key feature is a mechanism known as attention, which lets each token look at the other tokens in the passage and decide which are most relevant. In “The trophy would not fit in the suitcase because it was too big,” attention helps the model connect “it” to “trophy” rather than “suitcase.” This ability to weigh context across a whole passage is a large part of why transformers write more coherently than earlier methods. For a gentle introduction to the building blocks, see how neural networks work.

From probabilities to sentences

Predicting a probability is not yet writing. To produce text, the system does this loop:

  1. Take the prompt and any text already produced.
  2. Calculate a probability for each possible next token.
  3. Choose one token.
  4. Add it to the text and repeat.

How it chooses matters. If it always takes the single most probable token, the writing is predictable and can become repetitive. Most systems add controlled randomness, with a setting often called temperature. A low temperature gives safer, more consistent output. A higher temperature allows less likely tokens, which makes the writing more varied but also more error-prone. This is one reason the same question can get different answers on different tries.

Why it sounds fluent

Fluency comes from the training objective. The model has seen so much human writing that it has absorbed what well-formed text looks like at every level, from grammar to the typical structure of an argument. Predicting the next token well requires producing text that fits all of those patterns at once, so the result tends to read smoothly.

What fluency does not guarantee

Predicting plausible text is not the same as knowing what is true.

  • The model has no built-in fact-checking step. It produces what fits the pattern, so it can state wrong things confidently, which is the subject of AI hallucinations.
  • Whether it “understands” in a human sense is debated. It clearly captures a lot of structure in language and can solve many problems, but it also fails in odd ways on simple tasks, and it is safest to describe its behavior rather than assume a mind.
  • It reflects its sources. Gaps and skew in the material can show up as one-sided or stereotyped output, a topic covered under algorithmic bias.
  • Its knowledge comes from a fixed training period, so without extra tools for looking things up, it may not know about recent events.
  • It can be steered by wording, so how a request is phrased affects what comes back, as described in AI prompts.

After the basic training

A model trained only to predict text is not yet a good assistant. Developers typically refine it further, for example by having people rate or compare responses and training the model to prefer the kinds of answers people judge helpful and safe. This additional step shapes tone and behavior, such as following instructions and declining some requests. It does not turn the system into a reliable source of facts, though it changes how the model responds.

The takeaway

Large language models are very capable pattern predictors. Predicting one token at a time, with a great deal of learned context behind each choice, is enough to write, summarize, translate and explain. Understanding that mechanism makes both their usefulness and their mistakes easier to anticipate: they are strongest when a plausible, well-formed answer is what you need, and weakest when exactness matters and nobody checks the result.

The complete guide to generative ai

Generative AI Explained Without the Hype
Read the guide

Keep exploring

Guides from other topics

All topics →

How Mobile Apps Are Built

A plain-language walk through the stages of mobile app development, from the first idea and design sketches to testing, release and ongoing maintenance.

Mobile Apps 5 min read