---
title: "A/B tests"
subtitle: "Try another model on a share of your real conversations, with a budget and an end date, and compare cost, latency and scores side by side."
---

An A/B test sends a share of your live conversations through a different model, so you can see how it does on real traffic before you switch. You pick the share, a budget and an end date. Respan keeps each conversation on one side for all its turns, and stops the test when it reaches its budget or end date.

| Mode | What users get | What it costs |
|---|---|---|
| `shadow` (default) | Every user gets your model's answer. About a minute after a sampled conversation goes quiet, Respan replays it on the variant in the background. | The replays bill to your project and count toward the budget. |
| `split` | Users in the sampled share get a variant's answer, on every turn of their conversation. The rest stay on your model. | Each variant's calls bill as usual and count toward the budget. |

Use `shadow` to try a model with no risk to users. Use `split` when you want the variant to answer real users, or to compare several variants at once.

An A/B test is a version of a sessions dataset: the dataset collects the sampled conversations, and the test is the version that runs them on the variant. Respan creates that dataset for you when you start the test.

## Start a test

<Steps>
<Step title="Send each conversation with a session ID">

A/B tests only cover requests sent through the [Respan gateway](/docs/ai-gateway/quickstart) with a `session_identifier`, one per conversation. Requests without one always use your model. See [session IDs and attributes](/docs/ai-gateway/customers/respan-params).

<Tabs>
<Tab title="OpenAI (Python)">
```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.respan.ai/api/",
    api_key=os.environ["RESPAN_API_KEY"],
)

response = client.chat.completions.create(
    model="gpt-5.5",
    messages=[{"role": "user", "content": "Where is my order?"}],
    extra_body={"session_identifier": "conv_5d21"},
)
```
</Tab>
<Tab title="cURL">
```bash
curl https://api.respan.ai/api/chat/completions \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.5",
    "messages": [{"role": "user", "content": "Where is my order?"}],
    "session_identifier": "conv_5d21"
  }'
```
</Tab>
</Tabs>

</Step>
<Step title="Start the test">

Send `live_sampling` to `POST /api/datasets/`. Respan creates the test's sessions dataset, named after the test, and starts the test at once.

This shadow test replays 20% of conversations on `gpt-4.1-nano`, with a $5 budget, until the end of the year:

<Tabs>
<Tab title="cURL">
```bash
curl https://api.respan.ai/api/datasets/ \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Support bot: gpt-4.1-nano",
    "workflow": [{"type": "completion", "config": {"model": "gpt-4.1-nano"}}],
    "live_sampling": {
      "sampling_rate": 0.2,
      "budget_usd": 5,
      "expires_at": "2026-12-31T00:00:00Z"
    }
  }'
```
</Tab>
<Tab title="Python">
```python
import os
import requests

response = requests.post(
    "https://api.respan.ai/api/datasets/",
    headers={"Authorization": f"Bearer {os.environ['RESPAN_API_KEY']}"},
    json={
        "name": "Support bot: gpt-4.1-nano",
        "workflow": [{"type": "completion", "config": {"model": "gpt-4.1-nano"}}],
        "live_sampling": {
            "sampling_rate": 0.2,
            "budget_usd": 5,
            "expires_at": "2026-12-31T00:00:00Z",
        },
    },
)
ab_test_id = response.json()["id"]
```
</Tab>
</Tabs>

A split test sets `mode` to `split` and lists its `variants`. Each variant names its model, so the test needs no `workflow`. This one serves 35% of conversations on `gpt-4.1-nano` and 35% on `claude-haiku-4-5`:

```json
{
  "name": "Support bot: nano vs haiku",
  "live_sampling": {
    "mode": "split",
    "budget_usd": 10,
    "expires_at": "2026-12-31T00:00:00Z",
    "variants": [
      {"name": "nano", "model": "gpt-4.1-nano", "sampling_rate": 0.35},
      {"name": "haiku", "model": "claude-haiku-4-5", "sampling_rate": 0.35}
    ]
  }
}
```

| Parameter | Type | What it does |
| --- | --- | --- |
| `name` | string | The test's name. |
| `workflow` | array | The variant: a completion step with a model, or a prompt step with a pinned version. In a split test, each variant's `model` replaces the step's model. Optional when every variant names its model. |
| `behavior_settings` | array | The behaviors that score both sides. Defaults to the dataset's. |
| `live_sampling.mode` | string | `shadow` (default) or `split`. |
| `live_sampling.sampling_rate` | number | The share of conversations in the test, from 0 to 1: 0.2 is 20%, 1 is all. For a split test, it's the sum of the variants' shares and can be left out. |
| `live_sampling.budget_usd` | number | What the variant's calls may spend, in USD, before the test stops. Your model's calls don't count. Calls already under way finish, so the spend can go a little over. |
| `live_sampling.expires_at` | string | When the test stops, in ISO 8601. Without an offset it's UTC. |
| `live_sampling.variants` | array | Split tests only, up to 5. Each has a `name` (unique, and not `primary`), a `model`, and a `sampling_rate`. The rest of your conversations stay on your model. A shadow test has one variant and takes none. |

The response (`202`) is the test. Its `id` is the test's ID, `source_dataset` is the sessions dataset Respan created for it, and `running_status` is `running`.

To collect a test's conversations into a sessions dataset you already have, send the same body to `POST /api/datasets/YOUR_DATASET_ID/versions/` instead.

A project serves one split test at a time. A newer split test waits until the older one ends or is paused.

</Step>
<Step title="Let traffic come in">

There's nothing to change in your app. Conversations are sampled as they arrive.

</Step>
</Steps>

## Check a test

```bash
curl https://api.respan.ai/api/datasets/YOUR_AB_TEST_ID/versions/YOUR_AB_TEST_ID/ \
  -H "Authorization: Bearer $RESPAN_API_KEY"
```

Trimmed:

```json
{
  "id": "YOUR_AB_TEST_ID",
  "name": "Support bot: gpt-4.1-nano",
  "source_dataset": "YOUR_DATASET_ID",
  "running_status": "running",
  "live_sampling_stop_reason": null,
  "skipped_sessions": 0,
  "run_config": {
    "model": "gpt-4.1-nano",
    "live_sampling": {"mode": "shadow", "sampling_rate": 0.2, "budget_usd": 5.0, "expires_at": "2026-12-31T00:00:00Z"}
  }
}
```

| Field | What it says |
| --- | --- |
| `running_status` | `running`, `paused`, or `stopped`. |
| `live_sampling_stop_reason` | Why a stopped test ended: `stopped` (you stopped it), `budget` (the variant spent the budget) or `expired` (it reached `expires_at`). `null` while it runs. |
| `skipped_sessions` | Conversations a shadow test left out because it reached its hourly replay limit. |

To list your project's tests, newest first, call `GET https://api.respan.ai/api/datasets/list/?is_ab_test=true`.

## Read the results

### Shadow test

Compare the sessions dataset (what users got) with the test's replays. `YOUR_DATASET_ID` is the test's `source_dataset`:

```bash
curl "https://api.respan.ai/api/datasets/YOUR_DATASET_ID/versions/comparison/?version_ids=YOUR_DATASET_ID,YOUR_AB_TEST_ID" \
  -H "Authorization: Bearer $RESPAN_API_KEY"
```

Trimmed:

```json
{
  "baseline_version_id": "YOUR_DATASET_ID",
  "winning_version_id": null,
  "matched_row_count": 9,
  "versions": [
    {"id": "YOUR_DATASET_ID", "name": "Support bot sessions", "final_score_summary": {"average_final_score": 0.93, "scored_row_count": 9, "total_row_count": 9, "cost": 0.0005}},
    {"id": "YOUR_AB_TEST_ID", "name": "Support bot: gpt-4.1-nano", "final_score_summary": {"average_final_score": 0.93, "scored_row_count": 9, "total_row_count": 18, "cost": 0.0056}, "live_sampling_stop_reason": "stopped", "skipped_sessions": 0}
  ],
  "results": [ ... ]
}
```

Each version's `final_score_summary` has its average score and cost. `results` has one row per conversation, with each side's answer, score, cost and latency in `cells`. Add `row_filter=regressions` or `row_filter=improvements` to list only the conversations that got worse or better. The variant is scored by the behaviors in its `behavior_settings` once the test stops.

### Split test

Every call a split test serves carries `ab_test_side` in its metadata: `primary` or the variant's name. Get one side's calls, cost and latency from the [span summary](/docs/apis/spans/get-span-summary):

```bash
curl -X POST "https://api.respan.ai/api/request-logs/summary/?start_time=2026-12-01T00:00:00Z&end_time=2026-12-02T00:00:00Z" \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"filters": {"metadata__ab_test_side": {"operator": "", "value": "nano"}}}'
```

```json
{"number_of_requests": 221, "total_cost": 0.12, "avg_latency": 0.64}
```

Send it once per side and compare. The test's budget counts the variant sides' `total_cost`.

## Find a test's calls in Logs

A split test's calls carry two custom properties: `ab_test_id` (the test) and `ab_test_side` (`primary` or the variant's name). A shadow test's replays carry `ab_test_name`. In **Observability** › **Logs**, click a call to see them under its custom properties. To show one side's calls, click **Custom property**, pick `ab_test_side`, enter a value such as `nano`, and click **Apply**.

Respan sets `ab_test_id` and `ab_test_side`. Values your app sends under those names are dropped.

## Pause, extend or stop

Send `status` to the test's run:

```bash
curl -X PATCH https://api.respan.ai/api/datasets/YOUR_AB_TEST_ID/run/ \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"status": "paused"}'
```

| `status` | What happens |
| --- | --- |
| `paused` | The test takes no new conversations. Calls and replays already under way finish and bill. |
| `running` | A paused test takes conversations again. |
| `stopped` | The test stops for good, with `live_sampling_stop_reason` `stopped`. A split test sends every user back to your model. Its results stay readable. A stopped test can't be restarted. |

To extend a running or paused test, send `live_sampling` with a higher `budget_usd` or a later `expires_at`:

```bash
curl -X PATCH https://api.respan.ai/api/datasets/YOUR_AB_TEST_ID/ \
  -H "Authorization: Bearer $RESPAN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"live_sampling": {"budget_usd": 25, "expires_at": "2027-01-15T00:00:00Z"}}'
```

Neither can go down, and a test's mode, rate and variants can't change. To change them, stop the test and start a new one.
