Reinforcement Learning

Q-Learning and SARSA

Q-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

status: reviewimportance: importantdifficulty 4/5math: graduateread: 22mlive demo

Concept Structure

Q-Learning and SARSA

01Intuition

Start with the picture, metaphor, or geometric mechanism.

02Math

Make the objects explicit and connect them with notation.

03Code

Mirror the equations with runnable implementation details.

04Interactive Demo

Manipulate the mechanism and watch the idea respond.

1prerequisites
2next concepts
4related links

Learner Contract

What this page should let you do.

You are here becauseQ-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

This Reinforcement Learning concept is the current idea: keep the same invariant visible across Intuition, Math, Code, Interactive Demo.

By the end4/4 sections ready | runnable code expected | live demo

Explain the mechanism, trace the main notation, and test one prediction in the live demo.

Do this firstIntuition

Read the intuition before the notation; the math should name a mechanism you already felt.

Test the linkManipulate one control and predict the visible change.Then continue to Function Approximation in RL (review)

Claim/source review status

Substantive review recorded

1/1 claims have bounded review metadata; still check caveats and source scope.Metadata-derived; review may be AI-assisted. Not a human certification.
Claims1/1 reviewed
Sources3 cited
Codeattached
Demolive
Reviewed2026-07-03
Updatedpage 2026-07-03

Learning item flow

4/4 sections readyAsk about thisResearch room
ConceptQ-Learning and SARSAReinforcement Learning
3 sources attachedLocal snapshot ready
concept:reinforcement-learning/q-learning-sarsa
01

01

Intuition

Build the mental picture first so the rest of the page has something to attach to.

Section prompt

Temporal-difference learning updated a state value. Control needs one more question:

If I was in this state and took this action, how good was that action?

That is why Q-learning and SARSA learn Q(s,a)Q(s,a), an action value, instead of only V(s)V(s). Both methods observe one transition,

St,At,Rt+1,St+1,S_t, A_t, R_{t+1}, S_{t+1},

then update the value of the action that was just taken.

The split happens at the next state. SARSA asks, "What action did my behavior policy actually sample next?" Q-learning asks, "What is the best next action according to my current table?"

When exploration chooses the same action that the table already thinks is best, the two updates match. When exploration chooses a different action, SARSA learns about the exploratory path it is actually following, while Q-learning learns toward the greedy target path.

02

02

Math

Translate the story into symbols, assumptions, and a derivation you can inspect.

Section prompt

Assume a finite discounted MDP and a tabular action-value estimate Q(s,a)Q(s,a). At time tt, the agent observes state StS_t, takes action AtA_t, receives reward Rt+1R_{t+1}, and lands in St+1S_{t+1}.

SARSA is on-policy one-step TD control. It samples the next action At+1A_{t+1} from the current behavior policy, then updates with

Q(St,At)Q(St,At)+α[Rt+1+γQ(St+1,At+1)Q(St,At)].Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha \left[ R_{t+1} + \gamma Q(S_{t+1},A_{t+1}) - Q(S_t,A_t) \right].

The name SARSA comes from the quintuple

(St,At,Rt+1,St+1,At+1).(S_t,A_t,R_{t+1},S_{t+1},A_{t+1}).

Q-learning is off-policy one-step TD control. It can act with an exploratory behavior policy, but its target uses the greedy next action under the current table:

Q(St,At)Q(St,At)+α[Rt+1+γmaxaQ(St+1,a)Q(St,At)].Q(S_t,A_t) \leftarrow Q(S_t,A_t) + \alpha \left[ R_{t+1} + \gamma \max_a Q(S_{t+1},a) - Q(S_t,A_t) \right].

If St+1S_{t+1} is terminal, the bootstrap term is zero for both algorithms.

The important comparison is not "which algorithm is always better." It is what each update is estimating from the same experience:

  • SARSA target: reward plus the sampled next action value.
  • Q-learning target: reward plus the largest next action value.
  • Behavior policy: the policy that generated the data.
  • Target policy: the policy the update is evaluating or improving toward.
03

03

Code

Keep the implementation aligned with the notation so the algorithm is legible.

Section prompt
gamma = 0.90
alpha = 0.40
current_q = 1.10
reward = 0.20

next_q = {
    "repeat": 2.40,  # greedy under the table
    "ask": 1.00,     # sampled by exploration
    "skim": 0.60,
}

sampled_next_action = "ask"

sarsa_target = reward + gamma * next_q[sampled_next_action]
q_learning_target = reward + gamma * max(next_q.values())

for name, target in [
    ("SARSA", sarsa_target),
    ("Q-learning", q_learning_target),
]:
    error = target - current_q
    updated = current_q + alpha * error
    print(name, "target", round(target, 3), "error", round(error, 3), "new Q", round(updated, 3))

The same observed transition can produce different targets because the next action used by the bootstrap is different.

04

04

Interactive Demo

Use direct manipulation to connect the explanation to a moving system.

Section prompt

Use the Q-learning vs SARSA Target Lab to predict which update changes the current action value more.

Choose a transition case, inspect the current Q(St,At)Q(S_t,A_t) and the next-state action values, then commit to whether SARSA, Q-learning, or neither should update more. After reveal, the lab shows the sampled next action, the max next action, both targets, both TD errors, and the new value each algorithm would write.

The terminal case and sampled-greedy case are there on purpose: they show when the algorithms collapse to the same one-step target.

Live Concept Demo

Explore Q-Learning and SARSA

The stage is code-native and interactive. Use it to test the explanation against the mechanism.

difficulty 4/5graduatecode-aligned
Demo Prediction Checkpoint

Manipulate one control and predict the visible change.

Commit to what Q-Learning and SARSA should make visible before reading the result.

After The First Pass

Turn the concept into an inspected object.

Once the invariant is visible in the intuition, math, code, and demo, use these panels to inspect the mechanism visually, check source support, practice the idea, and attach a grounded research question.

Mechanism Storyboard

See the idea move before the page explains it

Q-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

Prediction open01 / Intuition
Prediction lens

Start with the picture, metaphor, or geometric mechanism.

Commit first

Before reading further, choose the kind of change Q-Learning and SARSA should make visible.

Visual Inquiry

Make the image answer a mathematical question

Q-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

4/4 stages readyLive demo connected
Prediction

Which visible object should carry the first intuition?

Commit first

Pick the cue that should make Q-Learning and SARSA easier to reason about before the page gives the answer.

Source Grounding

Canonical references for the mechanism on this page.

book · 2018Reinforcement Learning: An Introduction, Second EditionRichard S. Sutton and Andrew G. Barto

Canonical source for SARSA as on-policy TD control, Q-learning as off-policy TD control, the sampled next action versus max-over-actions distinction, and cliff-walking behavior contrast.

Open source
course-notes · 2026Stanford CS234: Lecture 4, Model-Free Control and Function ApproximationStanford CS234

Graduate RL source for on-policy/off-policy definitions, tabular Q-learning update, SARSA update, epsilon-greedy behavior policy, and function-approximation caveats.

Open source
course-notes · 2026UC Berkeley CS188: Model-Free LearningUC Berkeley CS188

Course-note source for model-free learning, active Q-learning, feedback-driven policy improvement, and the exploration requirement.

Open source

Claim Review

Q-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

Status1 substantive review recorded

Claims without a substantive review badge still need exact source-support review.

Sources3 references

sutton-barto-2018-rl, stanford-cs234-model-free-control, berkeley-cs188-model-free-learning

Local checks4 local checks

Use equations, runnable code, and demos to check whether the source support is operational.

Substantively reviewedIn finite tabular one-step TD control, SARSA updates Q(S_t,A_t) toward R_{t+1} + gamma Q(S_{t+1},A_{t+1}) using the next action actually sampled by the behavior policy, while Q-learning updates Q(S_t,A_t) toward R_{t+1} + gamma max_a Q(S_{t+1},a), making the target greedy even when the behavior policy explores.Claim metadata: source checked

The sources support finite tabular one-step action-value TD control where both algorithms update the visited state-action pair, but SARSA bootstraps from the sampled next action and Q-learning bootstraps from the greedy next action.

Sources: Reinforcement Learning: An Introduction, Second Edition, Stanford CS234: Lecture 4, Model-Free Control and Function Approximation, UC Berkeley CS188: Model-Free LearningFinite tabular teaching MDP only; excludes Expected SARSA, n-step methods, eligibility traces, Double Q-learning, DQN, replay buffers, target networks, continuous spaces, and function-approximation convergence failures beyond a caveat note.A bounded review summary is present; still check caveats and exact reference scope.

Checked Sutton/Barto Chapter 6 for SARSA's quintuple update and Q-learning's off-policy max-over-next-actions update; checked Stanford CS234 Lecture 4 for on/off-policy definitions, Q-learning and SARSA equations, epsilon-greedy behavior, and tabular convergence caveats; checked Berkeley CS188 for model-free Q-learning as active reinforcement learning with exploration. GPT Pro publication critique remains pending because 127.0.0.1:51672 is unavailable.

Reviewer: codex-local-source-review; reviewed 2026-07-03

Practice Loop

Try the idea before it explains itself

Q-learning and SARSA both update the action just taken; SARSA bootstraps from the next action the behavior policy sampled, while Q-learning bootstraps from the largest next action value in the current table.

Readiness0/3 checks ready
Predict

Before touching the demo, predict one visible change that should happen in Q-Learning and SARSA.

Hint 1

Reveal when your model needs a nudge.

Hint 2

Reveal when your model needs a nudge.

Hint 3

Reveal when your model needs a nudge.

Grounded research drawerClose
ConceptQ-Learning and SARSAReinforcement Learning

Research Room

Attach the question to a claim, equation, code, or demo

Pick the concept, equation, source, runnable code, claim, misconception, or demo state before asking for help. The handoff keeps that page item in context.
Next local actionNo local draft saved yet

Open the draft below to save one note and next action in this browser.

conceptReinforcement Learning

Q-Learning and SARSA

Attached question

What is the smallest example that makes Q-Learning and SARSA click without losing the math?

Local action draftNo local draft saved yetExpand only when ready to capture one local next action
Local action draft

This draft stays in this browser, attached to the selected learning item.

No local draft saved.
Evidence to inspect
  • References to inspect: attached references on this page.
  • Definition, prerequisite, and contrast concept links
  • The equation or runnable code that makes the concept operational
  • One demo state that shows the invariant instead of a slogan
What would resolve this
  • The learner can state the mechanism in their own words
  • The learner can name the prerequisite that would repair confusion
  • The learner can predict how the mechanism changes under one perturbation
Grounded AI handoff

I am working in Continuous Function's research reading room. Object: concept - Q-Learning and SARSA Selected item key: recorded for copy. Context: Reinforcement Learning Page anchor: recorded for copy. Open question: What is the smallest example that makes Q-Learning and SARSA click without losing the math? Evidence to inspect: - References to inspect: attached references on this page. - Definition, prerequisite, and contrast concept links - The equation or runnable code that makes the concept operational - One demo state that shows the invariant instead of a slogan What would resolve this: - The learner can state the mechanism in their own words - The learner can name the prerequisite that would repair confusion - The learner can predict how the mechanism changes under one perturbation Answer as a careful research tutor: stay source-grounded, separate verified evidence from assumptions, name the relevant math objects, and end with one next action.

View it in context
concept/concept-notebook/reinforcement-learning/q-learning-sarsa concept:reinforcement-learning/q-learning-sarsa