Artificial intelligence website defines off-policy and on-policy learning as follows:

"An off-policy learner learns the value of the optimal policy independently of the agent's actions. Q-learning is an off-policy learner. An on-policy learner learns the value of the policy being carried out by the agent including the exploration steps."

I would like to ask your clarification regarding this, because they don't seem to make any difference to me. Both the definitions seem like they are identical. What I actually understood are the model-free and model-based learning, and I don't know if they have anything to do with the ones in question.

How is it possible that the optimal policy is learned independently of the agent's actions? Isn't the policy learned when the agent performs the actions?


11 Answers 11


First of all, there's no reason that an agent has to do the greedy action; Agents can explore or they can follow options. This is not what separates on-policy from off-policy learning.

The reason that Q-learning is off-policy is that it updates its Q-values using the Q-value of the next state $s'$ and the greedy action $a'$. In other words, it estimates the return (total discounted future reward) for state-action pairs assuming a greedy policy were followed despite the fact that it's not following a greedy policy.

The reason that SARSA is on-policy is that it updates its Q-values using the Q-value of the next state $s'$ and the current policy's action $a''$. It estimates the return for state-action pairs assuming the current policy continues to be followed.

The distinction disappears if the current policy is a greedy policy. However, such an agent would not be good since it never explores.

Have you looked at the book available for free online? Richard S. Sutton and Andrew G. Barto. Reinforcement learning: An introduction. Second edition, MIT Press, Cambridge, MA, 2018.

  • 17
    $\begingroup$ nice explanation! Your example on Q-learning is better formulated that in Sutton's book, which says: "the learned action-value function, Q, directly approximates Q* , the optimal action-value function, independent of the policy being followed. This dramatically simplifies the analysis of the algorithm and enabled early convergence proofs. The policy still has an effect in that it determines which state-action pairs are visited and updated." $\endgroup$ Jan 17, 2017 at 13:50
  • 31
    $\begingroup$ In general, I don't find Sutton and Barto very readable at all. I find the explanations they offer are not very comprehensible. I am not sure why their book gets recommended all over the place $\endgroup$
    – Motorhead
    Jun 7, 2018 at 6:22
  • 6
    $\begingroup$ @JakubArnold the original Sutton & Barto book is from 1998 and it does not cover deep reinforcement learning. The 2nd edition only mentions things like AlphaGo, but the focus of the book is in more classical approaches. If you want more RL resources, take a look at this list. I suggest David Silver's videos and Puterman's book as they are more approachable. For more theoretical material, I recommend Bertsekas' books. Take a look at the Spinning Up website for DRL algorithms and links to original papers. $\endgroup$ Feb 14, 2019 at 11:03
  • 2
    $\begingroup$ @AlbertChen "So in this case, it does depend on exploration or not": No, because both algorithms explore. The difference is how Q is updated. $\endgroup$
    – Neil G
    Jul 15, 2019 at 20:57
  • 1
    $\begingroup$ @peterbe Please just ask other questions. There are many errors in your reasoning. $\endgroup$
    – Neil G
    Aug 20, 2021 at 8:25

First of all, what does policy (denoted by $\pi$) actually mean?
Policy specifies an action $a$, that is taken in a state $s$ (or more precisely, $\pi$ is a probability, that an action $a$ is taken in a state $s$).

Second, what types of learning do we have?

  1. Evaluate $Q(s,a)$ function: predict sum of future discounted rewards, where $a$ is an action and $s$ is a state.
  2. Find $\pi$ (actually, $\pi(a|s)$), that yields a maximum reward.

Back to the original question. On-policy and off-policy learning is only related to the first task: evaluating $Q(s,a)$.

The difference is this:
In on-policy learning, the $Q(s,a)$ function is learned from actions that we took using our current policy $\pi(a|s)$.
In off-policy learning, the $Q(s,a)$ function is learned from taking different actions (for example, random actions). We don't even need a policy at all!

This is the update function for the on-policy SARSA algorithm: $Q(s,a) \leftarrow Q(s,a)+\alpha(r+\gamma Q(s',a')-Q(s,a))$, where $a'$ is the action, that was taken according to policy $\pi$.

Compare it with the update function for the off-policy Q-learning algorithm: $Q(s,a) \leftarrow Q(s,a)+\alpha(r+\gamma \max_{a'}Q(s',a')-Q(s,a))$, where $a'$ are all actions, that were probed in state $s'$.

  • 2
    $\begingroup$ "In off-policy learning, the $Q(s,a)$ function is learned from taking different actions (for example, random actions). We even don't need a policy at all!" - How can you not have a policy? Isn't even taking random actions technically a policy? Also it would be helpful if you could illucidate the difference in the Q update between SARSA and Q-Learning that show what makes either on policy or off policy. $\endgroup$
    – alex
    Jun 8, 2020 at 3:43
  • 2
    $\begingroup$ @alex If I understand correctly, a policy is a function of current state and environment, while taking random actions would not take current state/environment into account. I guess you could have a function that just outputs random actions no matter the input, but then whether that's an actual "policy" is debatable. $\endgroup$
    – chimbo
    Mar 16, 2021 at 21:49
  • $\begingroup$ So can we say that algorithms like policy gradient are a mix of on and off-policy learning? Because there is this exploration-exploitation rate that tells the RL when to be greedy and when to explore $\endgroup$ Nov 11, 2021 at 18:26
  • 1
    $\begingroup$ I don't really understand this distinction. Isn't $a' = \pi(a'|s')$ just equal to $a' = max_{a'} Q(s', a')$ in the off policy case? That's still a policy, just not one you need to store separately from the Q function. Does the distinction lie in the way we often keep a separate target Q in some flavors of RL that we only update with information from the working Q every $k$ iterations? $\endgroup$ Nov 15, 2021 at 17:12
  • $\begingroup$ @PavelKomarov Let's assume we use $\varepsilon$-greedy policy. Then in the on-policy formula $a'$ in $Q(s', a')$ stands for the $\varepsilon$-greedy action which is different from $\mathrm{argmax}_{a'} Q(s', a')$. $\endgroup$
    – Appliqué
    Mar 4, 2022 at 19:09

On-policy methods estimate the value of a policy while using it for control.

In off-policy methods, the policy used to generate behaviour, called the behaviour policy, may be unrelated to the policy that is evaluated and improved, called the estimation policy.

An advantage of this seperation is that the estimation policy may be deterministic (e.g. greedy), while the behaviour policy can continue to sample all possible actions.

For further details, see sections 5.4 and 5.6 of the book Reinforcement Learning: An Introduction by Barto and Sutton, first edition.

  • $\begingroup$ Think this is the clearest and most correct answer $\endgroup$
    – chris838
    Jan 13, 2023 at 8:51

The difference between Off-policy and On-policy methods is that with the first you do not need to follow any specific policy, your agent could even behave randomly and despite this, off-policy methods can still find the optimal policy. On the other hand on-policy methods are dependent on the policy used. In the case of Q-Learning, which is off-policy, it will find the optimal policy independent of the policy used during exploration, however this is true only when you visit the different states enough times. You can find in the original paper by Watkins the actual proof that shows this very nice property of Q-Learning. There is however a trade-off and that is off-policy methods tend to be slower than on-policy methods. Here a link with other interesting summary of the properties of both types of methods

  • 3
    $\begingroup$ Off-policy methods are not only slower, but can be unstable when combined with bootstrapping (i.e. how Q-learning builds estimates from each other) and function approximators (e.g. neural networks). $\endgroup$ Sep 1, 2017 at 16:50

On-policy learning: The same (ϵ-greedy) policy that is evaluated and improved is also used to select actions. For eg. SARSA TD Learning Algorithm

Off-policy learning: The (greedy) policy that is evaluated and improved is different from the (ϵ-greedy) policy that is used to select actions. For eg. Q-Learning Algorithm


From the Sutton book: "The on-policy approach in the preceding section is actually a compromise—it learns action values not for the optimal policy, but for a near-optimal policy that still explores. A more straightforward approach is to use two policies, one that is learned about and that becomes the optimal policy, and one that is more exploratory and is used to generate behavior. The policy being learned about is called the target policy, and the policy used to generate behavior is called the behavior policy. In this case we say that learning is from data “o↵” the target policy, and the overall process is termed o↵-policy learning."

  • $\begingroup$ if you follow this description, it's not easy to tell why Q-learning is off-policy $\endgroup$ Jul 10, 2019 at 12:46
  • $\begingroup$ What is the chapter? $\endgroup$ Jul 7, 2022 at 11:53

This is the recursive version of the Q-function (according to Bellman equation):

$$Q_\pi(s_t,a_t)=\mathbb{E}_{\,r_t,\,s_{t+1}\,\sim\,E}\left[r(s_t,a_t)+\gamma\,\mathbb{E}_{\,a_{t+1}\,\sim\,\pi}\left[Q_\pi(s_{t+1}, a_{t+1})\right]\right]$$

Notice that the outer expectation exists because the current reward and the next state are sampled ($\sim)$ from the environment ($E$). The inner expectation exists because the Q-value for the next state depends on the next action. If you your policy is deterministic, there is no inner expectation, our $a_{t+1}$ is a known value that depends only on the next state, let's call it $A(s_{t+1})$:

$$Q_{det}(s_t,a_t)=\mathbb{E}_{\,r_t,\,s_{t+1}\,\sim\,E}\left[r(s_t,a_t)+\gamma\,Q_{det}(s_{t+1}, A(s_{t+1})\right]$$

This means the Q-value depends only on the environment for deterministic policies.

The optimal policy is always deterministic (it always take the action that leads to higher expected reward) and Q-learning directly approximates the optimal policy. Therefore the Q-values of this greedy agent depends only on the environment.

Well, if the Q-values depends only on the environment, it doesn't matter how I explore the environment, that is, I can use an exploratory behaviour policy.


On-policy methods attempt to evaluate or improve the policy that is used to make decisions, whereas off-policy methods evaluate or improve a policy different from that used to generate the data. [1]

[1]. Reinforcement Learning: An Introduction. Second edition, in progress. Richard S. Sutton and Andrew G. Barto c 2014, 2015. A Bradford Book. The MIT Press.


In the way I understood it, hope it helps :

On-policy learning updates the policy currently in use while off-policy learning updates a different policy using the data collected from a different policy.

  • On-policy learning is a type of RL that updates the policy being used to take actions as the agent interacts with the environment. Specifically, the agent learns by following the current policy and then updates the policy based on the rewards received from those actions. (It is often used in situations where the agent's exploration of the environment is limited and the learning must be done with the current policy).

  • Off-policy learning, updates a different policy than the one being used to take actions. This approach involves learning from the behavior of an "older" policy (or another one), while simultaneously interacting with the environment using a newer, improved policy (currently learned). (The main advantage of off-policy learning is that it allows for greater exploration of the environment, which can lead to better policies). Technically, the book is called buffer.

Example: an agent playing a game of chess. With on-policy learning, the agent would learn by playing the game using its current policy and then update its policy based on the rewards it receives (experience is sampled from the updated policy). But with the off-policy learning, the agent might study a chess book to learn new strategies, and then incorporate those strategies into its policy while still playing games using its original policy (experience is sampled from the "book" policy here).

I recommend to read this following article : https://medium.com/@sergey.levine/decisions-from-data-how-offline-reinforcement-learning-will-change-how-we-use-ml-24d98cb069b0


I find this helpful: Michael Herrmann: On-Policy and Off-Policy Algorithm

  • $\begingroup$ Hi. While the link might be helpful to provide useful info pertaining to the query, linked-only answers are not useful in the long run for the links are prone to get dead. So, this post could be useful if you could add some context from the link that is relevant here. $\endgroup$ Oct 5, 2022 at 11:34

An off-policy learner learns the value of the optimal policy independently of the agent's actions

should be clarified as the data from which off-policy learner learns is independent of the agent's current policy (the policy in the moment the learner update its Q table(or Q network))

I prefer to understand why Q learnig is off-policy alongside DQN. In DQN, we can use memory buffer, where we definitely update the policy using data generated from old policy, which meets the definition above.

As for q learning, what is confusing is that actually we don't use memory buffer(at least in sutton's book), the learning data is actually generated, or more precisely, influenced by our current policy! That is more like on-policy! But wait, that doesn't mean we can't use it in completely off-policy manner. Like Dqn, memory buffer is also applicable to q learning.

Back to Q-learning vs SARSA, it is not hard to see that SARSA can't fit memory buffer, we can't use old-policy-generating data to update our q table. Becanse we use the current policy to estimate the next state's value V(s'). So Neil's answer did tell the crucial point behind all I say. But you can go further to fit the definition better.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.