Reinforcement Learning: An Introduction, Ch. 3 — Finite MDPs, Returns and Episodes
TL;DR. Reinforcement learning optimises an agent that acts in steps. At each step the agent sees a state, picks an action, and the environment returns a reward and a next state. A Markov decision process (MDP) writes this down precisely. The return is the (possibly discounted) sum of future rewards, and the goal is a policy that maximises the expected return. Everything later on this site (value functions, policy gradients, PPO, GRPO) is a way to estimate or improve that one number.
Why it matters now. RLHF, RLVR and RL-trained search agents all start by choosing an MDP: what counts as a state, an action, a reward and the end of an episode. Those choices decide what the optimiser can see and exploit. For an IR researcher, ranking evaluation already contains the same ideas: a user who keeps reading with some probability is a discount factor, and a metric is an expected return.
The running example: a search-or-answer MDP
Section titled “The running example: a search-or-answer MDP”This page introduces one tiny MDP that every Fundamentals page reuses, so you can compute everything by hand.
| States | Q: only the question is in context. E: evidence has been retrieved. ⊥: terminal (episode over). |
| Actions in Q | answer → go to ⊥, reward 1 with probability 0.3, else 0. search → go to E, reward −0.1 (the cost of a tool call). |
| Actions in E | answer → go to ⊥, reward 1 with probability 0.8, else 0. search → stay in E, reward −0.1 (a second search adds nothing). |
| Discount | γ = 0.9 unless stated. Every episode starts in Q. |
| Starting policy π₀ | In Q: search with probability 0.5, answer with probability 0.5. In E: always answer. |
Key idea
Section titled “Key idea”The interface. At time the agent is in state , picks action , and the environment draws the next state and reward from . Sutton and Barto index the reward that follows as . The Markov property says this distribution depends only on the current state and action, not on the earlier history. If it does depend on history, the history must be put into the state. The 2013 DQN Atari paper does exactly this: one screen does not reveal the full situation, so it defines the state as the sequence of past observations and actions (and in practice feeds the last four frames).
The return. The return from time is the discounted sum of the rewards that follow (S&B Eq. 3.8):
It obeys a one-line recursion (Eq. 3.9) that the next page turns into the Bellman equation:
In words: the return now is the next reward plus the discounted return from the next step. The objective is the expected return from the start state, .
Why discount? Three readings, all useful. (1) Maths: with an infinite sum of bounded rewards stays finite. (2) Preference: a reward now is worth more than the same reward later. (3) Probability: is the chance the episode continues for one more step, which is exactly the patience parameter in rank-biased precision.
Worked by hand (γ = 0.9). There are four possible trajectories under π₀:
| Trajectory | Probability under π₀ | Return |
|---|---|---|
| Q –answer→ ⊥, correct | 0.5 × 0.3 = 0.15 | 1 |
| Q –answer→ ⊥, wrong | 0.5 × 0.7 = 0.35 | 0 |
| Q –search→ E –answer→ ⊥, correct | 0.5 × 0.8 = 0.40 | −0.1 + 0.9 × 1 = 0.8 |
| Q –search→ E –answer→ ⊥, wrong | 0.5 × 0.2 = 0.10 | −0.1 + 0.9 × 0 = −0.1 |
So . With the search-then-correct return becomes 0.9 and . The discount makes the slower route look a little worse.
Episodes, termination and truncation. This task is episodic: it ends in ⊥. A policy that searched in E forever would earn , a finite number only because . With that return diverges, which is why undiscounted LLM tasks always have a maximum length. Hitting that limit is a truncation (we stopped watching), not a termination (the task ended). The GAE page shows why the two must be handled differently.
Translating the toy MDP
Section titled “Translating the toy MDP”LLM token generation (RLHF / RLVR).
| MDP part | Token-level LLM view |
|---|---|
| State | prompt plus tokens generated so far, |
| Action | the next token , drawn from the vocabulary |
| Transition | deterministic: append the token |
| Reward | usually 0 until the end of the completion, then a reward-model score or verifier result (often minus a per-token KL penalty to a reference model) |
| Episode end | end-of-sequence token (termination) or maximum length (truncation) |
| Discount | usually γ = 1 |
One episode is one completion. The return of a completion with only a terminal reward is just that reward. This is why RL papers for LLMs often talk about “sequence-level reward”.
A search agent. State: the question plus the transcript of queries, retrieved snippets and reasoning. Actions: issue a query, open a document, or answer. Transitions: the retriever and corpus decide which documents come back, so transitions are stochastic and not under the policy’s control. Reward: answer correctness, maybe minus a tool cost. The toy MDP is this agent with one possible query and a fixed retrieval quality.
What to read in the book
Section titled “What to read in the book”Sutton & Barto, 2nd edition (free PDF): §3.1 the agent–environment interface, §3.2 goals and rewards, §3.3 returns and episodes, §3.4 unified notation for episodic and continuing tasks. Stop before §3.5; value functions are the next page. If you prefer video, Lecture 2 of David Silver’s course covers the same material (see the course map).
Limitations and open questions
Section titled “Limitations and open questions”- One scalar reward. The framework assumes all goals can be folded into a single number. Helpfulness, faithfulness and cost rarely fold cleanly, which is where reward hacking starts.
- Markov states are expensive for LLM agents. Keeping the whole transcript makes the state Markov but long. Summarising it to save context breaks the Markov property in ways that are hard to measure.
- γ has several meanings at once. It is a preference, a continuation probability and a variance-reduction knob. Papers often change it without saying which meaning they intend.
Connections
Section titled “Connections”- Next: Value functions and Bellman equations computes and for this MDP.
- Course path: Course map · Sutton & Barto · David Silver’s course.
- Why reward choice matters: Specification gaming and the reward-hacking overview.
- Search agents as MDPs: Deep research is a retrieval problem · Long-horizon tool-using agents.
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
In the search-or-answer MDP (search costs −0.1 and moves to the evidence state; answering pays 1 with probability 0.3 from the question state and 0.8 from the evidence state), a policy searches once with probability 0.5 and otherwise answers immediately. Compute the expected return from the start state for γ = 0.9 and for γ = 1. Then compute the return of a trajectory that searches three times before a correct answer, under both discounts.
What a strong answer covers
- Trajectory returns with γ = 0.9: correct direct answer 1, wrong direct answer 0, search then correct −0.1 + 0.9 = 0.8, search then wrong −0.1.
- Expected return with γ = 0.9: 0.5·0.3 + 0.5·(0.8·0.8 + 0.2·(−0.1)) = 0.15 + 0.31 = 0.46. With γ = 1: 0.15 + 0.5·(0.8·0.9 + 0.2·(−0.1)) = 0.15 + 0.35 = 0.5.
- Three searches then a correct answer, γ = 0.9: −0.1 − 0.09 − 0.081 + 0.729 = 0.458. With γ = 1: −0.3 + 1 = 0.7.
- Discounting penalises delay twice: the reward arrives later and is scaled by γ to the power of the number of steps, so extra searches cost more than their explicit −0.1.
- Mechanism
Why is "the prompt plus every token generated so far" a Markov state for an LLM policy, while "the last retrieved document" is not a Markov state for a search agent? What goes wrong in the return and value calculations if the agent's state drops the earlier tool calls?
What a strong answer covers
- Markov means the next-state and reward distribution depends only on the current state and action. The full prefix determines everything the transformer conditions on, and appending a token is deterministic.
- For the agent, the probability of a correct final answer depends on all evidence gathered so far, not only on the last document. Dropping history aliases different situations into one state.
- With aliased states, values average over situations that need different actions, so the learned policy cannot condition on what it already knows (it may re-issue queries or answer too early).
- The DQN paper handles a similar partial-observability problem by treating the whole sequence of observations and actions as the state; LLM agents do the same by keeping the transcript in context, at the price of context length.
- Connect
Rank-biased precision scores a ranked list as (1 − p)·Σ rel_i·p^(i−1). Show that this is a normalised discounted return of a simple MDP, name the state, action, reward and γ, and contrast it with DCG's 1/log2(i+1) weights in terms of the implied user model.
What a strong answer covers
- User MDP: state = rank position, action = continue or stop, reward = relevance of the document at that rank. Continuing with probability p gives the geometric weight p^(i−1), exactly γ = p.
- The (1 − p) factor normalises the sum of weights to 1, so RBP is the expected relevance per document read, an average-reward-style quantity.
- DCG weights decay like 1/log2(i+1), which is not geometric. There is no single constant continuation probability; the implied stop probability changes with rank.
- Takeaway for RL: γ can be read as a probability of continuing, which is the same patience parameter IR metrics already encode.
- Design
You want to RL-train a 7B deep-research agent on a BrowseComp-Plus-style corpus. Specify the MDP: state, actions, transitions, reward, discount and horizon. Say how you would handle episodes that hit the 30-tool-call limit, and why that choice matters for the return.
What a strong answer covers
- State: question plus transcript (queries, retrieved snippets, opened documents, reasoning). Actions: issue a query, open a document, answer. Transitions: the retriever and corpus, stochastic if the index or search API changes.
- Reward: answer correctness (exact match or a validated judge), possibly a small per-call cost. A cost changes the optimal policy, so its size needs justification.
- Discount: γ = 1 with a hard horizon is common for LLM tasks; γ below 1 adds an implicit preference for shorter episodes.
- Hitting the limit is truncation, not a true terminal state. Treat it as reward 0 only if "ran out of budget" really means failure; otherwise bootstrapping from a value estimate is more faithful.
- Validation: check that measured return tracks independently judged answer quality, and that the policy is not simply learning to stop early.
- Critique
The running example gives search a fixed reward of −0.1 to model cost. Identify the weakest assumption in this reward design for a real search agent, describe how a policy could exploit it, and propose an experiment that would expose the problem.
What a strong answer covers
- A constant cost assumes every search is equally expensive and equally useful. In practice cost varies (latency, tokens read) and value depends on the query.
- If the correctness reward is noisy or judge-based, the agent can trade away searches and guess, or learn answers that fool the judge; the cost term then pushes toward under-searching.
- If the cost is too small relative to a dense intermediate reward (for example a reward for retrieving any document), the agent can farm searches.
- Experiment: sweep the cost, then measure the number of searches, judged accuracy and accuracy under a held-out stricter grader. A mismatch between training reward and the held-out grader exposes the proxy.
- Research idea
Propose a study that estimates the "effective discount" a trained search agent is using, without access to its training γ. State the hypothesis, the minimal experiment and the result that would falsify it.
What a strong answer covers
- Hypothesis: the agent behaves as if it discounts future reward with some γ, so its stopping decisions trade expected accuracy gain against delay.
- Experiment: build questions where one more search raises the probability of a correct answer by a controlled amount (oracle corpus manipulation), then record when the agent stops.
- Fit γ (and a per-call cost) to the stopping decisions with a simple choice model, then test the fit on held-out questions.
- Falsified if no single γ and cost explains the choices across conditions, for example if stopping depends on transcript length or token budget rather than expected gain.