# Backtest traffic rules

Replay your recorded traffic decisions through a draft traffic policy and see what it would have changed, including how many of the visits it would stop went on to place an order.

Before a traffic rule blocks or challenges anyone, replay it over the traffic you already recorded. The backtest runs the same evaluator as the hosted check and the middleware over your recorded decisions and tells you what would have changed: how many requests each rule would have decided, which decisions would have gone a different way, and how many of the visits it would have stopped went on to place an order.

**Before you begin:** a traffic policy draft, recorded traffic (hosted checks, or middleware and edge reports), and a key on the Pro plan or above.

```bash
export PROVENANCE_API=https://sandbox-api.provenanceauth.com
export PROVENANCE_KEY=prv_test_ent_E7nT2vQ9bW4mK8sYd3cFjLh6gAe1pR5x
```

## 1. Record a request

The backtest replays decisions you already recorded. On a new account, send one hosted check first, here a script calling your checkout:

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/identify/assessments \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{"request":{"method":"POST","url":"https://harborbean.example/checkout","headers":{"user-agent":"python-requests/2.32.3"}}}'
```

**Node**

```ts
const check = await (await fetch(`${process.env.PROVENANCE_API}/v1/identify/assessments`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    request: { method: "POST", url: "https://harborbean.example/checkout", headers: { "user-agent": "python-requests/2.32.3" } },
  }),
})).json();
```

**Python**

```python
import os, httpx

check = httpx.post(
    f"{os.environ['PROVENANCE_API']}/v1/identify/assessments",
    headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"},
    json={"request": {"method": "POST", "url": "https://harborbean.example/checkout",
                      "headers": {"user-agent": "python-requests/2.32.3"}}},
).json()
```

```json
{
  "object": "traffic_assessment",
  "class": "declared_bot",
  "decision": { "action": "allow", "shadow": null },
  "action": "checkout",
  "source": "hosted"
}
```

With no active policy the check allows the request; the draft below would have challenged it.

## 2. Replay a draft

Send the draft as `document`; leave it out to replay the active policy. `days` is 1 to 30 (30 by default).

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/identify/policies/backtest \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{"document":{"rules":[{"id":"challenge-bots","label":"Challenge bots at checkout","when":[{"fact":"agent.class","op":"in","value":["suspected_bot","declared_bot"]}],"then":{"action":"challenge"}}],"default":"allow"},"days":30}'
```

**Node**

```ts
const backtest = await (await fetch(`${process.env.PROVENANCE_API}/v1/identify/policies/backtest`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    document: {
      rules: [
        {
          id: "challenge-bots",
          label: "Challenge bots at checkout",
          when: [{ fact: "agent.class", op: "in", value: ["suspected_bot", "declared_bot"] }],
          then: { action: "challenge" },
        },
      ],
      default: "allow",
    },
    days: 30,
  }),
})).json();
```

**Python**

```python
import os, httpx

backtest = httpx.post(
    f"{os.environ['PROVENANCE_API']}/v1/identify/policies/backtest",
    headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"},
    json={
        "document": {
            "rules": [{"id": "challenge-bots", "label": "Challenge bots at checkout",
                       "when": [{"fact": "agent.class", "op": "in", "value": ["suspected_bot", "declared_bot"]}],
                       "then": {"action": "challenge"}}],
            "default": "allow",
        },
        "days": 30,
    },
).json()
```

```json
{
  "object": "policy_backtest",
  "available": true,
  "days": 30,
  "requests": 1,
  "truncated": false,
  "totals": { "allowed": 0, "challenged": 1, "rejected": 0 },
  "rules": [
    { "rule_id": "challenge-bots", "label": "Challenge bots at checkout", "outcome": "ask", "action": "challenge", "matched": 1, "decided": 1, "per_week": 0.2, "summary": "Last 30 days: would have challenged 1 request, about 1 a month" }
  ],
  "traffic": {
    "actions": { "allow": 0, "log_only": 0, "rate_limit": 0, "challenge": 1, "require_intent": 0, "reject": 0 },
    "changed": { "allow→challenge": 1 },
    "by_class": { "declared_bot": { "challenge": 1 } },
    "reached_checkout": { "stopped": 1, "visits": 0, "flagged": 0 },
    "unscored": 0,
    "unscored_facts": [],
    "sources": { "hosted": 1, "middleware": 0, "edge": 0 },
    "lists_as_of": "now"
  }
}
```

## 3. Read the result

| Field | What it says |
| --- | --- |
| `totals` | What the draft would have done: `allowed` (allow and log only), `challenged` (challenge and require an intent), `rejected`. Rate limits count in `traffic.actions` only. |
| `rules[]` | Per rule: how many requests its conditions match (`matched`), how many it would have decided (`decided`), and a one-line `summary`. |
| `traffic.changed` | Decisions whose action would change from the recorded one, keyed `from→to`. An empty object means the draft decides as your live policy did. |
| `traffic.by_class` | The draft's actions per visitor class. |
| `traffic.reached_checkout` | Of the visits the draft would block or challenge that your live policy allowed (`stopped`), how many a checkout assessment or a store order named (`visits`), and how many of the orders those visits placed were disputed or flagged (`flagged`): a chargeback or fraud, a refund abuse, a cart approval the customer denied, or an order your store rejected ([order outcomes](https://provenanceauth.com/docs/authorize/guides/rule-suggestions.md#1-report-what-happened-to-orders)). |
| `traffic.unscored`, `unscored_facts` | Decisions the backtest couldn't replay, and the facts behind them (below). |
| `traffic.sources` | Where the replayed decisions came from. Middleware and edge reports are what your integration sent, and may be sampled. |

Check `reached_checkout` before a rule blocks: a rule that would have stopped visits that went on to place orders stops customers too.

## What a backtest can't replay

A decision keeps the facts a rule reads, with these exceptions:

- `intent_valid` and `biometric_bound` are kept on decisions recorded since November 2026. An older decision that carried a person's approval can't be scored on them; a decision without a person reads both as false.
- `protocol_kind` is kept beside `protocol` on decisions recorded since then; a decision with no protocol reads it as empty.
- `protocol_payee` is never kept, because it names another business.

When a rule that could have decided a request names one of these facts and the decision didn't keep it, the decision is counted under `unscored`, never guessed. Middleware and edge reports send these facts from the next release of `@provenanceauth/identify`. `in_list` and `not_in_list` read each list as it is now (`lists_as_of: "now"`).

## Limits

- The window is 1 to 30 days, and never longer than your account's decision retention (**Settings → Identify**).
- The newest 20,000 decisions in the window are replayed; past that, `truncated` is true and the counts cover the newest ones.
- One backtest runs per account at a time, and 30 a day.

## Errors you might see

| Symptom | Cause | Fix |
| --- | --- | --- |
| `402` [`plan_required`](https://provenanceauth.com/docs/platform/errors.md#plan_required) | Backtests are on the Pro plan and above | Choose a plan in **Settings → Plan & usage**. |
| `409` [`backtest_in_progress`](https://provenanceauth.com/docs/platform/errors.md#backtest_in_progress) | A backtest for your account is still running | Send it again when the running one answers. |
| `429` [`rate_limit_exceeded`](https://provenanceauth.com/docs/platform/errors.md#rate_limit_exceeded) | 30 backtests today | Wait for `Retry-After`. |
| `400` [`policy_invalid`](https://provenanceauth.com/docs/platform/errors.md#policy_invalid) | The draft has an unknown fact, operator or action | `error.details` names the rule and field. |

## See also

- [Write a traffic policy](https://provenanceauth.com/docs/identify/guides/traffic-policy.md): simulate a single request, then roll out in shadow mode
- [Traffic rule suggestions](https://provenanceauth.com/docs/identify/guides/traffic-rule-suggestions.md): drafts built from your recorded traffic, each with its backtest
- [Traffic assessment](https://provenanceauth.com/docs/identify/concepts/traffic-assessment.md): the facts a rule reads
- [API reference](https://provenanceauth.com/docs/api.md): `POST /v1/identify/policies/backtest`
