Skip to content

Reinforcement Learning: An Introduction, Ch. 3 — Finite MDPs, Returns and Episodes

main track #1corefundamentalbookrubric 11/122018 · MIT Press (2nd ed.), Ch. 3 · ~60 min

Richard S. Sutton, Andrew G. Barto

Status

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.

StatesQ: only the question is in context. E: evidence has been retrieved. ⊥: terminal (episode over).
Actions in Qanswer → 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 Eanswer → 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.
The search-or-answer MDP and its LLM and search-agent readings Left: a three-state MDP. From the question state Q, answer ends the episode with reward 1 with probability 0.3; search costs 0.1 and moves to the evidence state E. From E, answer ends with reward 1 with probability 0.8; search costs 0.1 and stays in E. Right: the same parts read as LLM token generation and as a search agent. Toy MDP (γ = 0.9) Q question only E evidence in context ⊥ search, r = −0.1 answer: r = 1 w.p. 0.3 answer: r = 1 w.p. 0.8 search, −0.1 LLM token generation state = prompt + tokens so far action = next token transition = append token reward = RM / verifier at the end Search agent state = question + transcript actions = query / open / answer transition = retriever (random) reward = correct − tool cost π₀: search w.p. 0.5 in Q, always answer in E → expected return 0.46
Original sketch. The search-or-answer MDP used across the Fundamentals pages, and how its parts map onto LLM token generation and a search agent.

The interface. At time tt the agent is in state StS_t, picks action At∼π(⋅∣St)A_t \sim \pi(\cdot \mid S_t), and the environment draws the next state and reward from p(s′,r∣s,a)p(s', r \mid s, a). Sutton and Barto index the reward that follows AtA_t as Rt+1R_{t+1}. 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 tt is the discounted sum of the rewards that follow (S&B Eq. 3.8):

Gt=Rt+1+γRt+2+γ2Rt+3+⋯=∑k=0∞γkRt+k+1,0≤γ≤1.G_t = R_{t+1} + \gamma R_{t+2} + \gamma^2 R_{t+3} + \dots = \sum_{k=0}^{\infty} \gamma^k R_{t+k+1}, \qquad 0 \le \gamma \le 1 .

It obeys a one-line recursion (Eq. 3.9) that the next page turns into the Bellman equation:

Gt=Rt+1+γ Gt+1.G_t = R_{t+1} + \gamma\, G_{t+1}.

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, J(π)=Eπ[G0]J(\pi) = \mathbb{E}_\pi[G_0].

Why discount? Three readings, all useful. (1) Maths: with γ<1\gamma < 1 an infinite sum of bounded rewards stays finite. (2) Preference: a reward now is worth more than the same reward later. (3) Probability: γ\gamma 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 π₀:

TrajectoryProbability under π₀Return G0G_0
Q –answer→ ⊥, correct0.5 × 0.3 = 0.151
Q –answer→ ⊥, wrong0.5 × 0.7 = 0.350
Q –search→ E –answer→ ⊥, correct0.5 × 0.8 = 0.40−0.1 + 0.9 × 1 = 0.8
Q –search→ E –answer→ ⊥, wrong0.5 × 0.2 = 0.10−0.1 + 0.9 × 0 = −0.1

So J(π0)=0.15+0.32−0.01=0.46J(\pi_0) = 0.15 + 0.32 - 0.01 = 0.46. With γ=1\gamma = 1 the search-then-correct return becomes 0.9 and J(π0)=0.5J(\pi_0) = 0.5. 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 −0.1(1+0.9+0.81+… )=−0.1/(1−0.9)=−1-0.1(1 + 0.9 + 0.81 + \dots) = -0.1/(1-0.9) = -1, a finite number only because γ<1\gamma < 1. With γ=1\gamma = 1 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.

LLM token generation (RLHF / RLVR).

MDP partToken-level LLM view
State StS_tprompt xx plus tokens generated so far, y<ty_{<t}
Action AtA_tthe next token yty_t, drawn from the vocabulary
Transitiondeterministic: append the token
Rewardusually 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 endend-of-sequence token (termination) or maximum length (truncation)
Discountusually γ = 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.

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).

  • 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.

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.

  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.

Done with this page?

Status