Large Language Models

How Large Language Models Work

Follow a prompt through tokens, the context window and the predict-next-token loop, and see why a model needs a system around it.

The cat sat Model on append and repeat

The short answer

A large language model is a program trained on a large body of text to predict the next token, a word or part of a word. It writes an answer one token at a time, reading everything in its context window on each step. It has no memory between calls and no live knowledge unless a surrounding system supplies them.

What is a large language model?

A large language model (LLM) is a program trained on a large body of text to predict the next token, a word or part of a word, from the tokens before it. ChatGPT, Claude and Gemini are products built on LLMs, and each one is a form of generative AI.

Training works by repetition. The training process shows the model a passage with the next token hidden, and the model guesses. The process then adjusts the model's parameters, the numbers that hold what it has learned, so the correct token scores higher next time. After enough passages, the parameters encode the grammar, facts and styles found in the text. Today's models use the transformer design that Google researchers published in 2017.

Training then stops, and the provider releases the model with its parameters fixed. Your conversations do not change them.

How does a model write an answer?

A model writes one token at a time. It reads the text so far and scores the candidates for the next token, and the software running it picks one. That token joins the text, and the cycle runs again.

text so far The capital of France is Model scores for the next token Paris a the Lyon sampling picks one Paris joins the text, and the loop runs again
One pass through the loop produces one token, so a full reply takes hundreds of passes.

On each pass the model reads the whole text again, including the tokens it has written itself. That re-reading keeps a reply on topic from its first sentence to its last.

What is a token?

A token is the unit of text a model reads and writes: a common word, a piece of a longer word, or a punctuation mark. A program called a tokeniser splits your prompt into tokens before the model sees it.

The invoice is overdue. tokeniser The invoice is over due . one word, one token one word, two tokens An illustration: each model's tokeniser splits text in its own way.
Four words and a full stop become six tokens, and the model counts in tokens.

In English, a token averages a little less than one word. Providers measure a model's limits and their bills in tokens, so a long document in the prompt costs more than a short question. What AI agents cost to run shows the arithmetic.

What is the context window?

The context window is the maximum number of tokens a model can handle in one call, counting what goes in and what it writes. The model can read anything inside the window and nothing outside it.

Context window: one fixed budget of tokens Earlier messages Instructions Your documents Conversation Reply outside the window: the model cannot read them the reply counts too
Four kinds of text share one budget, and whatever does not fit stays invisible to the model.

Your instructions, any documents you attach, the conversation so far and the reply all draw on that one budget. Once a long conversation fills the window, the application has to drop or summarise the oldest messages. If a chatbot loses track of an instruction you gave an hour ago, the application may have trimmed it out of the window.

Why does the same prompt give different answers?

The software picks each token by sampling, a weighted random draw from the model's scores. A token with a high score wins most draws, and a lower-scored token wins some. One different token early in a reply changes the tokens that follow it.

A setting called temperature controls how much randomness the draw allows. A low temperature makes replies more repeatable, and a high one makes them more varied. Run a prompt several times before you rely on it and judge the spread of answers, since a single good reply proves little.

Why do models state false things with confidence?

A model produces the tokens that fit the text so far, and nothing in the loop checks them against the facts. A true sentence and a false one come out in the same fluent voice. Researchers call a false statement of this kind a hallucination.

Two measures reduce it:

  • Grounding. Put the source material in the prompt, tell the model to answer from it, and give it permission to say that the source does not contain the answer. Retrieval-augmented generation does this at scale by fetching the relevant passages for each question.
  • Checking. Ask the model to quote the passage that supports each claim, then have a person verify the claims that carry consequences.

Anthropic's documentation describes both techniques and states that they reduce hallucinations without removing them.

What does a model lack unless a system supplies it?

A model on its own has no memory between calls and no knowledge of anything after its training ended. Each call starts from the fixed parameters plus whatever sits in the context window.

A chat application creates the feeling of memory by sending the earlier conversation back to the model with each new message. Longer-term memory needs a store outside the model, covered in AI agent memory. Live knowledge works the same way: a search tool or a document store fetches current information, and the application places it in the prompt.

Use that split to question any product built on an LLM. The model supplies the language skill. Ask the vendor which part of their system supplies the memory and the current facts, and how they tested it.

Common questions

Does a large language model understand language?

Researchers disagree. A model predicts tokens from patterns in its training text, and that process produces replies that read as understanding. For practical work, test the output on your own cases and verify the claims that matter.

Does the model learn from my conversations?

Not during the conversation. The parameters stay fixed after training. Some providers use conversations to train later versions, depending on your plan and settings, so read the data policy for the product you use.

Why does a chatbot forget what I said earlier?

The context window holds a fixed number of tokens. Once a conversation outgrows it, the application drops or summarises older messages, and the model can no longer read them.

Sources and references

  1. Vaswani et al., arXiv, Attention Is All You Need
  2. OpenAI Help Center, Understanding and counting tokens
  3. Anthropic, Context windows
  4. Anthropic, Reduce hallucinations

Concepts covered: large language model, token, context window, sampling, temperature, hallucination, grounding, transformer.

Weekly newsletter

The AI Brief by Clarence Cheang

A weekly exploration of the developments, technologies and ideas shaping artificial intelligence.

One email a week. Unsubscribe in one click. What is in each issue