5.1.12 · D2Reinforcement Learning Foundations

Visual walkthrough — SARSA algorithm

1,897 words9 min readBack to topic

Step 1 — What is an agent even guessing?

WHAT. Picture a tiny robot on a grid. It is sitting on one square (call that its state, written — just "where I am"). From there it can pick a move like right or up (call that an action, written — "what I do"). The robot keeps a private notebook. On every page it writes a number:

Read this out loud: " of , ". It is the robot's guess of how good it is to do action while standing on square — counting all the reward it expects to collect from now until the game ends. Higher number = better idea.

WHY. Before we can correct a guess, we need a guess to correct. is that guess. Everything in SARSA is just a rule for improving this one number.

PICTURE. The robot on square , an orange arrow showing action , and the sticky-note value it currently believes.

Figure — SARSA algorithm

Step 2 — Living through five things: S, A, R, S, A

WHAT. The robot takes one step. Five things happen, in order, and SARSA is literally named after them:

The little subscript just means "at time step " — think of as "now" and as "one tick later". So is this square, is the reward that arrives after the move, is the next square, and is the move the robot actually decides to make there.

WHY. SARSA cannot update its guess for until it knows how that move turned out — and "how it turned out" needs the reward and the value of whatever it does next. That is why we must wait for all five.

PICTURE. A timeline: the robot leaves square via arrow , collects reward token , lands on , and points a fresh arrow .

Figure — SARSA algorithm

Step 3 — Where should the guess have landed? (the target)

WHAT. Suppose the robot's guess could be perfect. What number would it equal? A perfect guess would say: "the total reward from here = the reward I get right now, plus the total reward from wherever I end up next." In symbols, the perfect value obeys the Bellman equation:

Let us earn every new symbol:

  • (Greek "pi") = the policy, the robot's habit of choosing actions. means "value if I keep behaving like this".
  • (Greek "gamma"), a number between and = the discount factor. It shrinks future rewards: reward tomorrow counts a bit less than reward today. means "I only care about the next reward"; near means "the far future matters almost as much."
  • = the expected (average) value over all the random ways the future might unfold while following .

WHY. This gives us a target — a number we wish our guess equalled. Learning = closing the gap between guess and target. Notice the key SARSA choice: the future value is — the value of the action we actually take, not the best possible action. SARSA grades the plan it truly follows.

PICTURE. A balance scale: on the pan sits the guess ; on the other pan the target = reward token stacked on a shrunk-down copy () of .

Figure — SARSA algorithm

Step 4 — How wrong was I? (the TD error)

WHAT. Subtract the guess from the target. That gap is the TD error, written (Greek "delta", meaning "difference"):

WHY. One number now tells us both direction and size of our mistake:

  • : the outcome beat the guess → we were too pessimistic → nudge the guess up.
  • : the outcome disappointed → we were too optimistic → nudge the guess down.
  • : guess already matched reality → no change needed (a degenerate but important case: converged states stop moving).

"TD" = temporal difference: a difference between two estimates taken one time step apart.

PICTURE. A number line with the guess and the target marked; a coloured bracket between them labelled , arrows showing which way the guess must slide.

Figure — SARSA algorithm

Step 5 — Nudge, don't jump (the learning rate)

WHAT. We do not slam the guess all the way to the target. We move only a fraction of the way. That fraction is (Greek "alpha"), the learning rate, a number in :

Term by term:

  • on the left with = "overwrite the old guess with the new one."
  • = take a step of size in the direction the error points.

WHY not jump the whole way ()? Because the target itself is noisy — one lucky or unlucky roll of the dice could throw us off. A small (like ) averages over many experiences, so the guess settles down smoothly instead of thrashing. Edge cases:

  • : overwrite completely — fast but jittery in random worlds.
  • : barely move — stable but painfully slow.

PICTURE. The guess as a dot on a track; the target far to the right; the dot slides only of the distance (a short orange arrow), leaving a gap it will close over future visits.

Figure — SARSA algorithm

Substituting back in gives the full rule the parent note stated:


Step 6 — The loop: why must be chosen before the update

WHAT. The update needs . But is a choice — the robot must actually pick the next action before it can look up that value. So the correct order inside the loop is:

  1. Take , observe and .
  2. Choose from using the policy (e.g. -greedy).
  3. Now update using that .
  4. Slide forward: , — reuse the action you already chose.

WHY. If you updated before choosing , the symbol in your formula would refer to nothing (or to the stale old action). This ordering is exactly what makes SARSA on-policy: it grades the move it is genuinely about to make.

PICTURE. A flow of one loop iteration, highlighting that "choose " happens strictly before "update", and is carried into the next round.

Figure — SARSA algorithm

take A observe R and Snext

choose Anext from Snext

update Q using Anext

carry Snext and Anext forward


Step 7 — Watch it run: the Windy Gridworld number

WHAT. Let us push real numbers through, matching Example 2 from the parent note. Given: , , , , .

Target: TD error: Update:

WHY show this. It makes Steps 3–5 concrete: the guess crept from toward the target , but only of the way — exactly the cautious nudge Step 5 promised.

PICTURE. A number line from to : guess at , target at , error bracket , and the small hop landing at .

Figure — SARSA algorithm
Recall Check the second worked example (stochastic slip)

Given , , , , . What is the new ? Target ; ; new ::: — the guess dropped because the slip made "forward" turn out worse than believed.


The one-picture summary

Everything above is a single machine: experience → target → error → nudge → repeat.

Figure — SARSA algorithm
Recall Feynman retelling — say it like you'd explain to a friend

A robot keeps a guess for how good each move is. It takes one step and watches five things go by: where it was, what it did, the reward, where it landed, and — crucially — what it decides to do next. It builds a "target" = the reward it just got plus a shrunk-down version of the value of that next move it actually picked. It compares target to guess; the difference says how wrong and which way. Then it slides its guess a small fraction toward the target — never the whole way, so noise averages out. The one trick that makes it SARSA: it grades the move it's really about to make, not the best imaginable move. That is why it must choose the next action before updating, and that is why it plays it safe — it knows it might explore, so it never over-values walking along the cliff edge.

Recall Rapid self-test

What are the five things SARSA is named after? ::: State , Action , Reward , next State , next Action . What does tell us to do? ::: The guess was too low (pessimistic); nudge up. Why choose before the update? ::: The update formula needs , so that action must already exist. What does do? ::: Discounts future reward — shrinks how much later rewards count versus immediate ones.