5 min read
Illustration of word tokens joined by attention arcs
Image credits · Illustration: Global Pulse Daily

How Large Language Models Work: Tokens, Attention and Training

A language model writes by predicting one small piece of text at a time, using a Transformer network whose billions of adjustable numbers were tuned on enormous amounts of writing.

In short

A large language model is a neural network trained on vast text to predict the next token. It splits input into tokens, uses Transformer attention to weigh context, and writes a reply one token at a time. Training tunes billions of parameters to make those predictions accurate.

Table of contents 7 sections
Key takeaways
  • A large language model generates text by repeatedly predicting the next token from the tokens before it.
  • Tokens are fragments of words that the model turns into lists of numbers called embeddings.
  • Transformer attention lets every token weigh the relevance of every other token in the context.
  • Pre-training teaches the model language, and fine-tuning with human feedback teaches it to follow instructions.
  • The model stores knowledge in its parameters rather than looking facts up, so fluent answers still need checking.

To see how large language models work, start with one idea: they predict the next piece of text. Given everything written so far, the model works out which fragment is most likely to come next, adds it, and repeats until the answer is complete. Tokens, attention and training are the machinery that makes that prediction good enough to hold a conversation.

What is a token, and why do models not read words?

Models do not read letters or whole words. A tokenizer first chops text into tokens, fragments drawn from a fixed vocabulary. A token can be a common word, a piece of a rarer word, a punctuation mark or a space, and a method such as byte-pair encoding decides where the cuts fall.

Each token is then mapped to an embedding, a long list of numbers. Training adjusts those numbers so that tokens used in similar ways end up with similar values. Because the model sees chunks rather than letters, it can stumble on jobs like counting the letters in a word, and limits on input length are measured in tokens, not words.

How does attention let a model use context?

The core of a modern model is the Transformer, introduced in a 2017 paper by researchers at Google and the University of Toronto titled Attention Is All You Need. Earlier designs relied on recurrent or convolutional networks. The Transformer dropped both in favour of self-attention: for every token, the model scores how relevant each other token in the context is, then blends their information in proportion. In the original design each token produces a query, a key and a value: its query is compared with every other token's key, and the resulting scores decide how much of each value to take.

Take the sentence "The trophy would not fit in the suitcase because it was too big." Attention is what lets the model link "it" to the trophy rather than the suitcase. Because attention ignores word order on its own, the model also adds position information to each token. And because all tokens can be processed in parallel, Transformers train significantly faster on modern chips than the designs they replaced.

A real model stacks many layers of this, each pairing an attention step with a small feed-forward network. Many chat models use a decoder-only version of the original design.

How do large language models learn from text?

Learning happens in two broad stages. In pre-training, the model reads an enormous amount of text and plays a single game: hide the next token, guess it, compare the guess with the real one, and nudge the parameters, the adjustable numbers inside the network, to guess slightly better next time. Nobody has to label the data, because the text supplies its own answers. GPT-3, described in a 2020 paper, had 175 billion parameters and was trained on 300 billion tokens.

A pre-trained model only continues text and does not reliably follow instructions. Developers therefore fine-tune it on examples of good responses, then use reinforcement learning from human feedback, in which people rank candidate answers and the model is trained to favour the preferred ones. OpenAI's InstructGPT study showed how much this matters: human evaluators preferred outputs from a 1.3 billion parameter model trained this way over those of the 175 billion parameter GPT-3.

How large language models work when you send a prompt

At answer time the parameters are fixed. Your prompt is split into tokens and passed through the layers, and the model outputs a score for every token in its vocabulary. Those scores become probabilities, and one token is chosen. It is not always the most likely one, because a setting often called temperature controls how adventurous the choice is, which is why asking twice can give different wording.

The chosen token is added to the text and the whole process repeats. The context window caps how many tokens the model can consider at once, and anything beyond it is invisible. Notice what is missing: there is no lookup in a database of facts. Whatever the model knows is encoded in its parameters, frozen when training ended.

Does a bigger model always perform better?

Not automatically. DeepMind's 2022 study of compute-optimal training found that many large models were undertrained and concluded that model size and training tokens should grow in equal proportion. Its model Chinchilla, with 70 billion parameters and four times more training data, outperformed the 280 billion parameter Gopher built on the same compute budget.

Scale still carries a cost. Training and running these models takes large clusters of specialised chips, which is why energy use has become a policy question. Our look at how much power AI data centres use puts numbers on it.

What can large language models not do reliably?

  • Guarantee that a fluent answer is true. The model produces plausible text, and plausible is not the same as correct.
  • Know recent or private information. Anything after the training cutoff, or inside your own files, has to be supplied in the prompt. Systems that fetch documents first, described in our explainer on retrieval-augmented generation, automate that step.
  • Keep unlimited material in view. The context window is finite.
  • Repeat themselves exactly. Sampling means the same prompt can produce different answers.

Whether pattern prediction at this scale amounts to understanding is debated among researchers. The mechanism itself is not in dispute.

What should you do with this knowledge?

Knowing how large language models work points to a few habits. Give the model the context it needs, because it can only use what sits in its window. Check names, numbers, quotations and citations before you rely on them.

Use models for drafting, summarising, translating and restructuring, where fluent language is the product. For exact arithmetic, current prices or records, reach for a calculator, a database or a search tool. When a model is given tools and a goal it becomes what people call an agent, a step explained in our piece on what an AI agent is. Before pasting in anything confidential, read how the provider handles the text you type.

Frequently asked

Sources
  1. Attention Is All You Need — arXiv (Vaswani et al.)
  2. Language Models are Few-Shot Learners — arXiv (Brown et al., OpenAI)
  3. Training language models to follow instructions with human feedback — arXiv (Ouyang et al., OpenAI)
  4. Training Compute-Optimal Large Language Models — arXiv (Hoffmann et al., DeepMind)
Written by Said Dulevic

Our international desk covers the infrastructure, policy and people behind the technology shaping the world. Tips and corrections: newsroom@globalpulsedaily.com

More from this author

0 comments

Never published.
A quick check that you are not a script.
Comments are moderated. Links, contact details and abuse are removed.

No comments yet — be the first.