Eitan Cohen 7 min readMost product experiments answer a retrospective question: which option performed better across a population? Contextual bandits address a more immediate one: given what is known about this situation, which option should the system choose now?
This distinction matters whenever a product repeatedly selects among actions and receives relatively prompt feedback. A learning platform might choose the next exercise. A support system might decide which troubleshooting step to present. An application might select one of several onboarding prompts. Instead of holding choices fixed until an experiment ends, a contextual bandit learns while allocating more traffic to promising actions.
The mechanism is powerful, but narrower than it first appears. It is not a general-purpose agent, and it does not plan long sequences. It is a disciplined way to make one recurring decision under uncertainty.
The decision pattern a contextual bandit solves
A contextual bandit repeats a four-part loop:
- Observe context: collect information available before the decision, such as device class, account state, or previous activity.
- Choose an action: select one option from a defined set.
- Observe a reward: measure the outcome attributable to that choice.
- Update: revise the estimated value of actions in similar contexts.
The term bandit comes from the multi-armed bandit problem: a gambler must choose among machines with unknown payouts. A contextual bandit adds information about the present situation. The best action need not be universally best; it may depend on the context.
Suppose an analytics product can show one of three setup prompts: import sample data, connect a data source, or watch a guided walkthrough. Its context might include whether the user has technical permissions, whether colleagues already use the product, and whether the account contains data. The reward might be completion of a meaningful setup step during the session.
Why this is not simply an A/B test
An A/B test generally assigns variants according to a fixed randomization rule, then estimates their effects. A contextual bandit changes allocation as evidence arrives and can personalize choices by context.
| Method | Primary objective | Allocation | Best fit |
|---|---|---|---|
| A/B test | Estimate causal differences clearly | Usually fixed during the test | High-confidence comparison and inference |
| Multi-armed bandit | Earn reward while learning | Adapts by action performance | Repeated choice without useful context |
| Contextual bandit | Choose well for each situation | Adapts by action and context | Repeated, personalized decisions |
The trade-off is consequential. Adaptive allocation can reduce exposure to weak actions, but it complicates analysis. Observations are produced by a policy that changes over time. A conventional comparison of raw conversion rates may therefore mislead: one action may have been shown more often in difficult contexts, or mostly during an earlier period.
Use an A/B test when precise effect estimation is the principal goal. Use a contextual bandit when improving cumulative outcomes during learning matters more, the decision repeats often, and rewards arrive soon enough to guide subsequent choices.
The central tension: exploration versus exploitation
If the system always selects the action with the highest current estimate, it may lock onto an early winner caused by noise. This is pure exploitation. If it distributes choices too evenly, it keeps spending opportunities on actions already known to be weak. This is excessive exploration.
A bandit policy manages the tension explicitly. Three common approaches are:
- Epsilon-greedy: usually select the estimated best action, but sometimes choose randomly. It is simple, though random exploration can waste traffic on clearly inferior options.
- Upper confidence bound: favor actions with strong estimated rewards or substantial uncertainty. Uncertain actions receive an exploration bonus.
- Thompson sampling: maintain a probability distribution over each action’s likely value, sample from those distributions, and choose the sampled winner. Uncertain possibilities are explored naturally.
With context, the estimates are produced by a model. A simple version might estimate reward using a separate linear model for each action. More flexible models can represent richer relationships, but they also require more data, stronger monitoring, and stricter control of feature drift.
Worked example: choosing an onboarding intervention
Consider a project-management product deciding what to show after a user creates an empty workspace. It has three actions:
- A: offer a project template.
- B: invite the user to import existing tasks.
- C: open a short interactive tour.
The team defines reward as creating or importing at least five tasks before the session ends. This is better than measuring clicks on the intervention because it sits closer to the desired product outcome.
For a first implementation, the context contains only three pre-decision features: device category, whether the user indicated they are migrating from another tool, and whether they selected an individual or team workspace. Keeping the feature set small makes failures easier to interpret.
Step 1: begin with controlled exploration
For an initial learning period, the policy assigns actions with known probabilities. It logs every decision, including options not chosen. One record might say that a migrating team user on desktop received action B, which had a selection probability of 0.4, and later earned a reward of 1.
The probability is essential. Without it, the team cannot reliably reconstruct how the changing policy produced its data or evaluate replacement policies using logged interactions.
Step 2: update beliefs from observed rewards
As outcomes arrive, the model learns relationships rather than one global ranking. It may discover that importing tasks works well for migrating users, while templates work better for new individual users. The tour might remain useful on mobile, where import is cumbersome.
This is the contextual advantage: the system can preserve several locally effective actions instead of declaring one universal winner.
Step 3: make the policy operational
For each eligible request, the service validates the context, scores available actions, applies its exploration rule, and returns both the choice and its probability. The product renders the intervention, then sends the eventual reward using the same decision identifier.
If the reward does not arrive before a defined attribution window closes, the event becomes a zero or an explicitly missing outcome according to a rule chosen in advance. Quietly dropping unsuccessful sessions would bias learning toward success.
Step 4: judge more than reward
The team compares the adaptive policy with a fixed baseline in a randomized holdout. It also segments results by context and monitors action exposure. A higher total reward is not sufficient if one user group receives a poor experience or if exploration collapses prematurely.
The event log is the real foundation
A contextual bandit is only as trustworthy as its decision log. Each record should preserve:
- a unique decision identifier and timestamp;
- the context exactly as known before selection;
- the complete set of actions eligible at that moment;
- the selected action and its selection probability;
- the policy and model version;
- the reward, its timestamp, and attribution status;
- any safety rule or fallback that overrode the policy.
Pre-decision context must remain distinct from information observed later. If the model trains on a feature created after the action, it gains access to the future and offline performance becomes fiction.
Selection probabilities support off-policy evaluation: estimating how another policy might have performed using data gathered by the current one. The basic idea is to give more weight to events that the proposed policy would have selected but the logging policy chose rarely. These estimates can become unstable when probabilities are very small, so they are evidence for staged deployment, not permission to skip live validation.
Where implementations quietly fail
The reward is a proxy the policy can game. If onboarding prompts optimize clicks, a conspicuous but unhelpful prompt may win. Pair an immediate reward with guardrail metrics such as abandonment, reversal, or later task completion.
Rewards arrive too late. A purchase months later creates slow, ambiguous feedback. Use a defensible nearer-term signal or choose a different method.
Actions change meaning. If designers substantially revise a template but retain its identifier, the model combines evidence from two different interventions. Version actions whenever their behavior changes.
Context becomes destiny. Historical behavior can encode unequal access or treatment. Exclude sensitive attributes unless their use is justified and governed, inspect outcomes across relevant groups, and impose eligibility constraints before optimization.
The environment shifts. Seasonality, product releases, and acquisition changes can invalidate learned preferences. Monitor reward by time, retain some exploration, and define reset or retraining rules.
A practical adoption test
Before building a contextual bandit, answer five questions:
- Is there a recurring decision with a finite, well-defined action set?
- Can context be captured before the decision without leakage?
- Does a meaningful reward arrive soon and frequently enough to support learning?
- Can the product tolerate deliberate exploration?
- Can every choice, probability, outcome, and override be logged reliably?
If any answer is no, begin with deterministic rules or a conventional experiment. Bandits do not repair vague objectives, sparse feedback, or weak instrumentation; they amplify those defects through automation.
The deeper opportunity is not merely personalization. A contextual bandit turns a product surface into a controlled learning system: one that records uncertainty, spends exploration deliberately, and becomes more selective as evidence accumulates. Its sophistication lies less in the model than in the discipline surrounding each decision.
This post was drafted with AI assistance and reviewed against our editorial policy before publication. Corrections are made at the source, on the page, with the date shown.
From our own rounds
Measured on The Curator, from real sessions people played on this site — not a third-party dataset.
- Rounds played here
- 147
- Questions per round
- 1.7
Rate this article
Discussion
Comments are moderated. Read our editorial policy.