## Configuring a Multi-Armed Bandit in LaunchDarkly

This topic explains how to set up and configure a multi-armed bandit (MAB) in LaunchDarkly.

### Steps to Configure a MAB:
1. [Creating the flag or AgentControl config and its variations](/content/docs/fed-docs/home/multi-armed-bandits/create#create-flags-or-agentcontrol-configs/index.html)
2. [Creating a metric](/content/docs/fed-docs/home/multi-armed-bandits/create#create-metrics/index.html)
3. [Building the multi-armed bandit](/content/docs/fed-docs/home/multi-armed-bandits/create#build-multi-armed-bandits/index.html)
4. [Starting an iteration](/content/docs/fed-docs/home/multi-armed-bandits/create#start-iterations/index.html)

### Prerequisites
Before you build a MAB, you should read about and understand the following concepts:
- [Multi-armed bandits](/content/docs/fed-docs/home/multi-armed-bandits/index.html)
- [randomization units](/content/docs/fed-docs/home/experimentation/randomization/index.html)

## Create Flags or AgentControl Configs
Before you begin a MAB, create a flag or AgentControl config with the variations you plan to optimize the performance of. You do not need to toggle on the flag before you create the MAB, but you do have to toggle flags on before you [start an iteration](/content/docs/fed-docs/home/multi-armed-bandits/create#start-iterations/index.html). AgentControl configs are on by default.

To learn more, read [Creating new flags](/content/docs/fed-docs/home/flags/new/index.html), [Creating flag variations](/content/docs/fed-docs/home/flags/variations/index.html), [Create configs](/content/docs/fed-docs/home/agentcontrol/create/index.html), and [Create and manage config variations](/content/docs/fed-docs/home/agentcontrol/create-variation/index.html).

You cannot run a MAB on a flag if:
- the flag has an active [guarded rollout](/content/docs/fed-docs/home/releases/guarded-rollouts/index.html)
- the flag has an active [progressive rollout](/content/docs/fed-docs/home/releases/progressive-rollouts/index.html)
- the flag is in a running Data Export experiment
- the flag is in a running [experiment with warehouse native metrics](/content/docs/fed-docs/home/warehouse-native/creating/index.html)
- the flag is a [migration flag](/content/docs/fed-docs/home/flags/migration/index.html)

You can build and run multiple MABs on the same flag or AgentControl config as long as there is only one running MAB per targeting rule. You cannot run multiple MABs on the same rule at the same time.

## Create Metrics
Metrics measure audience behaviors affected by your flags. You can use metrics to track all kinds of things, from how often end users access a URL to how long that URL takes to load a page. You can reuse existing metrics in multiple MABs, or create new ones for your MABs.

To learn how to create your own new metric, read [Metrics](/content/docs/fed-docs/home/metrics/index.html). LaunchDarkly also automatically creates metrics for AgentControl configs. To learn more, read [AgentControl config autogenerated metrics](/content/docs/fed-docs/home/metrics/autogen/ai/index.html).

##### Multi-armed bandits do not support metrics using percentile analysis methods

Multi-armed bandits support metrics using the "Average" [metric analysis method](/content/docs/fed-docs/home/metrics/components#analysis-method/index.html). You cannot use metrics using a percentile analysis method with a multi-armed bandit.

If you want to learn which variation performs better, it must be possible for that metric to measure something in all of the variations within the MAB. To learn more, read [Metrics and flag variations](/content/docs/fed-docs/guides/experimentation/designing-experiments#metrics-and-variations/index.html).

## Build Multi-Armed Bandits
You can view all of the MABs in your environment on the **Multi-armed bandits** list.

To build a MAB:
1. Click **Create**, then choose **Multi-armed bandits**. A "Create multi-armed bandit" dialog appears.
2. Enter a **Name**.
3. Enter a **Goal**.
4. Click **Create multi-armed bandit**. The **Design** tab opens.
5. Choose a context kind to **Randomize by**.
6. Select a **Metric**.
   - Hover over a metric to see which environments have received events for it. If no environments are receiving events, check that your SDKs are configured correctly.
   - Click **Create** to create and use a new metric.
7. Choose a **Flag or config**.
   - Click **Create flag** or **Create AgentControl config** to create and use a new flag or AgentControl config.
8. Choose a targeting rule for the **Experiment audience**.
   - If you want to restrict your MAB audience to only contexts with certain attributes, create a targeting rule on the flag or AgentControl config you include in the MAB and run the experiment on that rule. The context kind that the flag rule targets should match the randomization unit of your MAB.
   - If the flag or AgentControl config doesn’t have any targeting rules, the default rule is the only option.

9. (Optional) If you want to [exclude contexts in this MAB](/content/docs/fed-docs/home/experimentation/mutually-exclusive/index.html) from certain other MABs and experiments, click **Add experiment to exclusion layer** and select a layer.

###### Expand layer options

A [layer](/content/docs/fed-docs/home/experimentation/mutually-exclusive/index.html) is a set of experiments and MABs that cannot share traffic with each other. All of the experiments and MABs within a layer are mutually exclusive, which means that if a context is included in one, LaunchDarkly will exclude it from any other experiments and MABs in the same layer. All of the MABs within a layer must randomize by the same context kind, such as "user."

To add the MAB to an existing layer:
1. Click **Select layer**.
2. Search for and choose the layer you want to add the MAB to.
3. Enter a **Reservation** amount. This is the percentage of the contexts within this layer you want LaunchDarkly to include in this MAB.
4. Click **Save layer**.

If you need to create a new layer:
1. Click **Create layer**.
2. Add a **Name** and **Description**.
3. Click **Create layer**.
4. Enter a **Reservation** amount. This is the percentage of the contexts within this layer you want LaunchDarkly to include in this MAB.
5. Click **Save layer**.

10. Select a **Variation served to users outside this experiment**.
11. Select the percentage of contexts that match the MAB’s targeting rule to include in the MAB.
12. Select your desired **Update frequency**. This determines how often the MAB shifts traffic to the leading variation.
13. By default, starting traffic is split equally between all of the included variations. If you want to exclude a variation from the MAB:
    - Click **Edit** next to "Variations split."
    - For any variation you want to exclude, change the percentage to 0%.
    - Click **Save audience split**.
14. (Optional) If you want to include the MAB in a [holdout](/content/docs/fed-docs/home/holdouts/index.html), select a **Holdout** name.

##### Multi-armed bandits cannot be in a holdout and in a layer at the same time

Multi-armed bandits can either be in a holdout or in a layer, but not both. If you added the multi-armed bandit to a layer, you will not see the option to add it to a holdout.

15. Scroll to the top of the page and click **Save**.

If needed, you can save your in-progress MAB design to finish later. To save your design, click **Save** at the top of the creation screen. Your in-progress MAB design is saved and appears on the **Multi-armed bandits** list. To finish building the MAB, click on the MAB’s name and continue editing.

After you have created your MAB, the next step is to toggle on the flag. AgentControl configs are on by default. Then, you can start an iteration.

You can also use the REST API: [Create experiment](/content/docs/fed-docs/api/experiments/create-experiment/index.html)

## Start Iterations
After you create a MAB you can start an iteration in one or more environments.

To start a MAB iteration:
1. Navigate to the **Multi-armed bandits** list.
2. Click on the environment section containing the MAB you want to start.
   - If the environment you need isn’t visible, click the **+** next to the list of environment sections. Search for the environment you want, and select it from the list.

3. Click on the name of the MAB you want to start an iteration for. The **Design** tab appears.
4. Click **Start**, or, if your environment requires approvals, click **Request approval to start**. A “Start multi-armed bandit” dialog appears.
   - If your environment does not require approvals, but you want to request approval anyway, click the **down arrow** next to the **Start** button and click **Request approval to start**.
5. (Optional) If the flag is off, click **Toggle on flag**.
6. Click **Start multi-armed bandit**.
7. Repeat the steps for each environment you want to start an iteration in.

You can run a MAB indefinitely, allowing it to reallocate traffic as needed. If you are running a MAB on a time-boxed feature, such as a holiday promotion, then you can stop the MAB iteration when the promotion is over.

When you start an iteration of a MAB, LaunchDarkly sends the maintainer and anyone following the MAB an email, an in-app notification, and, if you have the Slack app integration configured, a Slack notification.

To learn more about starting and stopping iterations, read [Starting and stopping experiment iterations](/content/docs/fed-docs/home/experimentation/start-stop-exp/index.html).

You can also use the REST API: [Create iteration](/content/docs/fed-docs/api/experiments/create-iteration/index.html)
