---
title: Advanced Bandit Usage
description: "How to use Statsig bandits in code: check a bandit like an experiment, pass context features, get a ranked list of variants, and log the outcomes that train the model."
product: general
lang: en
last_updated: 2025-09-18
token_estimate: 1167
---
# Advanced Bandit Usage

You manage both contextual and non-contextual bandits in the Statsig console, or programmatically through the [Console API](https://docs.statsig.com/console-api/introduction). Both bandit types use the same SDK calls as experiments, so you can add a bandit to your code without learning a new API.

> For AI agents: a documentation index is available at [/llms.txt](/llms.txt). Append `.md` to any page URL for markdown, or send `Accept: text/markdown`.

## How a bandit works in your app

A bandit runs as a loop between your code and Statsig:

1. Your code checks the bandit with the same call you use for an experiment. For a contextual bandit, first add the user's context features as custom attributes on the user object.
2. Statsig returns one variant's JSON config and logs an exposure for that user.
3. When the user completes the target action, Statsig records the outcome. In Statsig Cloud, your code logs the success event. In Warehouse Native, Statsig reads outcomes from your metric source.
4. Statsig connects each outcome to the exposure that came before it, within the attribution window you set.
5. Statsig retrains the bandit hourly and shifts traffic toward the variants that perform best.

To confirm that exposures reach Statsig, check the Exposure Stream on your bandit's _Diagnostics_ tab.

## Checking a bandit

Check bandits using your standard experiment call. For example, configure a bandit's Variant JSON like this:

```json
{
  "text": "<my_text>"
}
```

Then read the value in code using the same pattern as [experiments](https://docs.statsig.com/guides/abn-tests). In React:

```tsx
import { useExperiment } from '@statsig/react-bindings';

const bandit = useExperiment('contextual_bandit');
const banditText = bandit.get('text', 'Default text');
```

For a contextual bandit, include the context features as custom attributes on the user object before you check the bandit, usually when you initialize the SDK:

```tsx
const user = {
  userID: 'user-123',
  custom: { plan: 'pro', platform: 'ios' },
};
```

To change attributes after initialization, go to [Updating user properties](https://docs.statsig.com/client/React#updating-user-properties).

Statsig treats a feature with only numeric values as continuous. Statsig one-hot encodes all other features, using up to the top 25 values that each appear in more than 1% of the data. For details, go to [Feature encoding](https://docs.statsig.com/autotune/contextual/methodology#feature-encoding).

If you wrap the bandit in a linked experiment, check the experiment first and check the bandit only for users in the experiment's treatment group. The two checks log separate exposures, so checking the bandit only where your code uses it avoids extra exposure logging. For a code example, go to [Linked experiments](https://docs.statsig.com/autotune/monitoring#linked-experiments).

## Getting a ranked list

Sometimes you can't show the top variant, for example when client-side filtering or other constraints rule it out. For a contextual bandit, the [Get Ranked List](https://docs.statsig.com/api-reference/autotune/get-ranked-list-for-contextual-bandit) HTTP API endpoint returns every variant with a predicted score, ordered from highest to lowest, so your code can show the highest-ranked variant that fits.

The ranked list requires manual exposure logging. Log an exposure for the variant you display so that Statsig knows which variant the user saw.

## Outcomes

In Statsig Cloud, log the bandit's success event to Statsig, with or without a value. Statsig connects exposures to outcome events within your configured attribution window. For how to log an event, go to [Log your first custom event](https://docs.statsig.com/guides/logging-events).

In Statsig Warehouse Native, provide your outcome dataset as a [metric source](https://docs.statsig.com/statsig-warehouse-native/configuration/metric-sources).

> **Note:**
>
> Data latency matters: if your outcome dataset has more than 24 hours of lag, bandit exploration can slow down significantly.
>
> In Warehouse Native, Statsig treats your metric source as complete up to the latest timestamp it contains, and scores each exposure once its attribution window ends before that timestamp. If an outcome hasn't landed in the metric source by then, Statsig counts that exposure as a failure. Later hourly updates correct it, but a winner declared in the meantime stays declared. Make sure the metric source's timestamp only moves forward once all outcome data up to that time has landed, for example when the source joins tables that load at different times or receives late events from devices.

