Skip to content

Guides

Tutorial: evaluate a reinforcement-learning policy

A runnable walkthrough for two kinds of reinforcement-learning question: how well a policy you can run performs over episodes, and how well a policy you cannot try in the world would have done, judged from the log of the policy it would replace, one decision at a time or several per episode. Every example runs locally with no provider credentials.

The reference for every field and evaluator is Reinforcement learning; the terms case, evaluator, metric, interval and gate are in Core concepts. This page is the hands-on route through them.

What Oloproof does and does not do here

Oloproof does not run your environment, host a simulator or train a policy. Your system runs each episode in its own environment and records it as an rl_episode/v1 artifact, and every episode metric is read out of that record. In the second and third parts nothing runs at all: the policy under test only states the probability it would give each action.

Keep the two questions apart:

QuestionWhat you havePart
How well does this policy do over episodes?A policy and an environment you can run1
How well would this policy have done in production?A log of decisions an older policy made, with the probabilities it used2
The same, when each choice changes what comes nextA log of whole episodes, with the probabilities used at every step3

Prerequisites

  • Python 3.11 or later, and Oloproof installed (pip install oloproof).
  • The example project, which ships with the package. Copy it into a new directory:
oloproof init --example rl my-rl

It holds three projects, episodes/, off_policy/ and sequential/. Every command below runs from inside one of them, and evidence is stored in its .oloproof/ directory.

Part 1: a policy you can run

cd my-rl/episodes

The files

FileWhat it is
env.pyA grid world, two policies (wander and plan) and the systems that run them
data/tasks.jsonl24 grids, each a task: walls, hazards, a start and a goal
oloproof.yamlThe suite for the baseline policy, wander
candidate.yamlThe same suite for the candidate policy, plan
release.yamlThe release policy
comparison.yamlThe rules a candidate must meet against the baseline

An episode moves one cell per step until it reaches the goal or runs out of steps. Every step earns -1 and reaching the goal earns +10, so a return lies between -30 (30 steps, no goal) and 10. Entering a hazard costs 1: that is the safety constraint. One move in ten slips in a random direction, whatever the policy chose, so the same policy can do differently on the same grid.

Recording an episode

The system runs the episode itself and hands the record to the case recorder:

@system(name="grid-agent", version="wander-v1", records=("rl_episode/v1",))
def run_baseline(task):
    return _run(task, wander)


def _run(task, policy):
    recorder = current_case()
    record = episode(task, policy, seed=task["seed"] * 1000 + recorder.replicate)
    recorder.rl_episode(record)
    return {"success": record["success"], "steps": len(record["steps"])}

The record lists each step's reward and cost, whether the episode reached the goal (success), and whether the step limit ended it (truncated).

Several episodes per task

oloproof.yaml declares replicates: 5, so every task runs five episodes. current_case().replicate says which one (0 to 4), and the environment seeds the episode from it: the same episode of the same task always replays the same way, and a second run reuses the stored episodes instead of running them again.

The task stays the unit. Each criterion is the mean, over the 24 tasks, of each task's own mean over its five episodes, so the interval counts 24 tasks and not 120 episodes. Counting episodes would pretend five episodes of one grid are five independent grids.

The evaluators:

evaluators:
  - {type: episode_return, return_range: [-30, 10]}
  - {type: episode_success}
  - {type: episode_cost_within, budget: 0, criterion: hazard_free}
metrics:
  - {id: solved_every_time, type: every_replicate, criterion: episode_success}

return_range is required: the interval on a mean return holds only within a range every return lies in, and a return outside it stops the run. solved_every_time counts a task only when all five of its episodes reached the goal.

Run the baseline

oloproof run
Gate: BLOCK (exit 3)
                                        Decisions
┏━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Rule          ┃ Metric          ┃ State                 ┃ Reasons                     ┃
┡━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ success-floor │ episode_success │ INSUFFICIENT_EVIDENCE │ interval_overlaps_threshold │
│ safety-floor  │ hazard_free     │ INSUFFICIENT_EVIDENCE │ interval_overlaps_threshold │
└───────────────┴─────────────────┴───────────────────────┴─────────────────────────────┘
                                                           Metrics
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Metric            ┃ Estimate ┃ Interval        ┃ N                                                                         ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ episode_return    │ -10.51   │ [-16.44, -5.11] │ mean of 24 observed · 0 missing · 0 excluded                              │
│ episode_success   │ 76.7%    │ [55.4%, 89.6%]  │ 24 cases observed · 0 missing · 0 excluded · 5 replicates each · 14 flaky │
│ hazard_free       │ 29.2%    │ [10.3%, 54.0%]  │ 24 cases observed · 0 missing · 0 excluded · 5 replicates each · 9 flaky  │
│ solved_every_time │ 37.5%    │ [18.7%, 59.5%]  │ 9 / 24 observed · 0 missing · 0 excluded · 5 replicates each · 14 flaky   │
└───────────────────┴──────────┴─────────────────┴───────────────────────────────────────────────────────────────────────────┘
Cache: execution 0 hit/120 miss; judgment 0 hit/360 miss

Read it row by row:

  • The wandering policy reaches the goal on 76.7% of episodes, but the interval runs from 55.4% to 89.6%, so the floor of 75% is neither cleared nor missed: INSUFFICIENT_EVIDENCE.
  • It solves only 9 of the 24 tasks on every one of its five episodes. "Usually works" and "works every time" are different claims, and an agent that will run unattended needs the second.
  • 14 tasks are flaky: their success moved between identical episodes. One episode per task could not have shown that.
  • It avoids hazards on 29.2% of episodes. The interval's top, 54.0%, is above the 50% floor, so even this poor record does not yet prove the policy unsafe.

Look at one task:

oloproof inspect latest --case grid_03
output: {
  "success": false,
  "steps": 30
}
judgments:
  episode_return: score -30
  episode_success: failed
  hazard_free: passed

The first episode of grid_03 ran out of steps: return -30, no goal, no hazard entered.

Run the candidate

oloproof run --config candidate.yaml
│ success-floor │ episode_success │ PASS                  │ lower_bound_meets_minimum   │
│ safety-floor  │ hazard_free     │ INSUFFICIENT_EVIDENCE │ interval_overlaps_threshold │
│ episode_return    │ 0.42     │ [-5.78, 2.59]   │ mean of 24 observed · 0 missing · 0 excluded                             │
│ episode_success   │ 100.0%   │ [79.9%, 100.0%] │ 24 cases observed · 0 missing · 0 excluded · 5 replicates each           │
│ hazard_free       │ 57.5%    │ [29.3%, 81.5%]  │ 24 cases observed · 0 missing · 0 excluded · 5 replicates each · 6 flaky │
│ solved_every_time │ 100.0%   │ [85.7%, 100.0%] │ 24 / 24 observed · 0 missing · 0 excluded · 5 replicates each            │

The planner reaches the goal on every episode, so the success floor passes. It still enters hazards: some grids have no safe path, and the slippery floor pushes it into others. Its safety rate of 57.5% is above the floor, but 24 tasks cannot yet show it is above 50%.

Compare the two policies

oloproof compare latest latest~1 --policy comparison.yaml
episode_return: +10.92 [+0.21, +17.74] · 24 paired · 0 missing · 0 excluded
episode_success: +23.3 points [-3.2, +44.6] · 24 paired · 0 missing · 0 excluded
hazard_free: +28.3 points [-2.4, +54.7] · 24 paired · 0 missing · 0 excluded
solved_every_time: +62.5 points [+24.3, +87.7] · 24 paired · 0 missing · 0 excluded
Decisions
  return-better  episode_return  superiority  PASS  difference_above_zero
  safety-no-worse  hazard_free  non-inferiority, margin 5.0 points  PASS  lower_bound_above_margin
Gate: ALLOW (exit 0)

The comparison pairs each task with itself, each side worth its mean over five episodes. The return difference is in the return's own unit: the planner earns about 10.9 more per episode, and at least 0.21 more. Paired on the same grids, a 5-point margin is enough to show the planner is no less safe, even though neither run alone could settle its own safety floor.

Part 2: a policy judged from a log

cd ../off_policy

A support desk routes each ticket to a small or a large model. The current router picks the small model 70% of the time and the large one 30%, at random. You have written a new rule: technical tickets, and long billing tickets, go to the large model. You cannot try it on real tickets yet, but the old router's log can answer for it. That is off-policy evaluation.

The log and its files

FileWhat it is
data/decisions.jsonl600 decisions the current router made
make_log.pyThe simulation that wrote that log, so you can see how it was made
router.pyThe new rule, the system under test
oloproof.yamlThe suite
release.yamlThe release policy

Each line of the log is one decision:

{"id":"t001","input":{"topic":"technical","length":"short"},"expected":{"action":"small","logging_probabilities":{"small":0.7,"large":0.3},"reward":1}}

input is what the router saw, action what it chose, logging_probabilities the probability it gave every model at that moment, and reward whether the ticket was resolved. The new rule only answers with its own probabilities:

@system(name="ticket-router", version="by-topic-v1")
def route_by_topic(ticket):
    large = ticket["topic"] == "technical" or (
        ticket["topic"] == "billing" and ticket["length"] == "long"
    )
    return {"probabilities": {"small": 0.0 if large else 1.0, "large": 1.0 if large else 0.0}}

The evaluators:

evaluators:
  - {type: off_policy_value, reward_range: [0, 1], propensity_floor: 0.3}
  - {type: off_policy_improvement, reward_range: [0, 1], propensity_floor: 0.3}

Each logged decision is reweighted by how much more, or less, likely the new rule was to make it than the old router: a ticket the old router sent to the large model with probability 0.3, which the new rule would always send there, counts 1 / 0.3 times, and a decision the new rule would never have made counts zero. propensity_floor is the smallest probability the old router gave any model it could pick. It bounds that weight, and without a bound no interval holds.

Run it

oloproof run
│ resolution-floor    │ off_policy_value       │ PASS                  │ lower_bound_meets_minimum                                                          │
│ better-than-current │ off_policy_improvement │ INSUFFICIENT_EVIDENCE │ interval_overlaps_threshold                                                        │
│                     │                        │                       │ a suite of about 3092 cases (2492 more) would decide it, if the next cases go like these; at that size it is settled about    │
│                     │                        │                       │ half the time                                                                                                                 │
│ off_policy_value       │ 0.775    │ [0.632, 0.921]  │ mean of 600 observed · 0 missing · 0 excluded │
│ off_policy_improvement │ 0.060    │ [-0.064, 0.188] │ mean of 600 observed · 0 missing · 0 excluded │

The new rule would resolve an estimated 77.5% of tickets and, at 95% confidence, at least 63.2%: the floor of 60% passes. Whether it beats the current router is not settled. The estimate is 6 points better, but the interval runs from 6.4 points worse to 18.8 better, and the engine says roughly how many more logged decisions would decide it.

That width is the price of learning from someone else's choices. Only the 30% of tickets the old router happened to send to the large model say anything about the large model, and each counts more than three times. Two things narrow it: a longer log, and an old router that explores more.

Logging decisions from your own system

  • Record the probabilities when the router decides, not afterwards, and record every option's probability, not only the chosen one's. Oloproof does not estimate them: a guessed probability would make the interval rest on the guess.
  • Put in input only what the router saw when it decided, never the chosen option or the outcome: the policy under test must not see them.
  • If your new policy samples its choice, return the probabilities it samples from, not the one option it drew. Name each option exactly as the log does: an option spelled differently counts as one the old router never chose.
  • Keep some randomness in production routing. A policy that never chooses an option leaves nothing in the log to judge a rule that would.
  • The answer describes tickets like the logged ones, over the period the log covers. A change in your traffic is outside it.
  • A malformed line (probabilities that do not add up to 1, a chosen option with probability 0, a reward outside reward_range) stops the run. A decision the new rule would make with an option the old router gave less than the floor, or whose reward is missing, counts as missing and widens the interval.

Part 3: several decisions per episode

cd ../sequential
python make_log.py

The desk now handles each ticket in up to two steps. First it picks the model that drafts the reply, small or large, 60% and 40% of the time. If the draft resolves the ticket, the episode ends with a reward of 1. If not, the desk picks again: escalate to a person, who almost always resolves it but costs much more (a reward of 0.3), or let the other model retry (1 if it resolves the ticket). It escalates or retries half the time each.

The first choice changes the second: a ticket the large model resolved never reaches step 2. Scoring each step as its own decision, as in Part 2, would answer a different question. These evaluators read the whole episode.

The episode log and its files

FileWhat it is
make_log.pyThe simulation that writes the log, data/episodes.jsonl: 10,000 episodes, the same every time
desk.pyThe new two-step rule, the system under test
oloproof.yamlThe suite
release.yamlThe release policy

The log is several megabytes, so the example ships the script that writes it rather than the log. Each line is one episode:

{"id":"e0001","input":{"steps":[{"observation":{"topic":"technical","length":"long"},"action":"small","reward":0},{"observation":{"topic":"technical","length":"long","drafted":"small"},"action":"escalate","reward":0.3}]},"expected":{"logging_probabilities":[{"small":0.6,"large":0.4},{"escalate":0.5,"retry":0.5}]}}

input.steps holds every step in order (what the desk saw, what it chose, the reward that followed) and expected.logging_probabilities the desk's probability of every option at each step. The new rule sends technical and long tickets to the large model, and when the draft fails, escalates technical tickets and retries the rest:

@system(name="support-desk", version="draft-then-escalate-v1", per_step=True)
def desk(step):
    ticket = step["observation"]
    if not step["history"]:
        large = ticket["topic"] == "technical" or ticket["length"] == "long"
        return {"probabilities": {"small": 0.0 if large else 1.0, "large": 1.0 if large else 0.0}}
    escalate = ticket["topic"] == "technical"
    return {
        "probabilities": {"escalate": 1.0 if escalate else 0.0, "retry": 0.0 if escalate else 1.0}
    }

per_step=True (per_step: true in oloproof.yaml) makes Oloproof call it once per step, with that step's observation and the history of the steps before it. It never sees the step's own choice, its reward or anything after it, which is what keeps the evaluation honest. The evaluators:

evaluators:
  - type: sequential_off_policy_value
    reward_range: [0, 1]
    propensity_floor: 0.4
    horizon: 2
  - type: sequential_off_policy_improvement
    reward_range: [0, 1]
    propensity_floor: 0.4
    horizon: 2

horizon is the most steps an episode has. The answer is the new rule's reward over those first steps, and a logged episode longer than it stops the run. The floor is the smallest probability the desk gave any option, at any step: 0.4.

Run the evaluation

oloproof run
│ reward-floor        │ sequential_off_policy_value       │ PASS  │ lower_bound_meets_minimum │
│ better-than-current │ sequential_off_policy_improvement │ PASS  │ lower_bound_meets_minimum │
│ sequential_off_policy_value       │ 0.938    │ [0.890, 0.977] │ mean of 10000 observed · 0 missing · 0 excluded │
│ sequential_off_policy_improvement │ 0.08     │ [0.04, 0.12]   │ mean of 10000 observed · 0 missing · 0 excluded │
Gate: ALLOW (exit 0)

The new rule would earn an estimated 0.938 per ticket, and at least 0.890 at 95% confidence, which clears the floor of 0.75. It beats the current desk by an estimated 0.08, at least 0.04. The simulation's hidden truth, which the run never read, is 0.942 and 0.092: both inside their intervals.

It took 10,000 tickets. A reward at step 2 is reweighted by both steps' choices, so one episode can count up to 1 / 0.4 × 1 / 0.5 = 5 times, against 2.5 for one step. That weight multiplies with every step: at a floor of 0.2, five steps can weigh 3,125 times, and an interval 1 wide would need some 21,000 episodes. Reinforcement learning has the table to check a log against before you collect it.

Logging episodes from your own system

  • Everything in Part 2's list holds at every step: record every option's probability when the policy decides, and put in each observation only what it saw then.
  • Set horizon to the most steps your episodes can take, and keep it short. Each step multiplies the weights, so two or three steps answer where ten rarely do.
  • If the new rule would take an option the desk gave less than the floor at some step, the steps before it still count, and the rest of the episode is bounded by what it could earn. The case shows the episode by its bounds and the interval widens; the run does not stop.
  • These runs take one episode per case (replicates: 1) and are not compared with oloproof compare: the improvement already compares the new rule with the logged one, on the same episodes.

What is not built

  • Off-policy evaluation of long episodes under a cautious logging policy: past a few steps the interval is too wide to decide, and no model of the environment narrows it.
  • A log whose router changed its probabilities as it learned during the window.
  • Hosting or resetting environments, simulators or robots.

Where to go next