Articles & BlogBy 12 min read

For anyone who uses an AI assistant and wants to know what is happening on the other side of the text box. No math is needed. By the end you will know why these systems are so fluent and why they are sometimes wrong.

A large language model, or LLM, is a computer program that has been trained on a very large amount of text to do one thing: given some text, estimate what comes next. Everything a chatbot does is built from that one skill, repeated thousands of times.

The mechanism explains the behavior. A system that predicts likely text is fluent by design and correct only as a side effect. Once you see that, you know when to check the output.

The aisle of a large public library reading room. Tall wooden shelves filled with rows of bound volumes in green, blue and red run down the right side, and readers sit at long wooden tables on the left, out of focusA language model learns from text in quantities no person could read, and what it keeps is the patterns, not a shelf of copies. Photo by Davide Cantelli, CC0, via Wikimedia Commons.

This article explains the mechanism and does not compare products. Profiles of particular model families are in "DeepSeek: The Company, the Models and the Questions", "Qwen AI: The Model Family From Alibaba Cloud" and "Hermes: The Open AI Models and Agent From Nous Research".

What is a language model?

A 2003 paper in the Journal of Machine Learning Research, "A Neural Probabilistic Language Model", states it in one line: a statistical model of language can be represented by the conditional probability of the next word given all the previous ones.

In plain words, a language model is a machine for filling in the blank. Give it "The capital of France is" and it returns a list of candidates with a probability for each. "Paris" gets most of the weight. "a" and "located" get a little. Your phone's keyboard does a small version of this when it suggests the next word.

What is a token?

Models do not read letters or whole words. They read tokens. Anthropic's documentation describes tokens as the smallest units of a language model, which can correspond to words, parts of words, characters or even bytes. Google's developer documentation says tokens can be single characters or whole words, and that long words are broken into several tokens.

Why pieces of words? A 2016 paper presented at the Association for Computational Linguistics, "Neural Machine Translation of Rare Words with Subword Units", gave the reason that stuck. A model has a fixed vocabulary, but language keeps producing rare words, names and new coinages. If the model can build an unfamiliar word out of smaller familiar pieces, it never meets a word it cannot handle.

The makers publish rules of thumb for English and call them estimates:

Maker's documentationRule of thumb for English
OpenAI help center1 token is about 4 characters, and 100 tokens are about 75 words
Google developer documentation1 token is about 4 characters, and 100 tokens are about 60 to 80 words
Anthropic documentationThe number of characters per token depends on the model and the language

For example, this article is about 2,000 words long. By the first rule above, that is roughly 2,700 tokens. Tokens matter to you because they are the unit that limits how much a model can read at once, and the unit that paid services meter.

How does predicting the next token teach a model anything?

A model is a very long list of numbers called parameters. Before training, the numbers are random and the output is noise.

Training is a guessing game played an enormous number of times. The model is shown a stretch of real text with the next token hidden. It guesses. The guess is compared with the real token, and every parameter is nudged a tiny amount in the direction that would have made the right answer more likely. Anthropic's documentation calls this stage pretraining: training on a large body of text to predict the next word given what came before.

Nobody labels anything by hand at this stage. The text is its own answer key, which is why the method can use so much of it.

Guessing well takes more than grammar. To continue a sentence about a chemical reaction, it helps to have picked up some chemistry. None of this is stored as a table of facts. It is spread across the parameters as patterns. A 2020 paper, "Language Models are Few-Shot Learners", described a model with 175 billion of them.

A model fresh from pretraining is not yet an assistant. The same Anthropic page says such models are not inherently good at answering questions or following instructions. A second stage fixes that. A 2022 paper from the Neural Information Processing Systems conference, "Training language models to follow instructions with human feedback", describes it: people write examples of good answers, people rank the model's own answers, and the model is trained further to prefer what people ranked higher. The paper's stated reason: making a language model bigger does not by itself make it better at doing what the user wants.

What is a transformer, and what does attention do?

The design nearly all of these models share is called the transformer. It comes from a 2017 paper titled "Attention Is All You Need". Earlier designs read a sentence one word at a time, in order. The paper proposed a design based only on a mechanism called attention. It was built for translating between languages, and its largest model was trained in three and a half days on eight graphics processors.

Attention answers one question for every token: which of the other tokens matter for understanding this one?

Take the sentence "The cat sat on the mat because it was tired." What does "it" refer to? You know at once that it is the cat. A transformer has to work that out with arithmetic. For the token "it", the attention step scores every other token for relevance and then builds a new picture of "it" that is mostly a blend of the high scorers.

Diagram of the sentence The cat sat on the mat because it was tired, drawn as ten boxes in a row with the word it highlighted. Curved lines run from it to other words. The thickest line goes to cat, a medium line goes to tired, and thin lines go to mat and because. A note says a thicker line means more weight and that this is an illustration, not a measurement from a model. Text below explains that to work out what it means, the model weighs every other token, and each layer runs many of these comparisons side by side.Attention lets each token borrow meaning from the tokens that matter to it.

Three details from the paper round out the picture:

  • Many heads. The original design ran 8 attention calculations side by side in each layer. The authors observed that different heads learn to do different jobs.
  • Many layers. The original stacked 6 layers. Each one refines the picture the layer below produced.
  • Position. Attention on its own has no sense of word order, so the design adds a signal to each token that marks where it sits in the sequence.

How does it write an answer?

One token at a time. The paper's word for this is auto-regressive: the model consumes what it has already produced as input for producing the next piece.

Diagram of one step of text generation in four parts. Part one: the text so far, cut into tokens, reads The, capital, of, France, is, followed by an empty box labeled next. Part two: the tokens pass into the transformer, where layers of attention read every token at once. Part three: the output is a probability for every token in the vocabulary, shown as bars: Paris 92 percent, a 3 percent, located 2 percent, all others 3 percent. Part four: an arrow loops back to the empty box with the note pick one, add it to the text, and go again. The numbers are an illustration, not output from a real model.A whole answer is this loop run again and again, one token each time.

A 500-word answer is that loop run roughly 670 times.

The pick is not always the top choice. A setting called temperature controls how adventurous it is. Anthropic's documentation says higher temperatures give more varied output and lower ones more conservative output, and that even at zero the results are not fully deterministic. This is why the same question can get two different answers.

What is a context window?

The context window is everything the model can see while it writes. Anthropic's documentation calls it the model's working memory, as distinct from the training data. The window holds your messages, any documents you attached, any instructions the company running the service added, and the model's own replies so far, including the one it is writing.

The window is measured in tokens and it has a limit. For a sense of scale, the 2020 paper mentioned above gave its model a window of 2,048 tokens, which by the rule of thumb is about 1,500 words. That is less than this article. Each maker publishes the figure for each of its models.

Nothing outside the window exists. The model does not remember your last conversation unless the product copies some of it back into the window. It does not know today's news unless someone puts it there.

Bigger is not automatically better. The same Anthropic page says that as the amount of text in the window grows, accuracy and recall decline, so what goes in matters as much as how much fits.

There is a security angle too. Everything in the window arrives as one stream of tokens: your request, a pasted web page, an email. "Prompt Injection: The New Phishing, Aimed at the Machine" explains how that gets abused, and "AI Assistants at Work: What Not to Paste In" covers what should stay out of the window in the first place.

Why do they make things up?

Researchers call it hallucination: a fluent, confident statement that is false.

The model is trained to produce likely text. For a question whose answer appeared many times in training, the likely text and the true text are the same. For an obscure fact, such as the publication year of a minor paper, the model has no strong pattern to lean on. It still has to output a next token, so it produces something shaped like an answer.

A paper presented at the 2024 ACM Symposium on Theory of Computing, "Calibrated Language Models Must Hallucinate", argues that this is more than a flaw to be engineered out. The authors show a statistical lower bound on how often a well-calibrated language model will invent certain kinds of facts, and they say it holds even with ideal training data that contains no errors.

A 2024 paper in Nature, "Detecting hallucinations in large language models using semantic entropy", studied one kind in detail. It calls them confabulations: answers that are both wrong and arbitrary, so that asking the identical question again can produce a different answer. The authors used that as a detector. Ask several times, group the answers by meaning, and treat wide disagreement as a warning sign.

As a rule of thumb, be most careful with the things a model has the least pattern for: precise numbers, dates, quotations, citations and anything recent. Anthropic's guide to reducing hallucinations lists habits that help: tell the model it may say "I don't know", ask it to quote the source word for word, and ask for a citation for each claim.

What do fine-tuning and retrieval add?

Two techniques adapt a general model to a particular job, and they are often confused.

Fine-tuningRetrieval
What changesThe model's parametersOnly what is placed in the context window
HowMore training on chosen examplesA search step finds relevant documents and adds them to the request
Good forA style, a format, a specialtyFacts that are private, recent or need a source
To update itTrain againChange the documents

Fine-tuning is further training of a model that has already been pretrained. Anthropic's documentation says it makes the model take on the patterns of the new data. A 2019 paper, "BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding", helped make the two-step recipe standard: pretrain once on unlabeled text, then fine-tune for each task with a small addition. OpenAI's documentation describes the practical form: supply examples of the inputs and outputs you expect, and get a model that handles that task well.

Retrieval leaves the model alone and changes what it reads. The method is usually called retrieval-augmented generation, or RAG, after a 2020 paper of that name. The authors observed that models with knowledge only in their parameters cannot easily revise what they know, cannot readily show where an answer came from, and may hallucinate. Their answer was to pair the model with a searchable collection of documents. They reported output that was more specific and more factual, and they showed that swapping the collection updates what the system knows without retraining.

For example, one person asks a general assistant about a small company's refund policy and gets a plausible answer that may be invented. The company builds its own support assistant with retrieval: each question triggers a search of the current policy documents, the matching passages go into the context window, and the model answers from them. The model can be the same one. What it was given to read is different.

Retrieval does not make a model truthful. If the search step fetches the wrong passage, the answer will be confidently based on the wrong passage.

What to remember

  • A language model predicts the next token. A whole answer is that prediction repeated, one token at a time.
  • Tokens are pieces of words. In English, a common rule of thumb is about 4 characters each.
  • Attention lets every token weigh every other token, which is how the model keeps track of what refers to what.
  • The context window is all the model can see while it writes. What is not in it does not exist for the model.
  • Fluent is not the same as correct. Check numbers, dates, quotes and citations, and ask again if it matters.
  • Fine-tuning changes the model. Retrieval changes what the model reads.

Whether anyone should be able to download and run these models freely is a separate argument, and "AI Models Anyone Can Download: Safety Risk or a Check on a Few Companies?" lays out both sides.

Sources (15)