Token LabSearch

← RL knowledge base
RL / 7 OF 8

Agent RL: train a policy inside a harness

Make the environment, tools and success criterion reproducible.

Study guide & lesson outlineLEARNING CONTRACT

Understand → implement → investigate

Goal: explain the mechanism in this lesson, reproduce one example and test a failure case. Reading alone does not mark mastery.

  • Understand: state assumptions, shapes/units and a counterexample.
  • Implement: reproduce a numerical example and inspect boundaries.
  • Investigate: compare one alternative under a matched budget.

Understand the mechanism

An agent harness controls observations, tools, budgets, retries and termination. Changing any of these can change the policy’s effective environment. Separate model training from tool implementation and evaluation orchestration. For delayed task rewards, retain trajectories and action outcomes; a final success bit alone rarely explains a failure.

Text / 原始公式
Environment contract = observations + actions + transitions + rewards + termination
Evaluate: success, cost, latency, constraint violations

Mechanism map & worked derivation

Observe task and evidence → Choose permitted next action → Execute within budget → Validate observation or stop
Original schematic; read the steps from top to bottom. SVG ↓
Symbols and assumptions
SymbolMeaning
sₜObserved task context, not necessarily full environment state
aₜSelected tool action with explicit arguments
oₜ₊₁Returned observation, including errors
  1. Write a transition contract for every tool: input schema, output schema, timeout behavior, side effects and idempotency. This makes the loop testable without a language model.
  2. A two-call task cannot complete with a one-call hard budget. After lookup, the system should record budget_exhausted rather than inventing verification evidence.
  3. If a write times out, its commit state is unknown. The next action should check state or use an idempotent retry, not assume nothing happened.
Experiment checkpoint

Separate mock control-flow tests from real-policy evaluation. A readable trace is not proof of external task completion.

Original exercise & analysis

Take the bounded agent simulator, introduce a failing tool and vary only the action budget. Record why each run stops. For later real training, freeze the tool schema, task seeds and environment version; evaluate on a second harness to test interface overfitting.

A local state-machine simulation is not RL training. You need sampled trajectories, a reward signal and parameter updates to claim policy learning.

Why might a trained agent fail after a tool rename?

The policy may depend on interface tokens or schema patterns learned in its training environment. Test semantic generalization with controlled interface changes.

Study evidence & continue

ReAct: tool-interaction foundation (not an RL algorithm) ↗
MATHEMATICAL REVIEW · 2026-10-26

Assumptions, implementation & acceptance

Tool budgets are nonnegative integers. Allowlist checks precede execution; exhausted budgets stop the loop. This deterministic mock tests orchestration, not an LLM’s planning competence.

Formula conditions, units and worked arithmetic reviewed. The implementation isolates one numerical mechanism; it does not train a policy or control real hardware.

Before running: predict a boundary case

The third action is never executed. This tests a budget gate, not a learned agent.

Hint: isolate the assumption

Keep the baseline input fixed, change one parameter or boundary, and compare both the numerical result and the claimed units. A passing example verifies this fixture only.

Minimal executable example

Save the example and linked modules in one folder. Use Node.js 22 or newer, then run node agent-rl.mjs. No third-party runtime dependencies.

const solve=()=>{
const costs=[1,2,4], budget=3;let spent=0,steps=0;
for(const cost of costs){if(spent+cost>budget)break;spent+=cost;steps++}return [spent,steps];
};
const actual=solve();
const expected=[3,2];
if(JSON.stringify(actual)===JSON.stringify(expected)){console.log('PASS',actual)}else{
const a=[actual].flat(), b=[expected].flat();
if(a.length!==b.length||a.some((v,i)=>!Number.isFinite(v)||Math.abs(v-b[i])>1e-10))throw Error('Numerical mismatch');
console.log('PASS within 1e-10',actual);
}
Reference output — reveal after predicting
[
  3,
  2
]

Test and transfer

The third action is never executed. This tests a budget gate, not a learned agent.

Record input, code version, expected result and observed result in the notebook below. Then construct a counterexample that violates the assumption above and explain why the original conclusion no longer follows.

Download complete kit (.tar.gz) · Complete reference kit and regression checks

Predict, test and keep evidence

Before revealing an answer, predict one result. After the exercise, record the discrepancy and a transfer test. Saved locally across language editions.

Open notebook / export / clear →

The mathematical review scope is recorded per lesson. It does not establish empirical paper reproduction; report suspected errors in your notes. Review scope