MegaMaester

Artificial Intelligence · Lesson 2

How Large Language Models Work

beginner16 min · 13 cards
Start here

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.

Concept 1 of 10

Why this matters

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.

Concept 2 of 10

Core concepts

Predicting the next token

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.

The context window

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.

Fluent is not the same as accurate

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.

Concept 3 of 10

Worked example

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.

Concept 4 of 10

Counterexample

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.

Concept 5 of 10

Case study: the transformer and next-token prediction

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.

Concept 6 of 10

Common misconceptions

  • "The model understands what it is saying." — It predicts likely tokens; fluency is a statistical achievement, not comprehension.
  • "It looks up answers in a database." — There is no lookup step; it generates each answer as a prediction, which is why it can fabricate.
  • "If it sounds confident and fluent, it is probably right." — Fluency reflects plausibility, not accuracy; confident text can be wholly wrong.
  • "Hallucination is a rare bug." — It is the same next-token process that yields correct answers, applied where the learned patterns do not match reality.
Concept 7 of 10

Interactive challenge — Predict the Next Token

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.

Concept 8 of 10

Knowledge check

  1. What single task is a large language model trained to perform?
  2. What is a token, and why do models work in tokens rather than whole words?
  3. What is a context window, and what happens to text outside it?
  4. Why can a model produce fluent output that is completely false?
  5. What did the 2017 transformer paper make possible?
Concept 9 of 10

Lesson summary

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.

Quick check

What most clearly distinguishes a generative model from a discriminative one?