Skip to content

Multi-Armed Bandit

Multi-armed bandit (MAB) experiments dynamically reallocate traffic toward better-performing variants as data accumulates, rather than maintaining fixed allocations throughout the experiment.


Algorithms

Each algorithm turns every variant's pulls and successes into a weight; the weights add up to 1 and decide where new users go (see How the weights affect traffic). An experiment is a bandit only when it names one of the three; optimization_type defaults to fixed.

Thompson Sampling

Each variant has a Beta posterior, Beta(1 + successes, 1 + failures). A weight update draws 10,000 samples from every posterior, and each variant's weight is the share of draws in which it is highest.

UCB1 (Upper Confidence Bound)

Each variant's score is mean + sqrt(2 * ln(total_pulls) / variant_pulls), and its weight is its score divided by the sum of the scores, so every variant keeps some traffic. While any variant has no pulls, the variants with none share all the weight equally.

Epsilon-Greedy

The variant with the highest conversion rate gets the weight 0.9 + 0.1 / n, and each of the other variants 0.1 / n, for n variants: epsilon is 0.1, and it is not a setting.


Creating a MAB Experiment

To create one in the dashboard, choose Bandit as the experiment type on the new-experiment form (guided setup or the single-page form) and pick the Optimization algorithm. The rest of this section does the same through the API.

Run the commands on this page in one terminal, in order, against the stack from the Quick Start. Each uses the shell variables set by the ones before it. Log in first; creating an experiment takes permission to create experiments, such as the DEVELOPER or ADMIN role:

TOKEN=$(curl -s -X POST localhost:8000/api/v1/auth/login \
  -H 'content-type: application/json' \
  -d '{"email":"admin@demo.com","password":"Demo1234!"}' | jq -r .access_token)

curl -s localhost:8000/api/v1/auth/me -H "Authorization: Bearer $TOKEN" | jq .role

It prints "ADMIN".

Set optimization_type to one of thompson_sampling, ucb1 or epsilon_greedy when you create the experiment. The bandit counts a conversion as an event matching the experiment's primary metric. This saves the experiment's id in $EXP_ID:

EXP=$(curl -s -X POST localhost:8000/api/v1/experiments/ \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d '{
    "name": "CTA Button Bandit",
    "optimization_type": "thompson_sampling",
    "variants": [
      {"name": "control", "is_control": true, "traffic_allocation": 34},
      {"name": "red_button", "traffic_allocation": 33},
      {"name": "green_button", "traffic_allocation": 33}
    ],
    "metrics": [
      {"name": "CTA click", "event_name": "cta_click", "is_primary": true}
    ]
  }')
EXP_ID=$(jq -r .id <<<"$EXP")

jq '{name, status}' <<<"$EXP"

The experiment is created in draft. Variants' traffic_allocations are whole percentages that add up to 100; they decide the split until the bandit's first weight update.


API Reference

GET /api/v1/bandit/{experiment_id}

Returns the current bandit state: the algorithm, and each variant's weight, pulls, successes and conversion rate. Anyone who may view the experiment can read it. This also saves each variant's id, for the weight override further down:

STATUS=$(curl -s localhost:8000/api/v1/bandit/$EXP_ID \
  -H "Authorization: Bearer $TOKEN")
CONTROL_ID=$(jq -r '.current_weights[] | select(.variant_name == "control") | .variant_id' <<<"$STATUS")
RED_ID=$(jq -r '.current_weights[] | select(.variant_name == "red_button") | .variant_id' <<<"$STATUS")
GREEN_ID=$(jq -r '.current_weights[] | select(.variant_name == "green_button") | .variant_id' <<<"$STATUS")

jq '{algorithm, total_pulls, recommendation}' <<<"$STATUS"
{
  "algorithm": "thompson_sampling",
  "total_pulls": 0,
  "recommendation": "EXPLORING"
}

Before any update has run, every variant has an equal weight. With traffic, a response looks like this:

{
  "experiment_id": "0bdda20f-2cd5-4e99-8ba0-0ce5fc378761",
  "algorithm": "thompson_sampling",
  "current_weights": [
    {
      "variant_id": "104a0022-083b-4e22-a793-096788bbe49b",
      "variant_name": "control",
      "current_weight": 0.18,
      "successes": 820,
      "pulls": 2246,
      "conversion_rate": 0.365
    },
    {
      "variant_id": "1f1f863f-38ae-49c9-b995-60290d58def3",
      "variant_name": "red_button",
      "current_weight": 0.61,
      "successes": 3890,
      "pulls": 6382,
      "conversion_rate": 0.61
    },
    {
      "variant_id": "6f07b272-cbcc-47f9-8336-c2b58992949a",
      "variant_name": "green_button",
      "current_weight": 0.21,
      "successes": 1240,
      "pulls": 3852,
      "conversion_rate": 0.322
    }
  ],
  "total_pulls": 12480,
  "regret_reduction_pct": 2.1,
  "recommendation": "CONVERGING",
  "last_updated": "2026-09-26T14:32:00+00:00",
  "seed": 6346510783624545786,
  "n_samples": 10000,
  "engine_version": "1.2.0"
}

Response Fields

Field Description
algorithm The experiment's optimization_type
current_weight Current traffic allocation fraction (0–1, sums to 1.0 across variants)
pulls Total assignments to this variant
successes Converting users in this variant: users assigned to it with at least one conversion event, each counted once
regret_reduction_pct Estimated regret reduction compared with a uniform split; null until there is data, and while the weights do no better than a uniform split
recommendation EXPLORING, CONVERGING, or DEPLOYING_<variant name> once one variant dominates
last_updated When the weights were last computed; null before the first update
seed, n_samples The random seed and draws per variant of the last Thompson-sampling update; null for the other algorithms

An experiment whose optimization_type is fixed answers 404 here.


POST /api/v1/bandit/{experiment_id}/update

Recalculates the weights now, outside the scheduler cycle. Requires permission to change experiments, such as the DEVELOPER or ADMIN role:

curl -s -X POST localhost:8000/api/v1/bandit/$EXP_ID/update \
  -H "Authorization: Bearer $TOKEN" | jq '{algorithm, n_samples}'

It returns the new state, which now has a last_updated time. Thompson sampling draws n_samples (10000) samples per variant, so with no data yet the weights move a little away from equal at random.


PUT /api/v1/bandit/{experiment_id}/weights

Overrides the variant weights. Requires the ADMIN role. The body maps each variant's id to its weight; the weights must be at least 0 and add up to 1.0, or the API answers 422. Use with caution: the scheduler replaces an override on its next cycle while the experiment is active.

In the dashboard, an Admin or a superuser uses Set weights by hand in the experiment page's Current traffic weights panel; it sends this request with each variant's whole percentage divided by 100.

curl -s -X PUT localhost:8000/api/v1/bandit/$EXP_ID/weights \
  -H "Authorization: Bearer $TOKEN" \
  -H 'content-type: application/json' \
  -d "{
    \"weights\": {
      \"$CONTROL_ID\": 0.1,
      \"$RED_ID\": 0.8,
      \"$GREEN_ID\": 0.1
    }
  }" | jq -c '[.current_weights[] | {variant_name, current_weight}]'

It prints the new weights:

[{"variant_name":"control","current_weight":0.1},{"variant_name":"red_button","current_weight":0.8},{"variant_name":"green_button","current_weight":0.1}]

GET /api/v1/bandit/{experiment_id}/history

Returns the bandit's past weight updates, newest first: each update's time (tick_at) and each variant's weight, keyed by variant id. Anyone who may view the experiment can read it. A weight override is not an update and is not listed. limit (default 200, 1 to 1000, otherwise 422) is the number of updates. It answers [] before the first update, and 404 for an unknown experiment or one whose optimization_type is fixed. Updates older than ANALYSIS_HISTORY_RETENTION_DAYS (default 90) are deleted. It is beta (x-stability: beta); the shape may still change.

curl -s "localhost:8000/api/v1/bandit/$EXP_ID/history" \
  -H "Authorization: Bearer $TOKEN" | jq -c '{updates: length, variants: (.[0].weights | length)}'

It prints one update, the one triggered above; the override is not listed.

The dashboard shows the same updates under Weight history on the experiment's page.


Scheduler

The bandit scheduler (BanditSchedulerRunner in backend/app/core/bandit_scheduler.py) starts with the API process and recalculates weights for every ACTIVE experiment whose optimization_type is not fixed. The cadence is BANDIT_UPDATE_INTERVAL_MINUTES (default 5). It does not run under APP_ENV=test.

Where the counts come from

For each variant the scheduler needs pulls (assignments) and successes (conversions). Sources are tried in order:

  1. PostgreSQL: pulls = assignments rows per variant; successes = converting users, counted exactly as /results counts them: users assigned to the variant with at least one event, tagged with that variant, whose event_name matches the experiment's primary metric (see the event-matching rule in the tracking API docs), inside the primary metric's conversion window when it has one (Conversion window). A user who converts three times counts once. With no metric configured, every event other than an experiment view is a success. It is used whenever any variant has an assignment there, and its pulls and successes are used whole: a DynamoDB count that is larger, or ahead, never replaces or mixes into them.
  2. DynamoDB real-time counters (get_experiment_counters), when the counters stack is deployed, only when PostgreSQL has no assignments for the experiment or cannot be read. Its conversion counter counts conversion events, not users, so a user who converts three times counts three there; the scheduler caps its successes at its pulls and cannot turn them into users. It applies no conversion window.
  3. The previously persisted BanditState.
  4. Zero-count priors (equal weights).

When the experiment's primary metric has lower_is_better set (uninstalls, errors), the bandit counts a user who did not convert as the success, so the variant with fewer converting users earns the traffic. Every algorithm and both live sources (DynamoDB and PostgreSQL) apply this, and so does the persisted BanditState fallback. The stored and reported successes and conversion_rate stay the converting users and their share, whatever the direction; only the algorithms see the flipped counts. Because a user who has not yet converted counts as a success, the most recent assignments look best until they have had time to convert, so such a bandit needs an event that comes soon after assignment (an event that happens days after assignment gives a misleading early signal).

With a conversion window on the primary metric, a user still inside their window counts as not converted until they convert, as without one. The arm that got more of the recent traffic has more users still inside their window, so more of its users can still convert: as those windows close its share of converting users can rise, and with lower_is_better its successes fall.

How the weights affect traffic

POST /api/v1/tracking/assign reads the latest BanditState for bandit experiments and routes new users according to the current weights (a deterministic hash of the user id is looked up in the cumulative weight distribution, so repeated calls agree before the assignment row exists). Users who already have an assignment keep it. Variants whose weight is 0 receive no new traffic. Until the first refresh has run, new users are split by the variants' traffic_allocation.


Permissions

Action Minimum Role
View bandit status VIEWER
View weight history VIEWER
Trigger weight update DEVELOPER
Override weights ADMIN