Blog / Artificial Intelligence

What do you need to learn to understand how LLMs work?

Four pieces of math (vectors, matrices, probability, and derivatives) explain almost everything a large language model does.

Level
Beginners
Prerequisites
High-school algebra (variables, simple equations); comfort reading short formulas; no programming or machine learning background needed
A grey chalkboard covered in handwritten math equations.
Photo by Artturi Jalli on Unsplash
On this page
  1. 1.Introduction
  2. 2.An LLM does one job: predict the next token
  3. 3.Vectors give every token a place in meaning space
  4. 4.The dot product measures how related two vectors are
  5. 5.Matrices are the model's learned transformations
  6. 6.Softmax turns raw scores into probabilities
  7. 7.Attention mixes context using dot products and softmax
  8. 8.Training follows the gradient to reduce the loss
  9. 9.Learn the math in the order the model uses it
  10. 10.Conclusion
  11. 11.Related Notes

Introduction#

Most explanations of large language models (LLMs) either stay vague ("it learned from the internet") or jump straight into papers full of symbols. Neither tells you what you actually need to learn. The good news is that the math is smaller than it looks. After reading this article, you will be able to name the four areas of math behind an LLM, say where each one appears inside the model, and know the order in which to study them.

This guide is for curious developers and students who have high-school algebra and nothing more. We will follow one sentence, "The cat sat on the ___", through the whole model and watch it become the word "mat". Figure 1 shows that journey in its simplest form; every section fills in one box.

Figure 1 — An LLM turns text into numbers, mixes them, and ends with one predicted token.

We start with the one job an LLM does, then climb through vectors, matrices, probability, attention, and training.

An LLM does one job: predict the next token#

Before any math, it helps to know what the model is trying to do. An LLM reads a piece of text and predicts what comes next, one small piece at a time. That is the whole job. Writing an email, answering a question, or producing code is this one step repeated: predict a piece, add it to the text, predict again.

The small pieces are called tokens. A token is a word or part of a word, chosen by a tool called a tokenizer (see Related Notes). In our sentence, "The", " cat", " sat", " on", and " the" are each one token, and the model's job is to guess the token that follows, which we hope is " mat".

A computer cannot predict anything from letters alone. It needs numbers it can compare, combine, and adjust. Every piece of math in the rest of this article exists to answer one question: how do we turn tokens into numbers, and numbers back into a good guess?

Takeaway: an LLM is a next-token predictor, so the first problem is turning tokens into numbers.

Vectors give every token a place in meaning space#

Now that we know the model needs numbers, the first tool is the vector. A vector is simply an ordered list of numbers, such as [0.9, 0.7, 0.1]. You can picture it as an arrow, or as a point, in a space with one direction per number.

An LLM keeps one vector for every token it knows. This vector is called an embedding, and you can think of each number as a score on some hidden trait of meaning. Real models use hundreds or thousands of numbers per token; to keep things readable, our toy model uses three, which we can label "animal", "pet", and "object" (see Figure 2).

Figure 2 — Toy embeddings: "cat" and "dog" have similar numbers, "mat" does not.
Token animal pet object
cat 0.9 0.7 0.1
dog 0.8 0.8 0.0
mat 0.0 0.1 0.9

The model is not told these numbers. It starts with random ones and learns them during training, which we cover later. What matters now is the idea: words with similar meanings end up with similar vectors, close together in space.

Takeaway: a token becomes a vector, so "similar meaning" can become "similar numbers". Next we need a way to measure that similarity.

With every token stored as a vector, we can ask how related two tokens are. The tool for this is the dot product: multiply the two vectors number by number, then add up the results. The answer is a single number, and a bigger number means the vectors point in a more similar direction.

Figure 3 applies it to our toy embeddings. "cat" and "dog" share high scores on "animal" and "pet", so their dot product is large. "cat" and "mat" share almost nothing, so theirs is small.

Plain text
cat · dog = 0.9×0.8 + 0.7×0.8 + 0.1×0.0 = 0.72 + 0.56 + 0.00 = 1.28
cat · mat = 0.9×0.0 + 0.7×0.1 + 0.1×0.9 = 0.00 + 0.07 + 0.09 = 0.16

Figure 3 — The dot product gives a high score to related tokens and a low score to unrelated ones.

This one operation is used everywhere inside an LLM. Whenever the model needs to ask "how much does this relate to that?", it computes a dot product. Keep it in mind, because attention is built from it.

Takeaway: the dot product turns "how similar are these?" into one number. Next we see how the model reshapes vectors before comparing them.

Matrices are the model's learned transformations#

The dot product compares vectors as they are, but the model often needs to look at them from a different angle first. The tool for this is the matrix: a grid of numbers that turns one vector into another. Applying a matrix to a vector is called matrix multiplication, and each number in the output is just a dot product between one row of the matrix and the vector.

Here is a tiny case. Take the matrix with rows [1, 0, 0] and [0, 0, 1]. Multiplying it by cat [0.9, 0.7, 0.1] gives [0.9, 0.1]: the first row keeps the "animal" score and the second keeps the "object" score. The matrix has chosen which traits matter for a particular question.

An LLM uses this to give each token two new vectors (see Figure 4). One matrix produces a query, which describes what the token is looking for. Another produces a key, which describes what the token can offer to others. The numbers inside these matrices are learned, and together with the embeddings they make up the model's parameters (see Related Notes).

Figure 4 — Two learned matrices turn the same token vector into a query and a key.

Takeaway: matrices reshape vectors so the model can ask specific questions. Before we put queries and keys to work, we need a way to turn scores into choices.

Softmax turns raw scores into probabilities#

So far the model produces scores, and scores can be any size. To make a decision, it needs probabilities: numbers between 0 and 1 that add up to 1, where each one says how likely an option is. The function that converts scores into probabilities is called softmax.

Softmax works in two steps. First it raises e (about 2.718) to the power of each score, which makes every value positive and stretches the gaps between big and small scores. Then it divides each result by their total, so they add up to 1. In symbols: p_i = e^(s_i) / sum of e^(s_j).

At the end of the model, every token in the vocabulary gets a score for "what comes after The cat sat on the". Suppose four candidates score mat 3.0, floor 2.0, sofa 1.5, and dog 0.0. Softmax turns them into the probabilities in Figure 5, and the model's best guess is "mat" with 61%.

Figure 5 — Softmax keeps the ranking of the scores but turns them into shares that add up to 1.

Takeaway: softmax turns any list of scores into probabilities. With dot products, matrices, and softmax in hand, we can now build attention.

Attention mixes context using dot products and softmax#

Our three tools come together in attention, the idea at the heart of modern LLMs. Attention lets each token look back at the tokens before it and decide which ones matter for understanding it. Without it, "the" at the end of our sentence would know nothing about the cat that sat.

Here is how it works for the last token, "the". Its query is compared, with a dot product, to the key of every earlier token. That gives one score per token: The 0.1, cat 2.0, sat 1.5, on 1.0. Softmax turns those scores into weights that add up to 1, shown on the arrows in Figure 6. "cat" gets the largest share because it is the most useful clue.

Figure 6 — The last "the" pays most attention to "cat", the clue that matters most.

The weights are then used to blend information. A third matrix gives each token a value, the information it hands over, and the new vector for "the" is the weighted average of those values: 47% from "cat", 29% from "sat", and so on. In practice, models also divide the scores by the square root of the vector length before softmax, so very long vectors do not produce huge scores that push all the weight onto one token.

Takeaway: attention is dot products plus softmax plus a weighted average. The last question is how the model learns all these numbers.

Training follows the gradient to reduce the loss#

Every embedding and matrix we have seen starts as random numbers, so a fresh model guesses badly. Training fixes this by measuring how wrong each guess is and nudging the numbers to be a little less wrong. The measure of wrongness is the loss, and the nudge comes from derivatives.

For next-token prediction, the loss is the negative logarithm of the probability the model gave the correct token (see Related Notes for logarithms). If the model gives "mat" a probability of 0.61, the loss is −ln(0.61) ≈ 0.49. If it had given "mat" only 0.03, the loss would be −ln(0.03) ≈ 3.51. Confident and right means a small loss; confident and wrong means a large one.

To shrink the loss, the model needs to know which way to move each parameter. A derivative answers exactly that: it tells you how much the loss changes when one number changes a little. The list of derivatives for all parameters is called the gradient. Each parameter then takes a small step against its derivative: new = old − learning_rate × gradient. With a parameter of 0.5, a derivative of 0.8, and a learning rate of 0.1, the parameter becomes 0.42.

Figure 7 — Training is a loop: predict, measure the error, find the slope, take a small step.

This loop, called gradient descent, runs billions of times over huge amounts of text. Because a model has many layers, the derivatives are linked together with the chain rule from calculus, which the next section puts on your study list.

Takeaway: the loss measures the error and the gradient shows how to reduce it. You have now seen every piece; next we put them in study order.

Learn the math in the order the model uses it#

You have now followed one sentence through the whole model, and each step used one area of math. The best study plan follows the same order, because each area builds on the one before. Figure 8 lists them with what to learn first and where you have already seen it.

Figure 8 — A four-step study path that mirrors the path a token takes through the model.
Step Learn Where the LLM uses it
1 Vectors and the dot product Embeddings, similarity, attention scores
2 Matrices and matrix multiplication Queries, keys, values, every layer
3 Exponentials, logarithms, basic probability Softmax, the loss
4 Derivatives and the chain rule Gradients, training

Just as useful is knowing what you can skip at first. You do not need proofs, eigenvalues, integrals, or advanced statistics to understand how an LLM works. Aim to compute each operation by hand on tiny numbers, as we did with "cat" and "mat"; once that feels easy, the formulas in papers become readable.

Takeaway: study vectors, then matrices, then probability, then derivatives, and practise each one on toy examples.

Conclusion#

An LLM does one job, predicting the next token, and four areas of math make that possible. Vectors turn tokens into numbers, and the dot product measures how related they are. Matrices reshape those vectors into queries, keys, and values. Softmax turns scores into probabilities, and attention combines all three to mix context. Training uses the loss and derivatives to improve every number, as Figure 9 summarises.

Figure 9 — The overview again, each step labelled with the math it uses and the training loop closing it.

Your next step: pick up the dot product and work through it on paper with three toy vectors of your own. For a full, beginner-friendly path through each area, see The Math Behind Large Language Models by Samy Ferradji (github.com/samiferr/math-behind-llm).

  • Transformer block: A transformer block is the repeating unit of an LLM: an attention step followed by a small feed-forward network. Models stack dozens of these blocks.