Lesson 1 · The problem
Learning by trial and error
The problem
A small robot sits in the bottom-left corner of a grid with 4 columns and 3 rows. One cell is a wall. The top-right cell is the goal, worth +1.
The cell under the goal is a pit, worth −1. Reaching either one ends the game.
Nobody gives the robot a map. Nobody tells it where the goal is, or which way to go. It can only try a move and see what happens. Over many tries, it has to learn to reach the goal and avoid the pit.
Learning to act from this kind of experience is reinforcement learning. This course builds it up from this grid to the methods used to train game-playing programs and chat models.
We name each cell by (column, row), counting from (1, 1) in the bottom-left corner:
| column 1 | column 2 | column 3 | column 4 | |
|---|---|---|---|---|
| row 3 | (1,3) | (2,3) | (3,3) | (4,3) goal, +1 |
| row 2 | (1,2) | wall | (3,2) | (4,2) pit, −1 |
| row 1 | (1,1) start | (2,1) | (3,1) | (4,1) |
In this lesson every move goes exactly where it's meant to. A move into the wall or off the edge leaves the robot where it is. The next lesson makes the moves slippery.
The agent and the environment
Reinforcement learning splits the world in two.
- The agent is the learner. Here it's the robot. It decides what to do.
- The environment is everything else. Here it's the grid. It takes the agent's move and says what happened.
They take turns, in a loop:
- The agent sees the state: what the situation is right now. Here the state is the robot's cell, such as (1,1).
- The agent picks an action: one of its choices. Here there are four: N, E, S and W (north, east, south, west).
- The environment answers with the next state and a reward: a number that says how good that step was. Here it's +1 for entering the goal, −1 for entering the pit, and 0 for every other move.
Then the loop repeats from the new state. One trip round the loop is one step.
Below, the agent has no idea what to do, so it picks one of the four moves at random every time. Each move has chance 1/4: a categorical distribution with four equal chances. Step through it. Each step comes in two halves: the agent acts, then the environment answers and a row is added to the log.
Tap a cell to inspect it.
reset(): the agent starts at (1,1).
Total reward so far: 0
| t | state | action | reward | next |
|---|---|---|---|---|
| Press play or step forward. | ||||
Things to notice:
- The reward is 0 on almost every step. Only the last step says anything: +1 or −1.
- Many moves bump into the wall or an edge and change nothing.
- Random moves are poor. Out of 2,000 random episodes from (1,1), 1,307 ended in the pit and 693 at the goal. Half of them took 26 moves or more.
Predict first
A random agent starts at (1,1). Which is it more likely to enter first: the goal or the pit?
Writing the loop down
Number the steps The agent is in state and picks action . The environment answers with the next state and the reward .
The reward gets the label because it arrives together with the next state. So is the reward for the first step, for the second, and so on. Each letter is a random variable: before the step happens, you don't know which value it will take.
Episodes
Each game, from the start until the robot enters the goal or the pit, is one episode. The goal and the pit are terminal states: entering one ends the episode. Then the robot goes back to (1,1) and a new episode starts.
Write down everything that happened in one episode, in order, and you get its trajectory:
Here is the trajectory of the shortest route to the goal, five moves: N, N, E, E, E.
| 0 | (1,1) | N | 0 | (1,2) |
| 1 | (1,2) | N | 0 | (1,3) |
| 2 | (1,3) | E | 0 | (2,3) |
| 3 | (2,3) | E | 0 | (3,3) |
| 4 | (3,3) | E | +1 | (4,3), the goal |
The episode lasted five steps. The rewards were .
What is the agent trying to do?
The agent wants to collect as much reward as it can over the whole episode, not only on the next step. On the grid, every route to the goal earns the same +1. The Returns and discounting lesson makes "as much as it can over time" precise, and shows why sooner is better.
To do that, the agent needs a rule for choosing actions in every state. That rule is called a policy. The next lesson defines it properly. The random agent above follows the simplest policy there is: "pick any move".
Which move earned it?
Here is one episode by the random agent. It reached the goal after 12 moves, so it earned +1. Tap a move in the strip to see where it happened on the grid.
Predict first
The agent reached the goal after 12 moves. Was its third move, E from (2,1), a good move?
Tap a cell to inspect it.
Total reward: 1
Move 3: E from (2,1), reward 0
Tap a move in the strip (or use the arrow keys), then test it.
Try move 3 first. Of 200 episodes that take E from (2,1) and then move at random, 34 reach the goal and 166 fall in the pit. So the episode above was lucky.
Then try the last move, E from (3,3). All 200 episodes reach the goal, because that move enters it.
Does that mean move 3 was bad? Not quite. E from (2,1) starts a route to the goal: E, N, N, E gets there in four moves.
The 200 episodes went badly because they moved at random afterwards. How good a move is depends on what you do next. The values lesson turns this into numbers.
This is the central puzzle of reinforcement learning. It's called credit assignment. A reward arrives late, and the agent has to work out which earlier moves deserve the credit, or the blame.
Chess has the same puzzle. You play 60 moves and lose. Was it an early mistake, a slip near the end, or weak play all through? The final result alone doesn't say.
Feedback that grades, not teaches
In supervised learning, every example comes with its right answer, its label. If the robot learned that way, a teacher would say "in (1,1), the right move is N" after every move.
Reinforcement learning never gets that. The environment only says what happened next and how good it was. Pick a cell and a move, and compare the two kinds of feedback.
Agent at (1,1)
Supervised learning: a teacher
Pick a move.
Reinforcement learning: the world
Pick a move.
Tap a cell to put the agent there.
Feedback that says how good an outcome was, but not what the right action would have been, is evaluative feedback. It makes reinforcement learning harder than supervised learning in three ways:
| Supervised learning | Reinforcement learning | |
|---|---|---|
| What you're told | the right answer | a reward: how good it went |
| When you're told | right away, for each example | often much later (credit assignment) |
| What data you see | a fixed dataset | only what your own actions lead to |
The last row matters a lot. The random agent rarely visits (3,3), so it rarely learns anything about it. An agent has to try things to find out about them. The exploration lessons are about how to do that well.
Michael Littman's review of the field (Nature, 2015) describes reinforcement learning by these three features. Its feedback is evaluative, sequential (decisions affect later states) and sampled (you only see what you try).
The loop in code
Every environment in this course has the same two-call interface:
reset()starts a new episode and returns the start state.step(a)takes actionaand returns one step: the state, the action, the reward, the next state, and whether the episode has ended.
The environments are tiny Python classes in the book's rl_envs package. They behave exactly like the TypeScript ones that run the figures. Here the agent plays the five-move route to the goal:
def play(actions: list[int], seed: int = 0) -> list[E.Transition]:
"""Drive the environment by hand: reset() once, then step(a) until the episode ends."""
env = E.Env(GRID, seed=seed)
env.reset() # back to the start cell, (1,1); returns its state id
log = []
for a in actions:
t = env.step(a) # one turn of the loop: (state, action, reward, next state, done)
log.append(t)
if t.done: # entered the goal or the pit
break
return logThe log comes back as five steps. The last one has reward 1 and done set to True. There are no tensors yet. Tensors arrive in the next lesson, where the environment's rules become a table of numbers.
DigressionThe reset() / step() interface, and GymnasiumWhere this interface comes from, and the one difference from the standard library. · 2 min
Most RL code uses the same loop, popularised by OpenAI's Gym library. Its maintained successor is Gymnasium, run by the Farama Foundation. It has many ready-made environments, from cart-pole to Atari games.
Gymnasium's step(action) returns five things: the observation (the state), the reward, terminated, truncated and an info dictionary. The two flags split our single done:
terminatedmeans the episode truly ended, as when the robot enters the goal or the pit.truncatedmeans it was cut short from outside, for example by a time limit of 500 steps.
The difference matters for learning. A terminated episode has nothing after it. A truncated one would have carried on, so its future still counts. Our small environments never truncate, so one done flag is enough.
reset() in Gymnasium returns the observation and an info dictionary, and takes a seed so runs can be repeated.
DigressionRL in the wildBackgammon, Atari, Go and reasoning language models: four results that made the field. · 4 min
The same loop, with far bigger states and much more experience, is behind some well-known results. Each one gets its own lesson later in the course.
TD-Gammon (1992–1995). Gerald Tesauro at IBM trained a neural network to play backgammon by playing against itself, hundreds of thousands of times. The only reward was winning or losing at the end of each game, so credit assignment was the whole problem. It learned with temporal-difference learning, the idea in the TD learning lesson, and reached a strong human level (Tesauro, 1995).
Atari (2015). A DeepMind team trained one deep Q-network, the subject of the deep Q-networks lesson, on 49 Atari 2600 games. It saw only the screen pixels and the score, and used the same network and settings for every game. It scored at least 75% of a professional human tester's score on 29 of them (Mnih et al., Nature 2015). Reading pixels used a convolutional network.
AlphaGo (2016). AlphaGo combined neural networks trained partly by reinforcement learning with tree search, the idea in the MCTS lesson (Silver et al., Nature 2016). In March 2016 it beat Lee Sedol, one of the world's top Go players, 4 games to 1.
DeepSeek-R1 (2025). A language model writes an answer one word-piece at a time, so writing an answer is an episode. DeepSeek trained models on maths and coding problems where a program can check the final answer and give a reward. Their R1-Zero model was trained with reinforcement learning alone, with no human-written worked solutions. It learned to write longer reasoning that checks its own steps (Guo et al., Nature 2025). The Large language models course covers this kind of training at scale.
Common misconceptions
"A reward of 0 means the move was bad." On the grid, almost every move earns 0, including every move on the shortest route. A 0 only means "nothing happened yet". Whether a move was good depends on the rewards that come after it.
"The last move before a reward gets all the credit." E from (3,3) entered the goal. But the moves that got the robot to (3,3) mattered too. Spreading credit back over earlier moves is the job of credit assignment.
"Reinforcement learning is supervised learning with rewards as labels." A label says what the right answer was. A reward says only how good the outcome was, often many steps later, and only for the action you took.
"The agent knows the rules of the grid." In this lesson it doesn't. It knows the four actions and sees states and rewards, nothing else. Some later lessons give the agent the rules to plan with. Others make it learn from experience alone.
Recap
- An agent acts in an environment. Each step, it sees the state, picks an action, and gets back a reward and the next state.
- An episode runs from the start to a terminal state. Its trajectory is
- The agent's aim is as much reward as possible over the whole episode, not only the next step.
- Credit assignment is the puzzle of working out which earlier moves earned a later reward. One episode can't answer it.
- Reinforcement learning gets evaluative feedback: how good the outcome was, never what the right action was. It only sees what its own actions lead to.
- Code names used:
rl_envs.Env,reset(),step(),rl_envs.gridworldandrl_envs.step.