Gradient Descent: The One Loop Behind Every AI, From a Tiny Model to an LLM
Gradient descent is the loop that trains almost every AI model, including large language models. We run it on two tiny examples with real numbers: finding one best number, and teaching a model to pick the right next word. Then we see what changes when the same loop is scaled up to an LLM.

The one-line idea
Every AI model is a calculation with adjustable numbers inside it. Training means changing those numbers until the model's mistakes get smaller. The method that does the changing is called gradient descent.
An LLM (large language model, the kind behind chat assistants) is trained with this same method. It is not a different trick. It is this loop, repeated an enormous number of times.
This article shows the loop on two tiny examples side by side, then explains what is different for an LLM.
| Example 1 | Example 2 | |
|---|---|---|
| Goal | Find the best value of one number | Teach a model to guess the next word |
| Why this one | The simplest case, so every step is clear | This is the same job an LLM does |
Part 1: What is "loss"?
To improve something, you first need to measure how bad it is right now. That measurement is a single number called the loss. Lower is better, 0 means perfect.
Example 1. We want to be 3 (the model does not know this). We measure the loss as the squared distance from 3:
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| Loss | 9 | 4 | 1 | 0 | 1 |
The loss is 0 only at . The model's job is to find that value.
Example 2. The model sees the words "the cat" and must guess the next word out of four options: cat, dog, sat, mat. The correct next word is sat.
The model gives each word a score (any number). To compare words fairly, scores are turned into probabilities (numbers between 0 and 1 that add up to 1). The formula is called softmax:
You do not need to memorise it. See what it does:
| Scores (cat, dog, sat, mat) | Probabilities |
|---|---|
| 0, 0, 0, 0 | 0.25, 0.25, 0.25, 0.25 |
| -0.25, -0.25, 0.75, -0.25 | 0.175, 0.175, 0.475, 0.175 |
Equal scores give equal probabilities. A higher score gives a higher probability.
The loss only cares about the probability given to the correct word:
| Probability of "sat" | 0.99 | 0.9 | 0.25 | 0.1 | 0.01 |
|---|---|---|---|---|---|
| Loss | 0.010 | 0.105 | 1.386 | 2.303 | 4.605 |
Giving the right word a high probability means low loss. Starting with all scores at 0, the loss is .
Part 2: What is the gradient?
We know how bad the model is. Now: which way should we change the number to make the loss smaller?
The gradient answers this. It is the slope: if I increase this number a tiny bit, how much does the loss change?
Example 1: check with real numbers. At the loss is 9. Move up by 0.01:
| Loss | 9 |
Loss changed by for a change, so the slope is about , roughly -6.
The exact formula is , which at gives . Matches.
- Negative gradient: increasing the number lowers the loss, so increase it.
- Positive gradient: decrease the number.
- Big gradient: the loss is very sensitive to this number.
Example 2. For softmax with this loss, the gradient of each word's score has a simple form:
At the start (all probabilities 0.25):
| Word | cat | dog | sat | mat |
|---|---|---|---|---|
| Gradient |
The correct word ("sat") has a negative gradient, so raise its score. The wrong words have positive gradients, so lower their scores.
Part 3: The update (the "descent")
Move each number against its gradient:
lr is the learning rate, a step size we choose. This is the "descent" in gradient descent: the loss goes down because every step moves against the slope.
Example 1 (lr = 0.1)
| Value | |
|---|---|
| Old | 0 |
| Gradient | -6 |
| New | |
| New loss | (was 9) |
Example 2 (lr = 1)
| Word | cat | dog | sat | mat |
|---|---|---|---|---|
| Old score | 0 | 0 | 0 | 0 |
| Gradient | 0.25 | 0.25 | -0.75 | 0.25 |
| New score | -0.25 | -0.25 | 0.75 | -0.25 |
| New probability | 0.175 | 0.175 | 0.475 | 0.175 |
New loss: (was 1.386). Both examples improved after one step.
Part 4: Repeat
Training is this loop, repeated:
- Measure the loss
- Compute the gradient
- Update the numbers
- Go back to 1
Example 1 (lr = 0.1)
| Step | Loss | |
|---|---|---|
| 0 | 0 | 9 |
| 1 | 0.600 | 5.76 |
| 3 | 1.464 | 2.36 |
| 10 | 2.678 | 0.104 |
| 30 | 2.996 | 0.00 |
Example 2 (lr = 1)
| Step | Probability of "sat" | Loss |
|---|---|---|
| 0 | 0.25 | 1.386 |
| 1 | 0.475 | 0.744 |
| 5 | 0.842 | 0.172 |
| 20 | 0.961 | 0.040 |
| 100 | 0.992 | 0.008 |
Notice in both: the loss falls fast at the start and slowly later. As the model improves, the gradient gets smaller, so steps get smaller. In Example 2, getting from 0.96 to 0.99 takes 80 more steps.
Part 5: The learning rate matters a lot
Example 1, same start (), different learning rates, after 30 steps:
| Learning rate | after 30 steps | Loss after 30 steps | What happened |
|---|---|---|---|
| 0.01 | 1.364 | 2.678 | Too slow. Still far from 3 |
| 0.1 | 2.996 | 0.00 | Good |
| 0.9 | 2.996 | 0.00 | Jumps back and forth over 3, still converges |
| 1.1 | -709.1 | 507,128 | Each jump overshoots by more than the last, so the loss explodes |
The learning rate is something the person training the model has to choose. Too small wastes time. Too big breaks training.
Part 6: So how is an LLM different?
An LLM does Example 2, scaled up. Here is what changes and what does not.
| Example 2 (ours) | An LLM | |
|---|---|---|
| Job | Guess the next word from 4 options | Guess the next token from tens of thousands of options (a token is a small chunk of text, often a word or part of a word) |
| Input | "the cat" | A long stretch of text before the guess |
| Where scores come from | 4 free numbers we adjust directly | A huge calculation with billions of adjustable numbers inside |
| Loss | Same idea | |
| Data | 1 example | An enormous amount of text |
| Learning loop | Loss, gradient, update, repeat | Same |
How training text becomes examples: take a piece of text, hide the next word, ask the model to guess it, measure the loss, update. Every position in every piece of text is a free example, because the correct answer is already in the text.
The one real addition is that the scores come from a long chain of calculations, not from free numbers. Finding the gradient for every adjustable number in that chain uses the chain rule again and again. That process has its own name, backpropagation, but it is still just "find each number's slope."
What this article does not cover
- How the model computes its scores. That part (in modern LLMs, an architecture called a transformer) is a separate topic.
- Training tricks. Real training updates using small batches of data at a time, and usually uses improved versions of gradient descent. The core step is still "move against the gradient."
- What happens after. Models are often trained further after this stage, which this article does not describe.
Glossary
| Term | Meaning |
|---|---|
| Loss | One number: how bad the model is right now |
| Gradient | Slope of the loss with respect to a number inside the model |
| Gradient descent | Repeatedly moving numbers against their gradient to lower the loss |
| Learning rate | Size of each step |
| Score | Any number the model gives a word |
| Softmax | Turns scores into probabilities that add up to 1 |
| Token | A small chunk of text, often a word or part of one |
| Backpropagation | Computing the gradient for every number in a multi-step calculation |
Full code
import numpy as np
# Example 1: find w that minimises (w-3)^2
w = 0.; lr = 0.1
for step in range(1, 31):
grad = 2*(w - 3)
w -= lr*grad
if step in (1, 3, 10, 30):
print(step, round(w, 4), round((w-3)**2, 4))
# Example 2: guess the next word (cat, dog, sat, mat), correct = sat
softmax = lambda z: np.exp(z)/np.exp(z).sum()
scores = np.zeros(4); correct = 2; lr = 1.0
for step in range(101):
p = softmax(scores)
loss = -np.log(p[correct])
if step in (0, 1, 5, 20, 100):
print(step, p.round(3), round(loss, 4))
grad = p.copy(); grad[correct] -= 1 # probability minus (1 for correct word)
scores -= lr*grad
Try: change lr in Example 1 to 1.1 and watch the loss explode, like the table in Part 5.
More from the lab

What Is a Neuron? Trying 7 Kinds on 3 Tiny Puzzles
A neuron is a tiny calculator: it takes numbers in, adds them up with weights, and gives one number out. We try 7 kinds of neuron, from the simplest to a small group of neurons, on three tiny puzzles (AND, OR, XOR). The kind of neuron barely mattered. What mattered was having more than one neuron working together.
Foundational
Neural Networks From Zero: Two Tiny Examples, Every Step Explained
Where neural networks are used, why almost every use reduces to predicting a number or choosing a category, and how a single neuron learns both. Every term is defined before it is used, and every step is shown with real numbers.