Learn/Generative AI, RAG & Agents/MODULE 1: FOUNDATIONS — UNDERSTANDING AI AND LARGE LANGUAGE MODELS
Lesson 6 of 37 · 4 min read

1.5 How LLMs Actually Work: Tokens, Embeddings, and Predictions

Let's open the hood.

We'll cover the three core mechanisms inside an LLM: tokenization, embeddings, and next-token prediction.

Each is intuitive once you see it.

Step 1: Tokenization

A computer doesn't see text the way you do.

It sees numbers.

So before an LLM can do anything with text, the text must be converted into numbers.

The first step is tokenization: breaking the text into small pieces called tokens.

A token is usually a word or a part of a word.

For example, the sentence:

"The dog chased the cat."

Might be tokenized as:

["The", " dog", " chased", " the", " cat", "."]

Six tokens, including punctuation. (The leading spaces are usually attached to the words — that's a quirk of how it works.)

But tokens aren't always whole words.

Less common words get broken into pieces:

"Antidisestablishmentarianism" → ["Ant", "idis", "establish", "ment", "arian", "ism"]

This lets the model handle any word, even ones it has never seen, by combining familiar pieces.

Each unique token in the model's vocabulary gets assigned a number, called a token ID.

The model has a fixed vocabulary, typically around 50,000–100,000 tokens.

So the sentence above might internally become something like:

[464, 3290, 26172, 262, 3797, 13]

Now the model can work with numbers.

💡 Why this matters: When you hear that an LLM has a "context window of 200,000 tokens," that means it can handle about 150,000 words of text at once (since tokens average smaller than words). When you're billed for API usage, you're billed per token. Tokenization is a real-world thing you'll bump into.

Step 2: Embeddings

Raw token IDs (like 464 or 3290) don't carry meaning.

The number for "dog" doesn't tell us that "dog" is related to "puppy" or "wolf."

So the model converts each token ID into something called an embedding: a long list of numbers (often hundreds or thousands of numbers per token) that represents the token's meaning in a kind of mathematical space.

The crucial property of embeddings is that similar meanings end up close together in this space.

So "dog" and "puppy" will have embeddings that point in nearly the same direction. "Dog" and "automobile" will point in very different directions.

You can almost think of embeddings as the meaning of each token, expressed in a language of pure numbers.

Here's a famous example from early embedding research.

If you take the embedding for "king," subtract the embedding for "man," and add the embedding for "woman," you get something very close to the embedding for "queen":

king - man + woman ≈ queen

This is shocking the first time you see it.

It means the embedding space has captured something deep about concepts and their relationships, not just surface text.

Embeddings will return as a starring character when we get to RAG in Module 2.

Step 3: Next-Token Prediction

Once the input text is tokenized and embedded, the LLM processes it through its many layers of neural network computation.

The details of what happens in those layers are too much for this course (it involves something called attention, which is a way for the model to figure out which earlier tokens are relevant to predicting the next one).

The output of all this computation is a probability distribution over the entire vocabulary.

In plain English: for every possible next token, the model outputs a probability.

For an input like:

"The capital of France is"

the output might look like:

TokenProbability
" Paris"92%
" the"4%
" a"1%
" often"0.5%
......

The model then picks one token (usually the most likely, but with some randomness to keep responses varied) and appends it.

Then it does the whole process again, with the new token added to the input.

And again.

And again.

One token at a time, the response is built up.

That's the entire generation process.

Read it again: an LLM generates text one token at a time, each based on a probability prediction given everything written so far.

A Concrete Walk-Through

Let's say you ask an LLM:

"Tell me a joke about pizza."

Here's what happens (simplified):

  1. Your text is tokenized: ["Tell", " me", " a", " joke", " about", " pizza", "."]
  2. Each token is converted to an embedding.
  3. The model processes the embeddings, considering relationships between tokens.
  4. It outputs probabilities for the next token. Maybe " Why" wins.
  5. It now considers ["Tell", " me", " a", " joke", " about", " pizza", ".", " Why"] and predicts again. Maybe " did" wins.
  6. This continues until a "stop" token is generated or a length limit is reached.

The final output might be:

"Why did the pizza go to the doctor? Because it had a lot of crust!"

No part of that joke was retrieved from a database.

The model generated it, token by token, based on what tokens tend to follow other tokens in the patterns it learned during training.

Keep going with a free account

A free account saves your progress across lessons and lets you pick up where you left off.