SARSA Implementation
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
SARSA is a classic on-policy reinforcement learning algorithm. Its name comes from the update tuple State, Action, Reward, State, Action, which reflects exactly what the algorithm uses to update its Q-values. The central idea is that the agent learns from the action it actually plans to take next under its current policy.
The Update Rule
The tabular SARSA update is:
Q(s, a) = Q(s, a) + alpha * (r + gamma * Q(s_next, a_next) - Q(s, a))
The important difference from Q-learning is the use of a_next. SARSA does not update toward the greedy action in the next state unless the policy actually chooses that action.
That is why SARSA is called on-policy.
A Small Tabular Implementation
Here is a simple Python implementation with an epsilon-greedy policy and a toy environment.
This is not a production RL framework. It is the clearest way to see the algorithm itself.
Why the Next Action Matters
Suppose your policy is still exploratory. SARSA updates with the value of the exploratory next action, not with the value of the best-looking action in the table.
That makes SARSA more conservative in some environments because it learns the value of behaving according to its actual policy, including exploration.
Tuning the Main Parameters
The three core hyperparameters are:
- '
alpha: learning rate' - '
gamma: discount factor' - '
epsilon: exploration rate'
If epsilon is too high for too long, learning can stay noisy. If it drops too fast, the agent may stop exploring before it has discovered a good policy.
Common Pitfalls
A common mistake is implementing the Q-learning update rule by accident and then calling it SARSA. If you use max_a Q(s_next, a) instead of Q(s_next, a_next), you changed the algorithm.
Another mistake is not handling terminal states separately. In terminal states there is no next action value to bootstrap from.
A third issue is expecting tabular SARSA to scale to huge or continuous state spaces without function approximation. The tabular form is best for small discrete problems.
Summary
- SARSA is an on-policy temporal-difference control algorithm
- Its update uses the next action actually chosen by the current policy
- The core implementation needs a Q-table, an epsilon-greedy policy, and the SARSA update rule
- Terminal states should be handled without bootstrapping from a next action value
- SARSA is easiest to understand and debug in small discrete environments first
Related reading
- SARSA value approximation for Cart Pole
- Save and load model optimizer state
- Save and load model optimizer state
- Save classifier to disk in scikit-learn
- scala median implementation
- Scalable, Efficient Hierarchical Softmax in Tensorflow?
- Save classifier to disk in scikit-learn
- Save Keras model at specific epochs

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.