# Canary baselines

A baseline learns what canary readings are normal for one agent or account on one model, so a policy or a Trace rule can trip on what is unusual for that agent.

A canary baseline is a learned distribution of one family's readings for an agent or account on one model, that lets you trip on what is unusual for that agent instead of on one fixed threshold.

## When to use it

Use a baseline when a fixed threshold doesn't fit every agent. A fraud-review agent reads `deception` all day, while a support agent never should; a baseline per agent lets each trip on its own outliers.

## How it works

1. You start learning a baseline for a model, optionally for one agent.
2. Every [traced inference](https://provenanceauth.com/docs/trace/guides/traced-inference.md) and [sidecar reading](https://provenanceauth.com/docs/self-host/sidecar.md) in its scope updates a running mean, variance and a 100-bin histogram per family. Readings that already tripped are left out and counted in `excluded`, so an attack during learning doesn't become normal.
3. When `n` reaches `min_samples`, the baseline is `ready` and frozen. A policy or a Trace rule then compares each new reading with it as a z-score or a percentile.

1. **Readings**: traced inferences, sidecar
2. **Baseline**: mean, sigma, histogram
3. **Relative check**: z-score or percentile

## Key fields

- `scope` (string): `agent` for one registered agent, or `account` for every reading on the account.
- `status` (string): `learning`, `ready`, `stale` (the agent moved to another model, or the baseline is over 30 days old), or `superseded` by a newer version.
- `n` (integer): Readings learned so far. The baseline is ready at `min_samples` (default 200).
- `mean` (number): Mean reading.
- `sigma` (number): Sample standard deviation. Relative scores use at least `sigma_floor` (0.01), so a very steady agent doesn't trip on noise.
- `histogram` (integer[]): Reading counts in 100 bins over 0 to 1, used for percentiles.

For a reading `score`, the z-score is `(score − mean) / max(sigma, 0.01)`. The percentile is the share of readings below the score's bin plus half of those in it.

## Start learning

Create a baseline with [`POST /v1/trace/baselines`](https://provenanceauth.com/docs/api/canary-baselines.md#post-v1-trace-baselines). Leave out `agent_id` for an account-wide baseline, and `families` for every family on the model.

**curl**

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

curl -s $PROVENANCE_API/v1/trace/baselines \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "model_version": "qwen2.5-0.5b-instruct@7ae55760", "families": ["prompt_injection"], "min_samples": 20 }' | tee baselines.json
```

**Node**

```ts
import { Provenance } from "@provenanceauth/sdk";

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });
const [baseline] = await prov.trace.baselines.create({ modelVersion: "qwen2.5-0.5b-instruct@7ae55760", families: ["prompt_injection"], minSamples: 20 });
```

**Python**

```python
import os
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])
baseline = prov.trace.baselines.create(model_version="qwen2.5-0.5b-instruct@7ae55760", families=["prompt_injection"], min_samples=20)[0]
```

```json
{
  "object": "list",
  "data": [
    {
      "id": "cbl_01M5Q0FNNNMQT3Z2WXDTG9EHH1",
      "object": "trace_canary_baseline",
      "agent_id": null,
      "scope": "account",
      "model_version": "qwen2.5-0.5b-instruct@7ae55760",
      "family": "prompt_injection",
      "version": 1,
      "status": "learning",
      "min_samples": 20,
      "n": 0,
      "excluded": 0,
      "mean": 0,
      "sigma": 0,
      "sigma_floor": 0.01,
      "histogram": [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      "started_at": "2026-10-24T13:09:29.909Z",
      "ready_at": null,
      "stale_at": null,
      "updated_at": "2026-10-24T13:09:29.909Z"
    }
  ],
  "has_more": false,
  "next_cursor": null
}
```

To refresh a ready baseline, re-learn it with [`POST /v1/trace/baselines/{id}/relearn`](https://provenanceauth.com/docs/api/canary-baselines.md#post-v1-trace-baselines-id-relearn): the new version learns while the current one keeps serving. To remove one, delete it:

**curl**

```bash
export BASELINE_ID=$(jq -r '.data[0].id' baselines.json)
curl -X DELETE $PROVENANCE_API/v1/trace/baselines/$BASELINE_ID \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
await prov.trace.baselines.delete(baseline.id);
```

**Python**

```python
prov.trace.baselines.delete(baseline["id"])
```

```json
{ "id": "cbl_01M5Q0FNNNMQT3Z2WXDTG9EHH1", "object": "trace_canary_baseline", "deleted": true }
```

## Use it in a policy or a Trace rule

In a [canary policy](https://provenanceauth.com/docs/trace/guides/canary-policies.md), use `op: "z_gt"` (0 to 10) or `op: "pct_gt"` (50 to 99.9). `baseline: "agent"`, the default, uses the agent's own baseline and falls back to the account's.

```json
{
  "id": "unusual-injection",
  "when": [{ "fact": "canary", "set": "prompt_injection", "op": "z_gt", "value": 3 }],
  "require": { "deny": true }
}
```

The same condition works in a [Trace rule on model output](https://provenanceauth.com/docs/trace/guides/trace-rules.md#on-model-output) (`"phase": "completion"`), where it is also the family's trip threshold; a request can send a relative `trip_thresholds` too: `{ "prompt_injection": { "z": 3 } }`. The response's `rules.baselines` names what each family was judged against.

With no ready baseline, a strict policy condition denies `canary_baseline_unavailable`, and a relative trip threshold leaves the reading at its published threshold and lists the family in `rules.baseline_unavailable`.

**Limitations:** a ready baseline is frozen; re-learn it after the agent's normal traffic changes. The number of baselines an account keeps depends on its plan.

## Related

- [Canary policies](https://provenanceauth.com/docs/trace/guides/canary-policies.md)
- [Trace rules](https://provenanceauth.com/docs/trace/guides/trace-rules.md)
- [Trace API reference](https://provenanceauth.com/docs/api/canary-baselines.md)
