Skip to main content

Chapter 20. Randomized Online A/B Experiments

20.1 A/B testing for agent changes

Shadow mode can reveal how a candidate behaves on current inputs, but it cannot show how users would respond to that candidate. When that causal question matters, a randomized A/B test may help—provided both variants are already safe to expose. Assign eligible users, sessions, or another defined unit to candidate or control, then compare outcomes chosen in advance. The assignment unit should limit contamination; a user who sees both versions may carry information or state between them. Before the test, define the estimand, primary metric, guardrails, sample size or stopping method, duration, exclusions, and analysis.

For agents, measure downstream outcomes such as verified task completion, repeat contact, escalation quality, and harmful or unauthorized actions. Clicks, thumbs-up, and model-judge scores are not enough. Track latency and cost as operating constraints. Check randomization, missing outcomes, novelty, interference, and sample-ratio mismatch. Do not use an A/B test to expose users to known unsafe behavior; independent safety gates and stop rules come first. Statistical significance alone does not establish practical value, safety, or transfer to another population.

Worked example: powering a live A/B test

Harbor & Pine wants to compare its current support workflow with a candidate agent. Both have passed independent safety gates. The team will randomize eligible users 1:1, keep each user in the assigned arm, and measure whether that user’s first eligible task is verified as complete within a fixed observation window. Each user contributes one binary outcome. The planning inputs below are fictional:

Planning input Assumption
Baseline completion probability \(p_0\) 0.60
Minimum worthwhile absolute increase \(\delta\) 0.05, or 5 percentage points; planned candidate rate \(p_1=0.65\)
Type I error \(\alpha\) 0.05, two-sided
Target power \(1-\beta\) 0.80
Allocation and analysis Equal independent arms; one fixed-horizon comparison of two proportions

Power is the probability that the specified test rejects its null hypothesis when the specified alternative and design assumptions are true. The 80% target here is for detecting a true five-point improvement. It is not the probability that the candidate works, and it does not guarantee detection of every improvement. Product owners choose the minimum worthwhile effect; with the same sample, smaller effects are generally harder to detect.

For this equal-arm design, a common large-sample normal approximation gives the required observed outcomes per arm:

\[ n \approx \frac{ \left[z_{1-\alpha/2}\sqrt{2\bar{p}(1-\bar{p})} + z_{1-\beta}\sqrt{p_0(1-p_0)+p_1(1-p_1)}\right]^2 }{ (p_1-p_0)^2 }, \qquad \bar{p}=\frac{p_0+p_1}{2}. \]

With \(\bar p=0.625\), \(z_{1-\alpha/2}\approx1.960\), and \(z_{1-\beta}\approx0.842\), the estimate is \(n\approx1470.49\). Round up to 1,471 users with observed outcomes per arm, or 2,942 altogether. This standard-library calculation reproduces the result without a model or external service:

from math import ceil, sqrt
from statistics import NormalDist

p0, p1, alpha, power = 0.60, 0.65, 0.05, 0.80
p_bar = (p0 + p1) / 2
z_alpha = NormalDist().inv_cdf(1 - alpha / 2)
z_power = NormalDist().inv_cdf(power)
n = ceil((
    (z_alpha * sqrt(2 * p_bar * (1 - p_bar))
     + z_power * sqrt(p0 * (1 - p0) + p1 * (1 - p1)))
    / (p1 - p0)
) ** 2)
print(f"observed users per arm: {n}; total: {2 * n}")
observed users per arm: 1471; total: 2942

At 250 eligible unique users per day, assuming all are randomized and every outcome is observed, enrollment takes about \(\lceil 2942/250\rceil=12\) days. The outcome window adds time after enrollment. If the team expects outcomes for only 90% of assigned users, a simple allowance is \(\lceil1471/0.90\rceil=1{,}635\) assigned users per arm. That allowance covers expected volume, not missing-outcome bias. Examine missingness by arm, define the analysis population and missing-data handling before the test, and run sensitivity analyses. More assignments do not fix missingness that depends on treatment or outcome.

This approximation cannot guarantee the power a test will achieve. It assumes plausible baseline and target rates, independent user-level observations, equal allocation, a consistently measured binary outcome, and one fixed-horizon, two-sided comparison. If the planned analysis uses another test, covariate adjustment, or allocation, calculate or simulate power for that design. Repeat interactions, shared organizations, interference, and multiple sessions can create clustering; randomize and analyze at the right unit and include correlation in the plan. Rare safety outcomes may need far more evidence or a different design. A primary-metric sample size does not establish safety. Multiple primary tests, sequential looks, or early efficacy stopping need predeclared multiplicity or sequential methods. Continue safety monitoring and stop for harm; never use the experiment to expose users to a known unsafe variant.

Chapter 2’s paired-task planning formula covers a different design: both systems run on the same offline tasks. It does not apply to these independent randomized user arms unless the experiment itself uses a matching paired design.

20.2 Run a synthetic fixed-horizon analysis

The power calculation above plans a study large enough to detect a specified effect under its assumptions. The smaller, separate exercise below instead shows how to inspect assignment and outcome counts; it cannot establish that the planned study has enough evidence or demonstrate a treatment effect.

Run python code\workflow\lab_workflow.py. The experiment section assigns 200 fictional user IDs to control or candidate with a stable hash of the experiment and user IDs, then analyzes one fabricated binary outcome per assigned user. It reports assigned, observed, missing and successful counts per arm, the candidate-minus-control observed rate difference, and a 1:1 sample-ratio mismatch diagnostic. The example uses a deliberately small teaching threshold of 80 observed outcomes per arm; it is not the 1,471 per arm required by the separate power example above.

Missing outcomes, insufficient observed outcomes or an SRM diagnostic below the prespecified 0.01 threshold yield insufficient_evidence; otherwise the status is still descriptive_only. The lab does not compute a confidence interval or p-value for treatment effect, account for clustering, implement sequential testing, assess guardrails, or authorize a launch. The simple SRM approximation also needs adequate counts. The fixture outcomes are fabricated independently of treatment and do not demonstrate a real product effect.

The stable hash is reproducible but is not a secure randomization service: real deployments need governed assignment, unpredictable allocation where appropriate, eligibility and exposure logging, persistent user-level assignment, privacy protections, fixed outcome windows, and independent safety monitoring. Predeclare missing-data and stopping rules. Add a missing outcome to the fixture and inspect both arm denominators and the resulting status; do not interpret complete-case differences as unbiased by default.