01 · MDPs & the RL Objective
What you're learning
The formal object every RL algorithm optimizes, built from the ground up: the Markov Decision Process, the return, policies, value functions, and the Bellman equations. By the end you should be able to write down , derive the Bellman expectation equations yourself, and see exactly why generating tokens from an LLM is an instance of this same structure — the seam we exploit in lesson 05.
This is lesson 01 of the rl-for-llms curriculum. Everything downstream — policy gradients, PPO, GRPO, RLHF — is a way of estimating or improving one of the quantities defined here. Get these anchors solid and the rest is machinery.
1. Learning map
graph TD A["Sequential decision problem<br/>(agent ↔ environment)"] --> B["Markov property<br/>state is sufficient"] B --> C["MDP tuple<br/>(S, A, P, R, γ)"] C --> D["Trajectory τ<br/>rollout"] D --> E["Return G_t<br/>discounted sum of reward"] C --> F["Policy π(a|s)"] E --> G["RL objective<br/>J(π) = E_τ[Σ γ^t r_t]"] F --> G G --> H["Value functions<br/>V^π, Q^π, A^π"] H --> I["Bellman expectation<br/>equations"] I --> J["Optimality<br/>V*, Q*, Bellman optimality"] J --> K["Greedy improvement"] G --> L["LLM as MDP<br/>(teaser → lesson 05)"] style G fill:#4a4,color:#fff style L fill:#a44,color:#fff style C fill:#44a,color:#fff
We build strictly bottom-up: the problem setup forces the Markov property, which lets us write the MDP, which gives us the return, which — combined with a policy — is the objective. Value functions are the bookkeeping that makes the objective tractable, and Bellman equations are the recursion that makes value functions computable.
2. Why this matters
Before any math, the motivation. Supervised learning optimizes a per-example loss against a fixed label. RL optimizes behavior against a scalar signal that (a) may be delayed, (b) depends on your own choices, and (c) doesn’t tell you what the right action was — only how good the outcome ended up being. That last point is the whole game: you get evaluative feedback (“that was worth 0.7”), not instructive feedback (“you should have done ”).
For LLMs this is the difference between next-token cross-entropy (imitate the data) and RLHF/RLVR (produce outputs a reward model or verifier scores highly). The moment you want a model to optimize for something you can score but can’t demonstrate token-by-token — helpfulness, correctness of a proof, a passing unit test — you are in the RL setting whether you call it that or not. This lesson gives you the object that setting optimizes.
The core reframe
In supervised learning the data distribution is fixed. In RL the distribution of states you visit depends on your policy — improving the policy changes the data. This coupling (policy → state distribution → objective → policy gradient) is the source of nearly every RL difficulty: non-stationarity, exploration, off-policy correction. Keep it in mind; it’s why RL is harder than it “should” be.
3. The problem setup
An agent interacts with an environment in discrete time steps . At each step:
- The agent observes a state .
- It selects an action .
- The environment returns a scalar reward and transitions to a new state .
graph LR A["Agent"] -->|"action a_t"| E["Environment"] E -->|"state s_{t+1}"| A E -->|"reward r_t"| A
The environment’s dynamics are captured by a transition distribution. In full generality this could depend on the entire history . That would be intractable — the history grows without bound and we could never estimate from finite data.
The Markov property
We make the problem tractable by choosing a state representation for which the future is conditionally independent of the past given the present:
This is the Markov property: the state is a sufficient statistic of the history for predicting the future. Nothing you’d learn by looking further back changes your prediction.
Markov is a property of the representation, not the world
Almost any process can be made Markov by folding enough history into the state. A raw camera pixel frame is not Markov (you can’t tell velocity from one frame); stack four frames and it is. So “is this Markov?” is really “did I put enough in the state?” This is exactly the lever we pull for LLMs in §10 — we define the state to be the full prefix, which is trivially Markov.
Markov ≠ memoryless in the colloquial sense
A Markov state can encode arbitrarily long dependencies — it just has to encode them in the state. The constraint is that the transition function looks only at the current state, not that the current state is small or forgetful.
Quiz: Is a partially observed environment (you see an observation , not the true state ) Markov in ?
Answer
Not in general. If is a lossy function of the true latent state, then can still depend on earlier observations that carried information about the hidden state. This is a POMDP (partially observed MDP). The standard fix is to build a belief state (a posterior over latent states) or a summary of history — e.g. an RNN/transformer hidden state — that is Markov. Recovering a Markov state from observations is a core modeling problem, and it’s why sequence models pair so naturally with RL.
4. The MDP formalism
A Markov Decision Process bundles everything above into a tuple:
| Symbol | Name | Meaning |
|---|---|---|
| State space | set of possible states | |
| Action space | set of possible actions (may depend on ) | |
| Transition kernel | dynamics; Markov by construction | |
| or | Reward function | expected immediate reward, |
| Discount factor | how much future reward is worth now |
Often an initial state distribution is included. Some formulations fold reward into the transition as a joint (this is Sutton & Barto’s convention1); it changes nothing structural.
Trajectories / rollouts
Running a policy in the MDP produces a trajectory (a.k.a. rollout, episode):
Its probability under policy factorizes cleanly because of the Markov property:
This factorization is the engine of policy gradients
Notice does not depend on the policy parameters, but does. When we later differentiate w.r.t. policy params, the environment terms drop out entirely — we never need to know or differentiate the dynamics. That’s what makes model-free policy gradients possible, and it’s the whole reason lesson 02 works. Anchor it now.
An episode is episodic if it terminates (reaches a terminal state, after which no more reward accrues) or continuing if it runs forever. LLM generation is episodic — it ends at EOS or a length cap.
5. The return: what “good” means over time
A single reward isn’t the objective — we care about cumulative reward from now on. Define the return from time as the discounted sum of future rewards:
which satisfies the recursion we’ll lean on constantly:
Why discount at all? Why ?
Three independent motivations converge on the same :
- Mathematical convergence. In a continuing task with bounded rewards , the undiscounted sum diverges. With it’s bounded: (geometric series). This alone forces for infinite-horizon problems.
- Uncertainty / hazard. can be read as a per-step survival probability: with prob the episode ends before the next step. Discounting = valuing reward you’re more likely to actually collect.
- Preference for sooner reward. Like a financial discount rate — reward now beats identical reward later.
sets the effective horizon
The effective planning horizon is roughly steps: steps, steps. Larger = more far-sighted but higher-variance and harder-to-optimize (credit must propagate further). It is a genuine hyperparameter of the problem statement, not just the algorithm.
is not innocuous for finite-horizon / sparse-reward tasks
Many LLM RL setups have a single terminal reward at step . Then , and the choice of silently down-weights long generations. In practice LLM-RL often uses (undiscounted, finite horizon) precisely to avoid penalizing length — and manages variance through other means (baselines, per-token advantages). Don’t cargo-cult into a sparse terminal-reward setting.
Quiz: With bounded reward and , what is the tightest upper bound on ?
Answer
. This bound is worth memorizing — it appears constantly in RL analysis (error bounds, value clipping ranges, etc.).
6. Policies
A policy is the agent’s behavior: a mapping from states to actions.
- Stochastic: is a distribution over actions. This is the general case and the one used for LLMs (the softmax over the vocabulary is a stochastic policy).
- Deterministic: , a single action per state. A special case, common in continuous control (DDPG2, TD33).
Why stochastic policies are the default in RL
Two reasons. (1) Exploration — a stochastic policy naturally tries different actions, which you need to discover which are good (§9). (2) Differentiability — for discrete actions, as a softmax gives a smooth objective you can take gradients through, whereas does not. For LLMs the policy is literally over the vocabulary, with the network weights and temperature controlling entropy.
A policy together with the MDP induces the trajectory distribution from §4. Everything we optimize is an expectation under this distribution.
7. The RL objective
Now we can state what RL actually optimizes. Given the induced trajectory distribution, the objective is the expected return:
The goal of RL is to find
That’s it. Every algorithm in this curriculum — REINFORCE, actor-critic, PPO, GRPO — is a strategy for climbing when you cannot compute it in closed form and can only sample trajectories.
What makes maximizing hard
You can’t differentiate naively: the reward comes from an environment you don’t have gradients through, and the distribution you’re averaging over depends on the parameters you’re optimizing. The trajectory factorization (§4) is precisely what lets us get an unbiased gradient estimate anyway — that’s the log-derivative / REINFORCE trick, and it’s the entire content of lesson 02. For now, just internalize: is the target.
Quiz: Two policies achieve the same expected return but has much higher variance in across episodes. Are they equally good to the RL objective as stated?
Answer
Yes — the objective as written, , is risk-neutral and cares only about the mean. Variance doesn’t enter. If you do care about worst-case or risk (a very live concern for deployed LLMs), you’re optimizing a different objective (e.g. CVaR, distributional RL, or a variance-penalized return). It’s worth being precise about this: much confusion comes from assuming the objective penalizes variance when it doesn’t unless you put it there.
8. Value functions: making the objective tractable
is a single number about the start. To improve a policy we need to know how good individual states and actions are. Enter value functions.
State-value function — expected return starting from state and following thereafter:
Action-value (Q) function — same, but you commit to action first, then follow :
They’re linked by averaging over the policy’s action choice:
Advantage function — how much better is action than the policy’s average behavior at :
The advantage is the single most important quantity for policy-gradient RL
means ” beats what I’d usually do here — do more of it.” means “do less.” Note by construction — the advantage is centered, which is exactly why subtracting as a baseline reduces gradient variance without adding bias (lesson 02). PPO4 and GRPO are, at their core, machinery for estimating well and taking controlled steps in its direction. GRPO’s trick is estimating the baseline from a group of sampled completions rather than a learned critic5 — but that’s getting ahead of ourselves.
Bellman expectation equations
Value functions have recursive structure — a direct consequence of . Take the definition of , peel off one step, and use the Markov property:
Written out over the distributions:
and analogously for :
These are the Bellman expectation equations61. They turn “sum an infinite horizon” into “one step of reward plus the discounted value of where you land.” That recursion is what makes value functions estimable — you can bootstrap from (temporal-difference learning) instead of waiting for full returns.
Bellman expectation vs. Bellman optimality — don't conflate them
The equations above evaluate a fixed policy (they’re linear in ). The optimality equations in §9 involve a (nonlinear) and characterize the best policy. “Bellman equation” is ambiguous — always know which one is meant.
Quiz: Derive the Bellman expectation equation for from in one line. Where exactly is the Markov property used?
Answer
. The last step — replacing with — is exactly where Markov is used: the future return depends on history only through the current state . Without Markov you couldn’t collapse the conditioning and the recursion wouldn’t close.
9. Optimality and greedy improvement
There exists an optimal value function — the best achievable value at each state over all policies:
A key theorem (for finite MDPs): there is always at least one deterministic optimal policy, and all optimal policies share the same .1 The optimal values satisfy the Bellman optimality equation — same recursion, but with replacing the policy expectation:
Greedy improvement (brief)
Given any , the greedy policy is guaranteed to be at least as good as (the policy improvement theorem). Alternating evaluate (compute ) and improve (go greedy) is policy iteration, and it converges to .1 Note the beautiful fact: is greedy w.r.t. its own — that self-consistency is exactly the Bellman optimality equation.
Why LLM-RL rarely uses pure greedy/value iteration
Value/policy iteration assume a known, enumerable and a tractable over actions. LLMs have a known transition (deterministic string append!) but an astronomically large state space and a -way action set — you can’t sweep states. So LLM-RL is almost entirely policy-gradient / actor-based (sample trajectories, estimate advantages, nudge ), not value-iteration-based. The value function still shows up, but as a baseline/critic (PPO) or an implicit group baseline (GRPO), not as the thing you over.
Quiz: The Bellman optimality equation replaces with . What property does this destroy, and why does it matter computationally?
Answer
It destroys linearity. The Bellman expectation operator is affine in , so evaluating a fixed policy is a linear system solvable in closed form. The makes the optimality operator nonlinear (piecewise-linear, convex), so you can’t just invert a matrix — you iterate the operator (value iteration), relying on it being a -contraction in to guarantee convergence to the unique fixed point .7 This contraction property is the theoretical backbone of nearly all value-based RL.
10. LLM connection: generation is an MDP
Here is the reframe that makes this entire curriculum apply to language models.8 Consider an LLM generating a response token by token. Map it onto the MDP tuple:
| MDP element | LLM instantiation |
|---|---|
| State | the prompt + all tokens generated so far: |
| Action | the next token |
| Policy | the model’s softmax over the vocabulary |
| Transition | deterministic append: |
| Episode / trajectory | the full generated sequence, ending at EOS or length cap |
| Reward | often 0 for all except the last — a single terminal score from a reward model or verifier |
Two structural features make the LLM MDP special:
- The transition is deterministic and known. Appending a token to a string has no stochasticity — is a delta function. All randomness lives in the policy. This is unusual and simplifying compared to control/robotics.
- The state is trivially Markov. Since is the entire prefix, the future depends on nothing but by construction — no POMDP headaches (§3). We got Markov for free by defining the state as the whole context.
Sparse, terminal reward is the defining challenge
In RLHF/RLVR the reward typically arrives only at the end: (or a 0/1 verifier), with otherwise. So for every step — every token in the sequence shares credit for one terminal scalar. How to distribute that single reward across all the token-actions that produced it is the credit-assignment problem, and it is exactly what the advantage function (§8) exists to solve. This is why PPO/GRPO spend all their effort estimating per-token advantages.
Don't over-read the analogy yet
The mapping is exact, but the algorithms need care: the action space is (huge softmax), the horizon is hundreds of tokens, and the “environment” (reward model) is itself a learned, gameable approximation. Reward hacking, KL-regularization to a reference policy, and length bias are all consequences we’ll tackle later. For now the takeaway is only the mapping itself.
Quiz: Given the deterministic-transition observation, simplify the trajectory probability (§4) for an LLM. What survives?
Answer
Since for the appended token and otherwise, every transition factor is . So — and with a fixed prompt, . That’s just the model’s sequence likelihood. The consequence is profound: is fully differentiable in , so the policy-gradient log-derivative term (lesson 02) is literally the per-token log-prob you already compute during training. RL on LLMs reuses the exact same forward/backward pass as pretraining — only the weighting changes. This is the seam lesson 05 opens up.
11. Reading order
Work through these in order:
- Primary text — value functions & Bellman: Sutton & Barto, Reinforcement Learning: An Introduction (2nd ed.), Chapter 3 (Finite MDPs) — the definitive treatment of §§3–9 here. Then Chapter 4 (Dynamic Programming) for policy/value iteration and greedy improvement. Free PDF: incompleteideas.net/book/the-book-2nd.html
- Intuition + notation you’ll reuse: OpenAI Spinning Up, “Part 1: Key Concepts in RL” — states/actions/policies, the RL objective , value functions, and the Bellman equations, in exactly the notation the rest of this curriculum uses.
- Discounting done carefully: Sutton & Barto §3.3–3.4 (returns, episodic vs. continuing, the unified notation) — clears up the vs subtlety flagged in §5.
- Optional depth (contraction/convergence proofs): Bertsekas, Dynamic Programming and Optimal Control, Vol. I — for the Bellman operator as a -contraction and why value iteration converges.
- Bridge to LLMs (read after lesson 02): Ziegler et al., “Fine-Tuning Language Models from Human Preferences” — the paper that first cast LLM generation as the MDP in §10.
12. Practice problems
Practice problem 1: Terminal reward and effective credit
Problem: An LLM generates a sequence of tokens. The only nonzero reward is at the final step; all earlier rewards are 0. (a) Write for an arbitrary step as a function of . (b) With , compute the return credited to the first token vs. the last-generated token. (c) What does this imply, and why do many LLM-RL setups set ?
Worked solution:
(a) With reward only at the end, .
(b) First token: , . Last token (, or the step producing the final action): . So the discounting silently gives the first token less credit than the last — even though early tokens (e.g. setting up a correct chain of thought) may matter more.
(c) Discounting a sparse terminal reward introduces a positional bias unrelated to actual causal contribution, and penalizes longer correct answers. Setting (finite horizon) gives every token equal claim to the terminal reward, letting the advantage estimator — not an arbitrary — do the credit assignment. This is why GRPO5 and many PPO-for-LLM recipes use (and often close to 1 in GAE9).
Practice problem 2: Advantage is a re-centering, not new information
Problem: Show that for any state , . Then explain in one sentence why this makes a variance-reducing baseline in the policy gradient but leaves the gradient unbiased.
Worked solution:
Because the advantage sums to zero under , subtracting the baseline from in the policy gradient contributes in expectation — so the gradient’s mean is unchanged (unbiased) while its variance drops, since we no longer scale by the large, high-variance raw return. (Full derivation in lesson 02.)
13. What’s next
You now have the object every RL algorithm optimizes and the LLM mapping that makes it relevant. The next thread:
- 02-policy-gradients — how to actually climb when you can only sample trajectories: the log-derivative trick, REINFORCE, baselines, and why the advantage function from §8 is the right thing to weight by. This is where the LLM seam from §10 becomes an algorithm.
Further out in the curriculum: actor-critic and GAE (estimating well) → PPO (controlled policy steps) → GRPO (group baselines, no critic) → the full RLHF/RLVR pipeline.
Before moving on, self-check
Can you (1) state from memory, (2) derive the Bellman expectation equation for , and (3) fill in the MDP tuple for an LLM without looking? If any of those is shaky, re-read the relevant section — everything downstream compounds on these three.
References
Part of rl-for-llms | Lesson 01 | Filed: 2026-09-02
Footnotes
-
Sutton, R. S., & Barto, A. G. (2018). Reinforcement Learning: An Introduction (2nd ed.). MIT Press. Finite MDPs, returns, value functions, and the Bellman expectation equations: Chapter 3. Existence of a deterministic optimal policy, the Bellman optimality equations, the policy improvement theorem, and convergence of policy iteration: Chapter 4 (Dynamic Programming). Free PDF: http://incompleteideas.net/book/the-book-2nd.html ↩ ↩2 ↩3 ↩4
-
Lillicrap, T. P., Hunt, J. J., Pritzel, A., Heess, N., Erez, T., Tassa, Y., Silver, D., & Wierstra, D. (2015). “Continuous Control with Deep Reinforcement Learning” (DDPG). arXiv:1509.02971. https://arxiv.org/abs/1509.02971 ↩
-
Fujimoto, S., van Hoof, H., & Meger, D. (2018). “Addressing Function Approximation Error in Actor-Critic Methods” (TD3). arXiv:1802.09477. https://arxiv.org/abs/1802.09477 ↩
-
Schulman, J., Wolski, F., Dhariwal, P., Radford, A., & Klimov, O. (2017). “Proximal Policy Optimization Algorithms.” arXiv:1707.06347. https://arxiv.org/abs/1707.06347 ↩
-
Shao, Z., Wang, P., Zhu, Q., Xu, R., Song, J., Bi, X., Zhang, H., Zhang, M., Li, Y. K., Wu, Y., & Guo, D. (2024). “DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models.” arXiv:2402.03300. Introduces Group Relative Policy Optimization (GRPO), which replaces the learned critic with a group-relative (per-prompt) baseline. https://arxiv.org/abs/2402.03300 ↩ ↩2
-
Bellman, R. (1957). Dynamic Programming. Princeton University Press. Original formulation of the Bellman equation / principle of optimality for dynamic programming. ↩
-
Bertsekas, D. P. (2017). Dynamic Programming and Optimal Control, Vol. I (4th ed.). Athena Scientific. Treats the Bellman operator as a -contraction in the sup-norm and the convergence of value iteration to the unique fixed point. ↩
-
Ziegler, D. M., Stiennon, N., Wu, J., Brown, T. B., Radford, A., Amodei, D., Christiano, P., & Irving, G. (2019). “Fine-Tuning Language Models from Human Preferences.” arXiv:1909.08593. Early formulation of autoregressive LLM generation as an RL/MDP problem with a reward model. https://arxiv.org/abs/1909.08593 ↩
-
Schulman, J., Moritz, P., Levine, S., Jordan, M., & Abbeel, P. (2015). “High-Dimensional Continuous Control Using Generalized Advantage Estimation” (GAE). arXiv:1506.02438. Introduces the bias–variance knob for advantage estimation. https://arxiv.org/abs/1506.02438 ↩