Skip to content
AI360Xpert
Core ML

GPT & Autoregressive Models

GPT reads strictly left to right and trains on exactly one job: guess the next word, over and over, which turns out to teach it far more than that sounds.

Generating one token at a time, feeding each output back in as the next input, with the causal mask blocking every position from seeing tokens ahead of it
Generating one token at a time, feeding each output back in as the next input, with the causal mask blocking every position from seeing tokens ahead of it

Why Does This Exist?

Building a language model used to mean picking a task-specific objective for whatever you wanted the model to do — one setup for translation, another for summarization, another for question answering, each with its own labeled dataset. That approach hits a wall fast: labeled task data is expensive and comparatively small, while raw text is nearly unlimited. GPT's founding bet, going back to 2018, was that one simple, unsupervised objective — predict the next word, everywhere, on any text at all — would generalize far better than anyone expected, precisely because it never runs out of training signal.

That objective needs an architecture built for it. GPT is a decoder-only encoder-decoder-architectures-family model: every layer is causally masked, so a position can attend to itself and everything before it, never anything after. That restriction isn't a limitation bolted on afterward — it's what makes the objective coherent at all. Train a model to predict what comes next while letting it see the answer, and it learns nothing; block the answer and you get a genuine prediction task, one you can run at essentially unlimited scale on unlabeled text.

Think of It Like This

Finishing someone else's sentence, one word at a time, forever

Picture someone who has read an enormous amount of writing and gotten extremely good at one specific party trick: given any sentence fragment, guess the single most likely next word. Not the next ten words — just the next one. Then, having guessed it, treat their own guess as part of the sentence and guess the next word after that. Repeat enough times and a full paragraph comes out, built entirely from a long chain of "what word probably comes next" decisions.

That's GPT's entire generation process, described exactly. Nothing more mysterious happens underneath — the trick is just extraordinarily well-tuned, because guessing the next word well, across every kind of writing a person could produce, turns out to require modeling grammar, facts, and reasoning patterns implicitly, even though none of those were ever the stated goal.

How It Actually Works

The objective: one word, over and over

GPT trains on causal language modeling: given tokens 11 through tt, predict token t+1t+1, for every position in every training sequence at once. There's no separate "understanding" phase and "generation" phase — the exact same forward pass that predicts token 51 from tokens 1 through 50 during training is what runs, one step at a time, during actual generation. The diagram above shows this loop: predict, append the result, feed the whole sequence back in, predict again.

Why one objective generalizes so widely

Predicting the next word accurately across a huge and varied body of text isn't actually a narrow skill. To guess correctly that "the capital of France is ___" ends in "Paris," a model needs a fact. To guess that "she picked up the phone and ___" is more likely to continue with an action than a noun, it needs grammar. To finish a step-by-step math explanation correctly, it needs to have tracked the reasoning so far. None of these were separately labeled as training targets — they emerge as byproducts of getting extremely good at the one stated task, because getting that one task right, at scale, requires learning all of them anyway. This is the mechanism behind what how LLMs work covers at a higher level: a single training signal, applied at enough scale, becoming general capability.

Sampling, not just picking the top word

At inference, GPT doesn't always take the single highest-probability next token — that tends to produce repetitive, flat text. Instead it samples from the predicted probability distribution over the vocabulary, with various strategies controlling how much randomness to allow. The mechanics of that choice belong to next-token prediction; what matters here is that the underlying model output is always a full probability distribution over every possible next token, and generation is a policy for choosing from it.

The one-token-ahead limit

Training this way only ever rewards a model for getting the very next token right — it has no direct signal about whether token 51 sets up a good token 55. That short-horizon pressure is a real limit of the plain next-token objective, and it's the gap that motivates multi-token prediction: predicting several tokens ahead during training, not just one, without changing what runs at inference.

Show Me the Code

A toy autoregressive loop: predict a distribution over a tiny vocabulary from the last token, pick the top choice, append it, repeat.

import numpy as np

def softmax(z: np.ndarray) -> np.ndarray:    e = np.exp(z - z.max(axis=-1, keepdims=True))    return e / e.sum(axis=-1, keepdims=True)

def generate_step(seq: np.ndarray, w: np.ndarray) -> np.ndarray:    logits = seq[-1] @ w                  # predict from the last token only    probs = softmax(logits)    next_token = np.zeros_like(seq[-1])    next_token[np.argmax(probs)] = 1.0     # greedy pick, for a deterministic demo    return next_token

rng = np.random.default_rng(0)w = rng.normal(scale=0.5, size=(4, 4))seq = rng.normal(size=(3, 4))               # 3-token starting sequencefor step in range(2):    seq = np.vstack([seq, generate_step(seq, w)])print(seq.shape)  # -> (5, 4) — two new tokens appended, one at a time

Each new row is generated from everything before it and then appended — exactly the feed-forward, feed-back loop the diagram draws, run twice.

Watch Out For

Assuming decoder-only means the model can't see much context

Causal masking blocks a position from seeing ahead, not from seeing everything before it. A GPT-style model at token 2,000 has full access to tokens 1 through 1,999 — the restriction is about direction of attention, not the amount of prior context available. Confusing "can't look forward" with "has narrow context" is a common misreading of what causal masking actually restricts.

Treating next-token prediction as trivially simple because the task description is simple

"Predict the next word" sounds narrow enough to dismiss, and it's easy to underestimate what a model has to learn to do it well at scale. The pitfall runs the other way too: don't assume the objective grants a model genuine long-horizon planning just because impressive text comes out. The training signal is still exactly one token ahead, every time — any longer-range coherence is a byproduct of scale and data, not something the objective directly rewards.

The Quick Version

  • GPT is a decoder-only, causally-masked transformer trained on one objective: predict the next token, everywhere, in any text.
  • The same forward pass used in training runs at inference, one step at a time, feeding each generated token back in as input.
  • Getting next-token prediction right at scale forces the model to implicitly learn grammar, facts, and reasoning patterns, without any of those being separate training targets.
  • Generation samples from a predicted probability distribution rather than always taking the top choice.
  • The objective only ever rewards the very next token, which is the gap multi-token prediction exists to address.
  • BERT is the encoder-only counterpart trained on the opposite kind of objective, masked language modeling.
  • Multi-Token Prediction addresses the one-token-ahead limit this page names as the objective's real constraint.
  • Encoder-Decoder Architectures is where GPT's decoder-only design sits among the other transformer variants.
  • How LLMs Work covers the broader picture of how this one training signal becomes general capability.
  • Next-Token Prediction covers the sampling strategies this page's generation step glosses over.

Related concepts