Multi-armed bandit

Also called: multi-armed bandit testing, bandit test, MAB

Last updated:

A multi-armed bandit is a test that moves traffic toward whichever variation is performing best while the test is still running. The name comes from a gambler facing a row of slot machines, the old one-armed bandits, and deciding which ones to play. Sticking with the machine that has paid best so far is called exploitation. Every pull spent on a different machine to learn what it pays is exploration, and a bandit algorithm is a rule for dividing pulls between them. In a product test the arms are variations. Each visitor is one pull, and the payout is whatever you count as success, a signup or a purchase, say. Herbert Robbins wrote down the version of the problem still analyzed today in 1952.

Multi-armed bandit vs A/B test

An A/B test holds a fixed split, often 50/50, until it reaches the sample size planned for it, and you pick the winner at the end. Every visitor who saw the losing variation along the way is a cost you accepted in exchange for a clean estimate of how large the difference is. A bandit shrinks that bill by handing more traffic to whichever arm pulls ahead, so fewer people see the weaker version. Precision goes down. Once the split is lopsided, the trailing arm has too few visitors to say how much worse it was, and the standard significance test assumes a split that never moved. Statisticians call the lost value regret, the gap between what the test earned and what serving the best arm from the first visitor would have earned, and bandit algorithms are compared by how little of it they run up.

How bandit algorithms decide

Epsilon-greedy sends most traffic to the arm with the best observed rate and a small fixed share, say 10%, to a random arm, so every arm keeps getting measured. You can explain it in a sentence. It also keeps exploring long after the answer is obvious, spending that 10% on arms that lost weeks ago. Upper confidence bound (UCB) scores each arm by its observed rate plus an allowance for uncertainty and plays the top score. An arm with few visitors gets a big allowance. So an untried arm is played early and drops back once its numbers come in. Thompson sampling keeps a probability distribution over each arm's true rate, draws one value from each distribution, and plays the arm with the highest draw. Over many rounds an arm gets traffic in proportion to the chance that it is the best one. Lilian Weng's survey of the three works through the math.

When a bandit beats an A/B test

Take a banner for a three-day sale. By the time an A/B test reached significance the sale would be over, and the clicks earned along the way are the whole point. A subject line for one email send works the same way. Bandits also cope with many arms, because the weak ones are starved early. Run an A/B test when someone will build on the result, such as keeping a redesigned checkout, because that decision needs an effect size and a confidence interval. The same applies when the outcome takes days to appear. A bandit learns only from rewards it has already seen, and a retention number read at day 30 arrives weeks after the traffic was split. If a variation could win on clicks while raising refunds, check those guardrail metrics on a fixed split before letting an algorithm chase the click rate. For the wider practice, see experimentation.

Running a bandit on feature flags

For a two-arm test, a flag's two variations are the arms and a targeting rule's rollout percentage is the weight: the share of matching users who get the chosen variation, with the rest going to the other one. The algorithm lives in your own code. An inspector on the SDK sends every exposure to your analytics tool (the analytics integration guide has PostHog and Amplitude examples), and the conversion is logged there as an ordinary event. A scheduled job reads both, computes the new weight and writes it back to the rule through the Management API, and the flag serves the new split from then on.

What a reweight does to your users

Sticky bucketing pins each user to one variation only until the weight changes. Users whose bucket falls in the range a reweight shifts then switch arms, so raising the leader from 30% to 50% moves a fifth of the users the rule matches over to it. For a change people can see, such as a page layout, those users notice the switch and carry both experiences into your numbers. So reweight on a schedule, hourly or daily, instead of after every visitor. Keep a floor of about 5% under each arm, or the job can't notice a trailing arm that recovers.

Want the full picture? Read the concept guide: Analytics & experimentation integration →

Try it in your own app

The Free plan covers 10 flags and 2 environments. No credit card, no demo call — sign up and ship.