Article · 5 min read

How does a language model work?

ChatGPT, Claude and Gemini seem to understand us. Under the hood they do one thing, over and over: guess the next piece of text. Here is the whole journey from letters to answers, with figures you can try for yourself.

Short answer

A language model is a neural network trained on vast amounts of text to do one thing: guess the next piece of text. The text is split into tokens, each token becomes a long list of numbers, and in dozens of layers the model lets the words influence each other through what is called attention. Out comes a probability for every possible next token. The model picks one, adds it and starts again. Everything it does, from poems to code, rests on that loop.

One question, over and over

At heart, a language model answers a single question: which piece of text comes next? It works out a probability for every possible continuation, picks one, adds it and asks the same question again. A long answer from a chatbot is thousands of such guesses in a row.

The model does not always pick the most likely word. A setting called temperature controls how bold it is: low temperature gives predictable answers, high gives more surprising ones and, in the end, incoherent ones. Try it yourself.

Figure 1The next word

This figure is interactive and needs JavaScript.

Click a word to continue the sentence, or let the model choose. Drag the temperature and watch the probabilities even out. The numbers are made up but show the principle; a real model chooses from its whole vocabulary.

From text to tokens

A computer cannot calculate with letters, so the text is first split into tokens. Common words often become a single token, while unusual words are split into several pieces. Every token has a number in the model’s vocabulary, which in today’s models usually holds more than a hundred thousand tokens. Those numbers are all the model sees.

The split is decided before the model is trained, using a method that merges characters that often appear together. Because most tokenisers were trained mainly on English text, words in other languages are often cut into more pieces, which makes that text a little more expensive and slower to process.

Figure 2Text becomes tokens becomes numbers

This figure is interactive and needs JavaScript.

Type something and see how it is split. A simplified split with made-up numbers, to show the principle; every model has its own.

Words as places in a space

Each token is then turned into an embedding: a long list of numbers, in large models thousands of them. Think of it as a place in a space with that many dimensions. During training, words with similar meanings end up close together, and directions in the space take on meaning. The step from “man” to “king” resembles the step from “woman” to “queen”.

The classic example comes from researchers at Google, who showed in 2013 that you can do arithmetic with word vectors in this way. In today’s language models the picture is more complicated, but the basic idea holds: meaning becomes geometry.

Figure 3Meaning becomes geometry

This figure is interactive and needs JavaScript.

Pick a word to see its nearest neighbours, or do arithmetic with meanings. A simplified map in two dimensions; a real model has thousands.

Attention: words look at each other

The meaning of a word depends on its context. In “The cat sat on the mat because it was tired”, it is the cat that is tired, but swap “tired” for “soft” and suddenly it is about the mat. To work this out the model uses attention: every token weighs up the tokens that come before it and decides how much each one should influence it.

When the model reads “tired” it turns its attention towards “cat”, and when it reads “soft”, towards “mat”. Notice that a word can only look back, never ahead: a model that writes text does not yet know what comes next. The idea was the breakthrough of the 2017 paper Attention Is All You Need, which introduced the transformer.

Figure 4Where do the words look?

This figure is interactive and needs JavaScript.

Swap the last word and choose which word does the looking. The stronger the gold, the more attention. The weights are made up but show the principle; a real model computes them in dozens of parallel heads in every layer.

Layer upon layer

Attention is only one of two steps. After it, an ordinary neural network processes each token on its own, and together they form a layer. A large language model stacks dozens of these layers on top of each other: GPT-3, from 2020, had 96. Layer by layer, the representation of the text grows richer. Research suggests that early layers capture more about word forms and grammar, and later layers more about meaning.

At the top, the result becomes a probability for every token in the vocabulary. One token is picked and added to the text, and the whole journey through every layer starts again for the next.

Figure 5One token, all the way

This figure is interactive and needs JavaScript.

Every new token takes a trip through the whole model. A simplified picture; in a real model the calculations for many tokens happen in parallel.

Training: guessing wrong, billions of times

Nobody programmed in what the model knows. It learned by guessing the next token in vast amounts of text and adjusting its parameters a tiny bit after every miss. Meta, for example, trained its Llama 3 model on more than 15 trillion tokens. Over time the right answer becomes more and more likely, and the error shrinks.

After this first training the model can continue any text, but it is not a helpful assistant. It only becomes one after fine-tuning, including RLHF, in which people rate the model’s answers.

Figure 6From chance to knowledge

This figure is interactive and needs JavaScript.

Drag the slider or play the training. The numbers are made up but show the principle: the error, measured as how surprised the model is by the right answer, falls as it sees more text.

Why it works, and where it fails

The remarkable thing is how much fits into that simple task. To guess the next word in a detective story you need, in some sense, to follow the plot, and to guess the next line of a program you need to understand the code. As models and data grow, new abilities appear. Whether this is understanding in the human sense is disputed, and whether the models experience anything is an even harder question. We take it up in Can an AI become conscious?

The same mechanism explains the weaknesses. The model aims for what is likely, not for what is true, so it can make things up with great confidence, so-called hallucinations. It only knows what was in its training data, unless it is allowed to search or use tools. And because it chooses with a measure of chance, the same question can get different answers.

Sources

  1. Vaswani et al., Attention Is All You Need, 2017.
  2. Mikolov et al., Efficient Estimation of Word Representations in Vector Space, Google, 2013.
  3. Sennrich et al., Neural Machine Translation of Rare Words with Subword Units, 2016.
  4. Petrov et al., Language Model Tokenizers Introduce Unfairness Between Languages, 2023.
  5. Brown et al., Language Models are Few-Shot Learners (GPT-3), OpenAI, 2020.
  6. Tenney et al., BERT Rediscovers the Classical NLP Pipeline, 2019.
  7. Meta, Introducing Meta Llama 3, 2024.
  8. Ouyang et al., Training language models to follow instructions with human feedback, OpenAI, 2022.

Terms in this article

How we work: the articles are written with the help of AI, and every fact is checked against the sources listed above. Found a mistake? Write to hej@sibolaget.se.

More questions?

New to AI? Start with What is AI? The glossary explains the terms, and the front page has straight answers to the most common questions. Missing something? Write to us.