Worked examples — SARSA algorithm
This page is the practice ground for the SARSA algorithm. If you want the theory of why the update rule looks the way it does, read the parent note first. Here we hunt down every kind of situation the SARSA update can face and work each one to a number. (Prefer Hinglish? See the Hinglish version.)
Before anything else, let's re-anchor the one formula everything here depends on. From the parent:
Four plain-word reminders, because we will never use a symbol we have not re-earned:
- is the reward — a single number the environment hands back right after the agent takes action at state . The subscript is just bookkeeping: the action happens "at time ," the reward arrives "one tick later," so we tag it . Think of it as "the immediate payoff for the move I just made." A negative is a penalty, a positive one is a treat.
- (the learning rate) is a number between 0 and 1. It is the size of the step we take toward the new estimate. means "trust the new number completely"; means "move only 10% of the way."
- (the discount factor) is a number between 0 and 1. It is how much we care about the future. means a reward tomorrow is worth as much as one today; means we only care about the reward right now.
- is the value of the action we actually chose next — never the best action. That "actually chose" is the whole soul of SARSA.
The scenario matrix
Every SARSA update is built from a handful of independent "dials." The table below lists each dial and the values it can take. To claim we've covered everything, we must hit each row at least once across our examples.
| Cell class | What varies | Values we must show |
|---|---|---|
| A. Sign of TD error | Was reality better or worse than the guess? | (pleasant surprise), (disappointment), (nothing to learn) |
| B. Terminal next state | Does the episode end on this step? | forced to zero |
| C. Discount extremes | How much future matters | (myopic), (normal), (undiscounted) |
| D. Learning-rate extremes | Step size | (full jump), small (gentle) |
| E. Exploration action | Next action is not the greedy one | |
| F. Word problem | Real domain (safety) | Cliff-walking safe-path story |
| G. Exam twist | SARSA vs Q-learning on same tuple | Compute both, compare |
We will now work 8 examples that, taken together, light up every cell.
Forecast: Reward is a big and the old guess was negative — do you expect to go up or down, and by roughly how much with ?
- Target. . Why this step? The next state is terminal, so there is no future value to add — the of any action at a terminal state is defined to be . This is cell B.
- TD error. . Why this step? is (target − old guess). It is strongly positive (cell A+): the outcome crushed our pessimistic estimate.
- Update. . Why this step? We only step 10% of the way (cell D-small), so the guess climbs from toward but only reaches .
Verify: New value lies between old and target — a valid convex step. Distance closed , exactly . ✓
Forecast: The next Q () is less negative than the current one (). Will be positive or negative?
- Target. . Why this step? We add the reward to the future value, but first shrink that future value by (cell C, normal γ) — a future worth only counts as from here.
- TD error. . Why this step? is (target − old guess). It is positive (cell A+): even though every number is negative, the target () is higher than the old guess (), so our estimate was too pessimistic.
- Update. . Why this step? We nudge the old guess upward by 10% of the error (cell D-small): , so climbs to — small and cautious.
Verify: sits between and , a valid convex step. Distance closed . ✓ Matches parent note exactly.
Forecast: Old guess is a rosy . The reward is a penalty and future value only . Guess should drop — but how far with ?
- Target. . Why this step? Immediate reward is the penalty ; to it we add the discounted future, . The two combine to .
- TD error. . Why this step? is (target − old guess). It is negative (clean cell A−): reality () fell far short of our optimistic .
- Update. . Why this step? A negative error drags the value down: , so the rosy deflates to . This is exactly how SARSA learns to distrust the slippery "forward."
Verify: is between target and old , moved down by . ✓
Forecast: Compute the target first. If it equals the old guess, what happens to ?
- Target. . Why this step? With the future value is added at full weight, plus the reward , giving exactly .
- TD error. . Why this step? is (target − old guess), and here they coincide. When our guess already satisfies the Bellman relation, there is nothing to correct.
- Update. . Why this step? Any times zero is zero, so the value does not budge — a zero error means "leave it alone."
Verify: No change at all — this is the fixed point SARSA converges toward, where every has averaged out to zero. ✓ (cell A=0 covered.)
Forecast: With , does that enormous future value of matter at all?
- Target. . Why this step? erases the future entirely — the agent only cares about the immediate reward. The is multiplied away.
- TD error. . Why this step? is (target − old guess): , a positive surprise driven purely by the reward.
- Update. . Why this step? (cell D-full) means we throw away the old guess and jump straight onto the target — the full error is applied at once.
Verify: With the new equals the target exactly: . ✓ Under , SARSA degenerates into "learn only the immediate reward" — a sanity check on the extreme.
Forecast: Q-learning would use the best (). SARSA uses what we actually did (). Which gives a smaller target — and therefore teaches more caution?
- Target (SARSA). . Why this step? SARSA is honest about the exploratory action it took — it plugs in , not the max. This is cell E and the entire personality of SARSA.
- TD error. . Why this step? is (target − old guess): , negative because the exploratory detour is worth less than we assumed.
- Update. . Why this step? Half the negative error is applied: , pulling down to — SARSA lowers the value of a state whose exploration can hurt.
Verify: Had we (wrongly) used the max , the target would be and would rise to . SARSA's honest number instead lowers the value, penalising the risky region where exploration hurts. The two answers differ — proof that on-policy ≠ off-policy. ✓
Figure 1 — what to look for. The grid below shows the same world. The coral band along the bottom (marked "CLIFF, reward -100") is the danger zone. Trace the two routes from S (bottom-left) to G (bottom-right): the lavender solid line is SARSA's route — it climbs up one row first, runs safely across the top, then drops to the goal, never touching the cliff. The red dashed line is Q-learning's route — it hugs the cliff edge along the bottom row, one slip away from a . The mint arrow at the start is the single step we compute below: , taking the agent from to and updating to .

Forecast: From the figure, why would SARSA prefer the upper lavender route over the direct red edge route that Q-learning loves?
- Target. . Why this step? One step of movement costs ; the next state is fresh, so its chosen-action value is still , and keeps it at full weight.
- TD error. . Why this step? is (target − old guess): the initial guess was , so the error is just the step cost, .
- Update. . Why this step? Half the error is applied: , so the "up" action from the start now carries a small cost estimate of .
Verify: Matches parent note Step 1 exactly (). Why SARSA stays safe: along the cliff edge (the red route), an -greedy slip sometimes costs . Because SARSA plugs in the action it actually risks taking next (Example 6's logic), the cliff-edge cells accumulate that occasional , pushing their far down. The upper lavender route never touches the cliff, so its expected value stays higher — SARSA climbs. ✓
Forecast: Both share reward and old guess. The only difference is which next-Q they use: SARSA uses (chosen), Q-learning uses . Predict which ends higher.
- SARSA target. . → → . Why this step? SARSA uses the actually taken left action (). Its error is , and half of that () drops the value from to .
- Q-learning target. . → → . Why this step? Q-learning uses , ignoring what was really done. Its error is a big , and half () lifts the value from to .
- Difference. . Why this step? Subtracting the two updated values isolates the cost of honesty: Q-learning's optimistic assumption inflates the value by over SARSA's cautious one.
Verify: , , gap . On-policy honesty produces the more conservative value. ✓
Recall
Recall What forces the next-state value to zero, and when?
A terminal state — by definition , so the TD target collapses to just . Which cell used this ::: Example 1 (cell B).
Recall If
, what does the new equal? Exactly the TD target — we discard the old estimate entirely and jump onto it (Example 5).
Recall SARSA and Q-learning disagree only because of which quantity?
The next-state value: SARSA uses of the action actually chosen; Q-learning uses (Examples 6 and 8).