> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cartesia.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# A/B testing

> Compare managed agent versions by splitting live calls and measuring their results.

Route new calls across managed agent versions to compare changes with production traffic. Traffic allocation does not change the published version, and calls already in progress stay on the version selected when they started.

```bash theme={null}
curl -X PUT "https://api.cartesia.ai/agents/$AGENT_ID/traffic-allocation" \
  -H "X-API-Key: $CARTESIA_API_KEY" \
  -H "Cartesia-Version: 2026-08-14" \
  -H "Content-Type: application/json" \
  -d '{
    "assignment_strategy": "random_per_call",
    "targets": [
      { "version_id": "av_baseline", "percentage": 50 },
      { "version_id": "av_variant", "percentage": 50 }
    ]
  }'
```

## Before you start

Create a [custom metric](https://docs.cartesia.ai/line/evaluations/metrics.md#custom-metrics) for the outcome you want to compare. Assign the metric before you start the test so Cartesia evaluates calls from every version.

You also need at least two versions of the same managed agent. Publish a change in the Playground or [update the agent](https://docs.cartesia.ai/api-reference/agents/update.md) to create a version. Then [list the agent's versions](https://docs.cartesia.ai/api-reference/agents/versions/list.md) and copy their `id` values. You do not need to deploy managed versions before you add them to an allocation.

## Split traffic between versions

Call [`PUT /agents/{agent_id}/traffic-allocation`](https://docs.cartesia.ai/api-reference/agents/traffic-allocations/update) with one to five unique version IDs. Percentages can use up to two decimal places and must total `100`.

Each successful `PUT` creates an immutable allocation revision. New calls use the latest revision. To inspect it, call [`GET /agents/{agent_id}/traffic-allocation`](https://docs.cartesia.ai/api-reference/agents/traffic-allocations/get-current).

```bash theme={null}
curl "https://api.cartesia.ai/agents/$AGENT_ID/traffic-allocation" \
  -H "X-API-Key: $CARTESIA_API_KEY" \
  -H "Cartesia-Version: 2026-08-14"
```

## Choose how phone calls are assigned

Set `assignment_strategy` based on how you want phone calls to reach the versions in your traffic split:

* `sticky_by_caller` keeps an external phone number on the same version while the traffic split stays the same. Cartesia uses the caller number for inbound calls and the destination number for outbound calls. This is the default.
* `random_per_call` assigns each new phone call separately. Calls from the same phone number can reach different versions, which helps when you test with only a few phone numbers.

Web calls that use traffic allocation are always assigned per call, so `assignment_strategy` does not affect them.

When you create an allocation, omitting `assignment_strategy` uses `sticky_by_caller`. When you replace an active allocation, omitting the field preserves its current strategy. Set the field explicitly when you want to change strategies.

## Supply variables for every version

Cartesia validates dynamic variables after it selects a version for a web session or batch recipient. If the selected version's initial message needs a missing value, the web session ends or the batch recipient fails with `error_code: missing_dynamic_variables`.

Supply the variables required by every version in the allocation. See [Dynamic variables](https://docs.cartesia.ai/agents/dynamic-variables.md) for request examples.

## Move a change from staging to production

1. Use **Preview** to test the draft without changing live calls.
2. Before publishing the candidate, set the current production version to `100` percent in the traffic allocation.
3. Publish the candidate, then update the allocation to send it a small percentage of live calls.
4. Compare the results. Move the candidate to `100` percent if it wins, or return the production version to `100` percent.

## Compare results

[`GET /agents/calls`](https://docs.cartesia.ai/api-reference/agents/calls/list-calls) returns the selected `agent_version_id` and the `traffic_allocation_id` used for each call. Group calls by `agent_version_id`, then use each call's ID to compare its [metric results](https://docs.cartesia.ai/line/evaluations/results.md).

```json theme={null}
{
  "id": "ac_123",
  "agent_id": "agent_123",
  "agent_version_id": "av_variant",
  "traffic_allocation_id": "ta_456",
  "status": "completed"
}
```

The allocation ID identifies the exact immutable revision. Use [`GET /agents/{agent_id}/traffic-allocations`](https://docs.cartesia.ai/api-reference/agents/traffic-allocations/list) to retrieve the targets and percentages for earlier revisions.

## Change or end a test

Send another `PUT` request to change the percentages. To keep the winning version in use as you publish new versions, set its percentage to `100` and leave the allocation active. New versions receive no traffic until you update or clear the allocation.

To return to the default routing behavior, call [`DELETE /agents/{agent_id}/traffic-allocation`](https://docs.cartesia.ai/api-reference/agents/traffic-allocations/clear). New calls then use the latest published version, including any versions that you publish later.

If the winning version is older, you can make it the published default before clearing the allocation. [Get that version](https://docs.cartesia.ai/api-reference/agents/versions/get.md) and pass its `config` to [Update Agent](https://docs.cartesia.ai/api-reference/agents/update.md). The update creates a new published version.

Clearing an allocation does not delete its history. It records an empty revision, and the current-allocation endpoint returns `404` until you create another allocation.

## What to test

Versions capture the agent's instructions, model, voice, language, and other versioned configuration. Change one behavior at a time when you need to attribute a result to a specific change.

Versions store references to shared resources such as tools. Editing a shared tool can affect every version that references it. To test tool behavior in isolation, create a separate tool and attach it to the variant.
