Please enable JavaScript to use CodeHS

LLMs Under the Hood

How LLMs are pre-trained

Large language models learn by reading billions of examples and predicting the next word. This simple task, repeated over and over, is what builds world knowledge, grammar, and reasoning.

AI ~12 min Intermediate
The Big Idea

Next-token prediction

The simplest way to describe LLM pre-training is: predict the next token. The model is given a sequence of words and asked to guess what comes next.

Input: The cat sat on the ...

Target: mat

That does not sound very smart by itself, but when you do it trillions of times on books, articles, code, and conversations, the model starts to absorb facts, grammar, logic, and even style.

Key idea: next-token prediction forces the model to learn patterns in language, because the only way to get good at guessing the next word is to understand context.


CS Minute

Tokens and data

Before training, text is split into tokens. A token might be a whole word, part of a word, a punctuation mark, or even a common pair of words. The model never sees raw text; it sees a list of token IDs.

Example:

"Hello, world!" → ["Hello", ",", " world", "!"]

LLMs are trained on corpora — huge collections of text. More data, more patterns. But the quality of the data matters: a model trained mostly on messy, biased text will pick up those biases.


Math Minute

Probability distributions

At each step, the model outputs a score for every token in its vocabulary. Those scores are turned into probabilities with the softmax function.

Softmax turns scores into probabilities that add up to 1. If the model is very confident in one word, that word gets most of the probability.

During training, the model compares its predicted probability for the correct next token to 1 and pushes it higher. Over time, the right answers become more likely and the wrong answers become less likely.


Math Minute

The training loss

The standard loss for next-token prediction is cross-entropy loss. It measures how surprised the model is by the correct answer. If the model gives the right token a probability of 1, the loss is 0. If it gives the right token a probability near 0, the loss is huge.

Training loop: read a batch of text, predict the next token, compute loss, run backpropagation, update weights, repeat.

Modern LLMs do this for trillions of tokens across thousands of powerful GPUs for weeks or months.


Interactive

Train a tiny next-token model

Below is a bigram model: it learns which word tends to follow another word. Type or edit a small corpus, click Train model, then ask it to predict or generate text.

    Train the model and generate text to see the result here.

    This is a toy. Real LLMs use much larger vocabularies, context windows, and transformer architectures. But the core idea — predict the next token from context — is the same.


    Code

    A bigram model in Python

    The interactive demo above is a bigram model: it learns which word follows another by counting pairs. Here is the same logic in Python, from training on a tiny corpus to generating a short sequence.

    Real LLMs do the same thing, but instead of counting words they use a neural network to predict probabilities. They also look at much more context — many tokens, not just one.


    Your Turn

    Check yourself

    Why does the quality of the training data matter so much? What could go wrong if an LLM is trained mostly on unreliable sources?

    Your answer:

    Sample answer: The model learns patterns from whatever it sees. If the data is biased, incorrect, or toxic, the model can reproduce those patterns. Good training data helps the model be accurate, fair, and helpful.


    Check Yourself

    Quick quiz

    1. What is the main task LLMs are pre-trained on?

    2. What does softmax do?

    3. What does cross-entropy loss measure?

    Keep learning about LLMs

    Predicting the next token is powerful, but the architecture that makes it practical at scale is the transformer. Next, learn how attention lets the model focus on the right words at the right time.

    Part of the “LLMs Under the Hood” tutorial series.