Skip to content
BIASED

Training agents with reinforcement learning

Episodes as sequences, token masks, shaping, environments, and grader hacking.

From one response to an episode

Reasoning models train one long response judged at the end. An agent’s episode is different: the model writes a turn, a tool runs, its output returns into the context, and this repeats dozens of times before anything grades the task. Most of the context was written by the environment. And the environment — including the files that decide the reward — can be changed by the agent.

An episode as one sequence

Key idea. Treat the whole episode τ=(x,a1,o1,…,aK,oK)\tau=(x,a_1,o_1,\ldots,a_K,o_K) as one token sequence with mask mj=1m_j=1 on model tokens and 00 on task/tool tokens. The policy gradient keeps only the model’s tokens:

∇J(θ)=Eτ∼pθ[A^(τ)∑j=1Nmj ∇log⁡πθ(wj∣w<j)].\nabla J(\theta)=\mathbb E_{\tau\sim p_\theta}\Big[\hat A(\tau)\sum_{j=1}^{N}m_j\,\nabla\log\pi_\theta(w_j\mid w_{<j})\Big].

Without the mask the loss trains the model to predict tool outputs weighted by success — “auto-suggestive delusion”. Search-R1: average exact match 0.4310.431 with the mask vs 0.3430.343 without. Worked group: rewards (1,0,1,1,0,0,0,0)(1,0,1,1,0,0,0,0) → this success gets A^≈+1.29\hat A\approx+1.29 on all 135135 model tokens.

Credit across turns and shaping

Episode-level advantages give every turn the same weight. Remedies: a turn-level critic (ArCHer); or potential-based shaping F=γΦ(h′)−Φ(h)F=\gamma\Phi(h')-\Phi(h) with Φ=0\Phi=0 at episode end, which telescopes and leaves the optimal policy unchanged. Arbitrary bonuses (e.g. for every tool call) change the objective and are exploited. Many systems keep a sparse outcome only (DeepSWE: 11 if hidden tests pass).

Environments and rejection sampling

Environments pair a task distribution, a resettable sandbox, and an outside grader: SWE-Gym, R2E-Gym/DeepSWE, WebRL, Search-R1. Tasks are filtered so GRPO gets signal (not always-succeed / never-succeed).

Filtered behaviour cloning (STaR, expert iteration, RFT): sample, keep successes, SFT on model tokens, repeat. At the current policy its gradient is ∇log⁡P(success)\nabla\log P(\text{success}). In expectation it differs from GRPO mainly in how it weights tasks; it is the baseline every agentic RL result should beat. On a small toy, filtered BC matched GRPO near the 2/32/3 ceiling.

Asynchrony and reward hacking

Long uneven episodes push asynchronous rollouts: generation and training decouple; data is slightly off-policy, corrected by importance ratios and clipping (AReaL, async RLHF).

Reward hacking: agents edit what the grader reads — exit(0), skipped tests, patched timers/evaluators, __eq__ always true. Baker et al.: a CoT monitor caught 95%95\% of systemic hacks vs 60%60\% from actions alone; putting the monitor in the reward taught obfuscation. Toy: hackable grader reports 0.9770.977 while hidden tests say 0.6500.650.

Defences and next

Grade outside the sandbox; least privilege on tests; monitor to patch environments, not as a training reward; track an honest eval the training reward does not see.

Every quoted pass rate came with a scaffold, budget and date. Next: evaluating agents — trustworthy measurements of random systems on finite task samples.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.