How Large Language Models Work
How large language models work: trained to predict the likely next token, bounded by a context window, fluent but not always accurate.
Artificial Intelligence · Lesson 2
How large language models work: trained to predict the likely next token, bounded by a context window, fluent but not always accurate.
Large language models now draft emails, answer questions, and write code so fluently that it is natural to assume something is thinking on the other side. It is not. An LLM is a next-token prediction engine trained on enormous amounts of text, and almost everything that puzzles people about these systems, why they sound authoritative while wrong, why they fabricate a citation, why rephrasing changes the answer, follows from that one fact. Knowing how the machine produces its words is the difference between using it well and being fooled by it.
At its core, a large language model does one thing: given a stretch of text, it predicts what comes next. It works not in whole words but in tokens, chunks that may be a full word, part of a word, or a punctuation mark. Trained on vast amounts of writing, it tunes itself to guess the likely next token, then the next, feeding each choice back in. A paragraph of fluent output is that loop run hundreds of times, extending a pattern of text in the most statistically plausible direction it learned, not consulting facts or looking anything up.
A model does not consider everything ever written; it sees only the text currently in front of it, called the context window. This is the span of tokens it can take into account at once: your prompt, the recent conversation, anything you paste in. Whatever falls outside is invisible to it. That is why a long chat can seem to "forget" its own beginning, and why pasting relevant text helps, since you place information where the model can see it.
Because the model optimises for plausible-sounding text, fluency and truth come apart. It produces a smooth, confident sentence whether or not the content is correct, predicting what a good answer looks like rather than checking what is true. When it confidently states something false, a fake reference or invented statistic, this is called hallucination, not lying or malfunction but the system generating likely text with no built-in sense of fact.
Type "The capital of France is" and the model completes it with "Paris." It is tempting to say it knows the fact. In reality it predicts the most likely continuation, and in its training text that phrase is followed by "Paris" overwhelmingly often. Ask it to complete "The capital of the fictional kingdom of Xanadu is" and it invents a confident city by the same process, with no notion of which answer is real.
Contrast a search engine or database query, which retrieves stored records and can cite a source. Ask it for something not in its records and it returns nothing rather than a fabrication. An LLM has no such store; it generates a likely answer every time and so never draws a blank, producing plausible text even when it should have nothing to say. Retrieval finds; prediction produces.
The capability rests on an architecture called the transformer, introduced in the 2017 paper "Attention Is All You Need." Its central idea let models weigh the relationships among all the tokens in a stretch of text efficiently, which made training on far larger amounts of data practical. Scaled up over the following years, transformer-based models trained on next-token prediction grew fluent enough to reach the public with the late-2022 release of ChatGPT. The durable point is simple: a verifiable advance in 2017 made large-scale text prediction work, and everything an LLM does still reduces to prediction.
You are given the start of several sentences and asked to guess the model's most likely completion, then to spot which prompts invite a confident but potentially fabricated answer.
Think Like a Maester: An LLM answers "what text is likely here?", never "what is true?". The two often coincide, which is precisely what makes the times they diverge so easy to miss.
A large language model is trained on huge amounts of text to predict the most likely next token, over and over, until it produces fluent output. It learns the statistical patterns of language, not facts or meaning, and holds only the text in its context window in view at any moment. Because it optimises for plausibility rather than truth, fluent and accurate are not the same, and hallucination is that same prediction process meeting patterns that do not match the world. The transformer of 2017 made this scale possible, but the essential idea stays fixed: an LLM predicts text; it does not understand or retrieve it.
Mark this lesson complete to track your progress.