Skip to main content
MahiDev — Learn. Build. Become AI-ready.

Lesson 57

SARSA

Update an action value from the reward and from the next action the same policy actually takes. One example is on this page.

Intermediate12 min
SARSA MahiDev

What You Will Learn

  • Idea
  • Example
  • When to use it

Idea

SARSA is the on-policy cousin of Q-learning. The target is the reward plus the discounted value of the next action that was really taken. If the policy explores, those exploratory actions pull the values down. The learned policy matches how the agent behaved.

Example

python
next_q = 0.2
reward, gamma, q, alpha = 1.0, 0.9, 0.0, 0.5
updated = q + alpha * (reward + gamma * next_q - q)
print(f"Q: {updated:.2f}")

Output:

text
Q: 0.59

When to use it

  • Use it when the values should include the cost of exploring.
  • The next action must come from the same policy that chose the first action.
  • On the same numbers, SARSA is more cautious than Q-learning whenever the sampled next action is weak.

Loading playground…