Mathematical Foundations¶
This chapter derives the objects that an Agentic Reinforcement Learning (Agentic RL) implementation manipulates. The goal is to make every tensor in a training batch traceable to a probability, estimator, or explicit approximation.
1. Notation and the two nested policies¶
A Large Language Model (LLM) agent has a semantic policy over environment actions and an autoregressive policy over tokens.
- \(s_t\): latent environment state at decision step \(t\)
- \(o_t\): observation emitted from \(s_t\)
- \(h_t\): policy-visible information state built from observations, past actions, and memory
- \(a_t\): semantic action at decision step \(t\)
- \(x_{t,j}\): generated token \(j\) within action \(a_t\)
- \(L_t\): number of policy-generated tokens in \(a_t\)
- \(r_t\): reward associated with a transition or action
- \(T\): number of environment decisions before termination
- \(\theta\): trainable policy parameters
If the environment consumes a generated token sequence directly, then
and
If a deterministic parser \(g\) converts text to a structured action, \(a_t=g(x_{t,1:L_t})\), several token sequences can map to the same action. The environment-level probability is then
Production trainers almost never compute this sum. They optimize the sampled token sequence. This is a valid likelihood-ratio estimator for the behavior actually sampled, but it means formatting tokens can receive credit even when the environment treats several formats as equivalent.
2. From next-token likelihood to a policy¶
Pretraining models a token sequence \(x_{1:n}\) as
Maximum likelihood minimizes
During agent execution, the same conditional distribution becomes a stochastic policy. The difference is causal data generation:
- in teacher forcing, the next prefix comes from a fixed dataset;
- in online RL, sampled tokens determine the semantic action, which changes the next observation and therefore the future training distribution.
Cross-entropy and policy gradient can operate on the same logits, but they estimate gradients of different objectives under different data distributions.
3. MDPs, POMDPs, histories, and belief states¶
An MDP satisfies
An agent rarely observes \(s_t\) completely. In a POMDP it sees \(o_t\sim O(\cdot\mid s_t)\). A theoretically sufficient information state is the belief distribution
The Bayesian update is proportional to
LLM agents generally do not maintain this distribution explicitly. They use a serialized history, summary, retrieval result, recurrent state, or learned memory \(h_t=f(o_{0:t},a_{0:t-1})\). The policy is therefore \(\pi_\theta(a_t\mid h_t)\), and the quality of \(f\) is part of the agent.
This matters experimentally. If training supplies hidden state or evaluator annotations that inference cannot observe, the policy is trained on a different information structure from the deployed agent.
4. Return, value, action value, and advantage¶
For discount \(\gamma\in[0,1]\), define reward-to-go
Under policy \(\pi\),
and
Using history \(h_t\) rather than latent state \(s_t\) is deliberate: the critic must be clear about whether it is allowed privileged environment state. A centralized training critic may use extra state, but that changes the estimator and must not leak into the actor at inference.
For an MDP, the Bellman expectation equations are
These equations explain why a useful critic can propagate sparse terminal success backward. They do not guarantee that a learned critic will generalize under a changing language policy.
5. Trajectory probability¶
For a fully observed MDP with initial-state distribution \(\rho_0\),
Only the policy term depends on \(\theta\) when the environment dynamics are fixed with respect to the policy parameters. Therefore
This is the crucial reason model-free policy gradients do not need a differentiable browser, compiler, simulator, user, or robot.
6. Deriving REINFORCE¶
Let \(R(\tau)\) denote total return and
Differentiate:
Rewards before action \(a_t\) cannot be caused by that action. Replacing total return with reward-to-go preserves expectation and reduces variance:
Expanding the semantic action into generated tokens gives
A terminal outcome therefore supplies the same Monte Carlo return to all policy tokens in a turn unless a finer estimator or reward decomposition is added.
7. Why a baseline does not bias the gradient¶
Subtract any baseline \(b(h_t)\) that does not depend on the sampled action:
Since \(\pi\nabla\log\pi=\nabla\pi\),
Thus
has the same expectation and can have much lower variance. A learned critic approximates \(V^\pi(h_t)\). A leave-one-out or group mean uses other samples for the same prompt/task. Baseline validity depends on the estimator details: including the current action's reward in a sample mean, normalizing by a sample-dependent standard deviation, or coupling trajectories can introduce finite-sample bias even when the intuitive “center the rewards” story sounds correct.
8. Actor–critic and temporal-difference residuals¶
For a learned value \(V_\phi\), the one-step TD residual is
If \(V_\phi=V^\pi\), \(\delta_t\) is an unbiased sample of the one-step advantage conditioned on \((h_t,a_t)\). In practice the critic is approximate and trained on a non-stationary distribution.
Generalized Advantage Estimation is
- \(\lambda=0\): one-step TD, lower variance and more bootstrapping bias.
- \(\lambda=1\): Monte Carlo return minus value, less bootstrapping bias and higher variance for finite episodes.
Timeout handling is mathematical, not bookkeeping. A true terminal state has
no future value. A rollout truncated only because a collector reached a length
budget may require bootstrapping from \(V(h_T)\). Marking both as done=True
changes targets.
9. Importance sampling and policy versions¶
Suppose data came from behavior policy \(\mu\) but the target is \(\pi_\theta\). For a single action,
when support is sufficient. The ratio is
For a token sequence, a trajectory/action ratio is a product of token ratios:
Equivalently, log-ratios add. Products over long sequences can have enormous variance. Practical LLM algorithms therefore use token-level clipping, sequence-level clipping, truncated ratios, small policy lag, or objectives that accept some bias.
An old-policy log-probability stored with each generated token is meaningful only if it was computed under the exact token prefix, chat template, model weights, sampling mask, and vocabulary used for generation.
10. PPO's clipped surrogate¶
Let behavior policy be \(\pi_{\theta_{\text{old}}}\) and define
PPO maximizes
The sign of \(\hat A_t\) matters:
- for positive advantage, an increase above \(1+\epsilon\) stops improving the surrogate;
- for negative advantage, a decrease below \(1-\epsilon\) stops improving it.
Clipping is not a hard guarantee on KL divergence. A trainer should still log approximate KL, clip fraction, ratio distribution, entropy, and gradient norms.
LLM PPO often adds a value loss and entropy term:
with sign convention depending on whether code minimizes loss or maximizes an objective. A single sign error can make the “entropy bonus” reduce entropy.
11. KL regularization and the reference policy¶
To preserve capabilities and limit reward overoptimization, optimize
The sampled log-ratio
is an unbiased Monte Carlo estimate of forward \(D_{\mathrm{KL}}(\pi_\theta\Vert\pi_{\text{ref}})\) when \(y\) is sampled from \(\pi_\theta\). If samples come from an older behavior policy, this interpretation needs correction.
For tokens,
Applying a per-token KL penalty makes longer responses accumulate more penalty. Dividing by length changes the optimized objective. Neither choice is neutral; state the normalization explicitly.
The KL-regularized optimum for a fixed context and reward has form
which is the relationship used to derive DPO-style preference objectives. DPO is valuable background, but a fixed pairwise dataset does not directly solve multi-turn on-policy credit assignment.
12. Group and leave-one-out baselines¶
For a task \(q\), sample \(G\) trajectories with returns \(R_1,\ldots,R_G\). A leave-one-out baseline is
Because \(b_i^{\text{LOO}}\) excludes action/trajectory \(i\), it is independent of that sample conditional on the task when rollouts are independent. RLOO uses this structure to avoid a learned critic.
A commonly shown GRPO normalization is
This makes updates invariant to affine scaling within a group and emphasizes relative performance. It also creates important edge cases:
- if all rewards are equal, the group supplies no learning signal;
- small groups produce noisy mean and standard deviation estimates;
- prompt difficulty affects reward variance and therefore gradient scale;
- standard-deviation normalization can overweight low-variance groups;
- including \(R_i\) in \(\overline R\) couples the sample with its baseline;
- outcome advantage copied across all tokens interacts with response-length normalization.
These are estimator properties, not minor implementation details. DeepSeekMath introduced GRPO (Shao et al., 2024); Dr. GRPO analyzes several normalization biases (Liu et al., 2025).
13. Loss normalization and length bias¶
Suppose token loss for response \(i\) is
where \(m\) marks policy-generated trainable tokens and \(s\) is a surrogate term. At least three batch reductions are common:
Per-token normalization¶
Every token has equal weight; long trajectories influence more terms.
Per-sequence normalization¶
Every sequence has equal total weight; each token in a long response receives less weight.
Per-turn or per-trajectory normalization¶
Multi-turn systems can average within turns, then trajectories, then tasks. This changes the relative influence of long episodes, verbose actions, and tasks with many decisions.
The denominator is part of the objective. When distributed workers have different token counts, averaging already-normalized local losses is generally not equivalent to globally summing numerators and denominators.
14. Multi-turn credit assignment¶
For a terminal-only task reward \(R_T\), Monte Carlo credit uses
for every turn. It is unbiased with a valid baseline but high variance and does not distinguish an essential early tool call from irrelevant later text.
Possible refinements include:
Turn-level environment rewards¶
Use verified progress \(r_t\) and reward-to-go. Dense rewards reduce temporal distance but can alter the optimum. Potential-based shaping
preserves optimal policies under standard MDP conditions, whereas arbitrary “looks like progress” rewards do not.
Learned values¶
Estimate \(V(h_t)\) or \(Q(h_t,a_t)\). This can use all downstream reward but is susceptible to approximation error and distribution shift.
Process rewards¶
Score turns, subgoals, or reasoning spans with rules, humans, or a learned model. The supervision becomes denser, but the policy can optimize evaluator artifacts.
Counterfactual and branching estimates¶
From a saved state before action \(a_t\), sample alternate continuations to estimate its marginal effect. This is closer to causal credit but multiplies environment and rollout cost and requires reproducible state cloning.
Hierarchical credit¶
Treat a plan/subgoal as an option with its own termination and return. A manager chooses subgoals; a worker executes primitive actions. The hierarchy can shorten credit paths, provided the option boundaries are meaningful and trainable.
15. Entropy and exploration¶
For a categorical token distribution,
Entropy is not identical to useful behavioral diversity:
- high token entropy may vary punctuation without changing the semantic action;
- low token entropy may still support diverse long-horizon plans through early branch choices;
- sampling temperature alters rollout distribution but not by itself the objective's entropy regularization;
- tool schemas and constrained decoding reduce syntactic entropy while possibly improving semantic exploration.
Log entropy per token, semantic action diversity, unique successful strategies, and group reward variance separately.
16. Multi-objective and constrained RL¶
Agent utility normally includes success, safety, cost, latency, and user preference. A scalar reward might be
The weights encode policy decisions and can hide unacceptable tradeoffs. For a hard expected constraint
form a Lagrangian
Alternating policy ascent and multiplier ascent/descent (depending on sign convention) can enforce an expectation constraint, but it does not guarantee zero catastrophic violations. Authorization checks, sandboxing, and action filters remain necessary outside the learned policy.
Lexicographic objectives—first satisfy safety, then maximize task utility—can be more appropriate than allowing enough task reward to compensate for a severe violation.
17. Estimator diagnostics¶
Track quantities that reveal the estimator, not just final reward:
- mean, standard deviation, and histogram of raw reward components;
- return and advantage by task, turn, length, and success/failure;
- group reward variance and fraction of all-equal groups;
- policy/behavior token log-ratio distribution;
- approximate KL to old and reference policies;
- clip fraction for positive and negative advantages;
- entropy by position and semantic decision;
- value loss, explained variance, and calibration by horizon;
- gradient norm before/after clipping and by parameter group;
- effective sample size for importance weights, \((\sum_i w_i)^2/\sum_iw_i^2\);
- response/episode length and correlation with reward/advantage;
- policy lag measured in optimizer steps and divergence, not wall time alone.
A rising reward with collapsing group variance may mean the curriculum became too easy. A stable loss with exploding ratios may mean clipping hides unusable off-policy data. A successful average with a heavy tail of safety violations is not a successful policy.
18. Tensor contract for an implementation¶
A minimal padded batch can use shapes
| Tensor | Shape | Meaning |
|---|---|---|
input_ids |
[B, L] |
prompt, observations, and sampled policy tokens |
attention_mask |
[B, L] |
valid non-padding positions |
action_mask |
[B, L-1] |
next-token positions generated by the policy |
old_logprobs |
[B, L-1] |
behavior log-probability of sampled next token |
ref_logprobs |
[B, L-1] |
reference-policy log-probability, if used |
turn_ids |
[B, L-1] |
environment decision associated with each token |
rewards |
[B, T] or events |
reward components at decision boundaries |
advantages |
[B, T] or [B, L-1] |
credit estimate aligned to turns/tokens |
policy_version |
[B] or per segment |
checkpoint that sampled the action |
For causal logits logits[:, :-1], the target is input_ids[:, 1:]. The action
mask must align with the target token, not the input position. Tool observations,
system prompts, user messages, padding, and tokens generated by other policies
must have zero policy-gradient mask.
For packed variable-length trajectories, replace padding with segment IDs, cu-seqlens, or block-diagonal attention metadata. The same logical contract must remain testable.
19. Invariants worth testing¶
- A constant reward across all independent actions yields zero expected baseline-centered policy gradient.
- Adding a constant to every reward in an exactly centered group does not change normalized advantages.
- Masked observation tokens contribute exactly zero policy loss and gradient.
- Re-tokenizing decoded rollout text is never used to reconstruct behavior log-probabilities.
- With \(\theta=\theta_{\text{old}}\), every valid importance ratio is one within numerical tolerance.
- Padding or repacking the same examples leaves the globally normalized loss unchanged.
- A true terminal state does not bootstrap; a collector truncation follows the configured bootstrap rule.
- Distributed loss reduction matches a single-process concatenated batch.
- Old log-probabilities are immutable and tagged with the behavior policy.
- Reward, action, and observation events reconstruct the original environment trajectory in order.
20. Common mathematical category errors¶
- Calling a sample baseline a “value function” when it does not condition on state/history.
- Calling clipped PPO exactly on-policy after multiple epochs over the same rollout without acknowledging increasing mismatch.
- Treating a KL estimator from behavior-policy samples as the target policy's exact forward KL.
- Dividing token loss by sequence length and claiming the objective is unchanged.
- Copying terminal reward to every token and calling the resulting signal token-level process supervision.
- Using privileged evaluator state in a critic without documenting centralized training.
- Interpreting a standard-deviation-normalized group advantage as an absolute measure of task utility.
- Assuming lower estimator variance implies lower bias or better final policy.
References¶
- Ronald J. Williams, “Simple Statistical Gradient-Following Algorithms for Connectionist Reinforcement Learning”, 1992.
- Richard S. Sutton and Andrew G. Barto, Reinforcement Learning: An Introduction, 2nd ed., 2018.
- John Schulman et al., “High-Dimensional Continuous Control Using Generalized Advantage Estimation”, 2015.
- John Schulman et al., “Proximal Policy Optimization Algorithms”, 2017.
- Zhihong Shao et al., “DeepSeekMath”, 2024.
- Zichen Liu et al., “Understanding R1-Zero-Like Training: A Critical Perspective”, 2025.