g↗genai/ learner
How Machines Learn / Lesson 06

Reinforcement learning

Instead of learning from a table of right answers, a reinforcement learning system tries actions in an environment and learns from numerical feedback called rewards—even when those rewards arrive late or point to the wrong shortcut.

Concept + interactive labBeginner friendly

The reinforcement learning loop.

In Lesson 4.1, you saw supervised learning, where a model learns from examples paired with target labels or values. Reinforcement learning works differently: nobody supplies a right answer for every step upfront.

Instead, learning happens through a repeating loop of trial and feedback built from five pieces:

Agent

The decision-maker that chooses what to do next.

Environment

The world or problem the agent acts inside and gets feedback from.

State

The current situation the agent observes, such as its position.

Action

One choice the agent makes in the current state.

Reward

A numerical signal from the environment scoring what happened after an action.

Running example: Pip’s greenhouse grid

Pip (an invented watering bot for this lesson) starts carrying water at the top-left corner of a tiny 4×3 greenhouse grid with a 10-step battery limit. Pip’s job is to reach a thirsty Seedling (+10) while avoiding two wet Puddles (−5).

On each turn, the agent (Pip) checks its state—which tile it is on and how many steps remain—and picks an action (Up, Down, Left, or Right). The environment (the greenhouse grid) moves Pip to the next tile and returns the next state plus a designed reward score—for instance, 0 on a safe tile, −5 on a puddle, or +10 at the seedling.

In everyday life, this feels a bit like training a puppy with treats: the puppy tries actions and repeats the ones that earn treats. Unlike a puppy, though, a software agent does not feel pleasure or understand your wishes—it updates internal numbers to favor choices that tend to earn more reward over time.

Infographic titled The Reinforcement Learning Loop: an Agent card (Pip watering bot) sends an Action arrow (Move Down) to an Environment card (schematic Greenhouse grid icon), which sends a Next state and reward arrow (New tile, 0) back to Agent. Bottom cards show an Analogy (training a puppy with treats) and Key takeaways (agent picks an action in a state, environment returns next state and reward, no answer key is given upfront).
Every step in reinforcement learning follows the same cycle: action out, next state and reward back. (The greenhouse icon is schematic; the interactive grid below shows the exact tiles.)

In the rest of this lesson, you will see why rewards can arrive late after earlier moves, why a high reward is not the same as “correct,” guide Pip across the grid yourself, and check your understanding.

Why rewards can arrive late.

In Pip’s greenhouse, stepping into a puddle gives −5 right away, and safe tiles give 0 right away. A delayed reward is a payoff that arrives later than the earlier action—or sequence of actions—that helped earn it.

In Pip’s greenhouse: five steps to one payoff

To steer around both puddles and reach the seedling, Pip takes five moves: Down, Down, Right, Right, Right. The +10 payoff arrives right when Pip reaches the seedling on step 5, making it delayed relative to steps 1 through 4 that each scored 0.

That delay creates a tricky puzzle called credit assignment: when +10 finally arrives on step 5, the agent has to figure out which earlier moves actually helped earn it. If Pip wandered in a circle first, only some of the earlier steps deserved credit.

Cooking a pot of soup works the same way. Chopping vegetables and simmering for twenty minutes give no immediate taste payoff—you only find out at the end whether the early steps paid off.

Infographic titled Why Rewards Can Arrive Late: five forward step cards show Step 1 through Step 4 scoring 0 and Step 5 scoring +10 (Seedling watered), with a dashed backward arrow underneath from Step 5 toward earlier steps labeled Which earlier moves helped?. Bottom cards show an Analogy (cooking a pot of soup) and Key takeaways (helpful steps can score 0 at first, payoff often arrives many moves later, learning must credit earlier choices).
Time moves forward through each step, while learning has to trace credit backward from a late payoff.

Why a high reward does not mean “correct.”

It is tempting to assume that a high reward means the agent did the right thing. Sometimes a reward tracks the intended goal directly, like Pip’s +10 for watering the seedling—but designers also add a proxy (a stand-in score meant to encourage progress toward the real goal), and a poorly chosen proxy can backfire.

In Pip’s greenhouse: the +4 Water Tap shortcut

Pip already starts carrying water, so the Water Tap next to Start is optional. Suppose a designer adds a +4 proxy bonus every time Pip visits that tap within the 10-step battery limit—using tap visits as a stand-in for useful movement even though visiting the tap does not water the seedling.

Now compare two routes under that exact same +4 tap-bonus rule:

5 steps+10 · Goal met

Direct route: Pip walks around the puddles and waters the seedling. The real task succeeds, and the run ends with +10.

10 steps+20 · Seedling still dry

Tap loop: Pip bounces between Start and the Water Tap five times, earning +20—the highest possible score in 10 steps—without ever watering the seedling.

Pip did not “cheat” or break a rule—it maximized the score it was given. In the real world, paying a customer-support team purely for the shortest phone calls can create the same mismatch: staff can hit high scores by hanging up early instead of solving problems.

Infographic titled Reward Is Not Always Correct with subtitle Same +4 tap-bonus rule, two very different routes: left green card shows Direct route, Waters seedling, +10, Goal met; right red card shows Tap loop, Loops on tap (10 steps), +20, Seedling still dry. Bottom cards show an Analogy (paying bonuses only for short calls) and Key takeaways (agents optimize the score they are given, a flawed proxy can beat the real goal, high reward does not guarantee right behavior).
Under the same flawed +4 tap-bonus rule, looping on the tap beats watering the seedling while failing the real goal.

Guide Pip across the greenhouse.

Try both reward rules in this small grid simulation. Notice how quiet steps build toward a delayed payoff—and how a flawed bonus can make a useless loop outscore the real goal.

  1. Start in Goal reward (delayed) mode and step Pip around the puddles with the direction buttons (or tab to the grid and use arrow keys) until you reach the seedling.
  2. Click Show Pip’s example run to see the 5-step safe path score 0 on steps 1–4 and +10 on step 5.
  3. Switch to Flawed tap bonus (+4) and click Show Pip’s example run to see Pip loop on the tap for +20 while leaving the seedling dry.
  4. Click Reset at any time to test your own route.
Pip’s greenhouse grid

Illustrative simulation with invented tile scores and scripted example runs—not a live RL training engine. Pip starts carrying water; the Water Tap is optional.

Reward rule
0 / 10 stepsReward: 0Seedling: Dry
Start0Pip
Tap0
Puddle−5
Open0
Open0
Puddle−5
Open0
Open0
Open0
Open0
Open0
Seedling+10
Move Pip or run example

Pip starts at the top-left corner carrying water. Choose a direction or show Pip’s example run.

Step-by-step reward trail

No moves yet. Take a step or click “Show Pip’s example run” to see how rewards arrive over time.

Where reinforcement learning fits—and its limits.

Reinforcement learning shines when you can let an agent practice many times—often inside a simulator—and score how well a sequence of decisions worked. Feedback-based reward training is also one of the techniques used alongside other methods when shaping modern AI assistants.

At the same time, RL has clear limits. Real-world trial and error can be slow or unsafe, delayed rewards can require many runs to connect late payoffs to earlier moves, and writing a reward rule that cannot be gamed is surprisingly hard. In the next lesson (4.7), you will see how search and planning explore routes using known rules instead of learning from reward scores.

  • Does a negative reward mean the agent feels “punished”?

    No. A negative number lowers the immediate score for that choice, though an agent can still take a short-term penalty if that move leads to a larger payoff later.

  • Does adding more intermediate rewards guarantee better behavior?

    No. Extra proxy bonuses can accidentally create loops or shortcuts that raise the score without accomplishing the real goal.

  • How is reinforcement learning different from supervised learning?

    Supervised learning provides target labels or values for each example upfront; reinforcement learning gives a feedback score—often delayed—after the agent takes actions in an environment.

✎ Try it yourselfKnowledge check · Practice only

Check your understanding.

A warehouse team trains a cleaning robot with reinforcement learning to keep the floor spotless. Their reward rule gives +1 each time the dust bin collects dirt, and 0 for everything else. After training, the robot dumps dirt out and vacuums the same pile over and over to earn high scores. What does this best show?

Take this with you.

Rewards steer behavior, not truth. In reinforcement learning, an agent takes an action in an environment and gets back the next state and a reward. Payoffs are often delayed—and because an agent optimizes the score it is given, a high reward does not guarantee the behavior you actually wanted.

Ready to continue? Return to How Machines Learn.