# Control Templates

Source: https://docs.formance.com/v3.2/modules/reconciliation/controls

Stack: v3.2

Control templates describe the relationship Reconciliation should protect. Choose the template closest to the business question, then configure its sources and per-asset tolerance.

| Template               | Business question                                                        | Sources                                                                                                                         |
| ---------------------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
| `ledger_vs_pool_drift` | Does cash held externally back the position recorded in Ledger?          | One Ledger account set and one cash pool                                                                                        |
| `ledger_invariant`     | Do several Ledger account groups maintain the required net relationship? | N Ledger account sets as signed terms (at least one, with no upper limit; meaningful net relationships usually use two or more) |
| `account_threshold`    | Is a Ledger account group within its allowed balance range?              | One Ledger account set                                                                                                          |
| `source_parity`        | Do two independent balance sources agree?                                | Any two Ledger account sets or cash pools                                                                                       |

All four templates evaluate one outcome per asset. If two assets fail, Reconciliation opens two alerts so they can be handled independently.

## Template catalog

Ledger vs pool

Use `ledger_vs_pool_drift` for the classic backing check between a dynamic Ledger account set and a [cash pool](https://docs.formance.com/v3.2/modules/payments/cash-pools).

```json
{
  "templateKind": "ledger_vs_pool_drift",
  "templateSpec": {
    "ledger": "main",
    "ledgerQuery": {
      "$match": { "metadata[reconciliation.pool]": "stripe" }
    },
    "paymentsPoolID": "<POOL_ID>",
    "ledgerSign": -1,
    "tolerance": {
      "USD/2": 0,
      "EUR/2": 50
    }
  }
}
```

The control checks this relationship for every asset found on either side:

```text
absolute(ledgerSign × ledger balance + pool balance) ≤ tolerance
```

Choose the sign from the way your Ledger represents the external position:

- use `-1` when the Ledger and pool balances are both naturally positive;
- use `+1`, the default, when the Ledger balance is the negative counterpart of positive external cash.

Tolerance defaults to zero for assets not listed. This means a newly observed asset is still checked strictly instead of being silently ignored.

When migrating a legacy policy, omit `ledgerSign` to preserve its `ledger + pool = 0` convention. Review the first evaluation's raw and signed Ledger values before scheduling the rule.

Ledger invariant

Use `ledger_invariant` when signed groups of accounts inside Ledger must preserve a financial identity. Typical examples include customer assets versus obligations, safeguarded funds versus customer entitlements, or a control account versus its sub-ledger accounts.

```json
{
  "templateKind": "ledger_invariant",
  "templateSpec": {
    "terms": [
      {
        "ledger": "main",
        "query": { "$match": { "metadata[funds.role]": "held" } },
        "sign": 1
      },
      {
        "ledger": "main",
        "query": { "$match": { "metadata[funds.role]": "obligation" } },
        "sign": -1
      }
    ],
    "tolerance": {
      "USD/2": 0,
      "EUR/2": 0
    }
  }
}
```

Each term selects a Ledger account set and applies a sign of `+1` or `-1`. Reconciliation requires at least one term and does not impose an upper limit, although a meaningful net relationship normally uses two or more:

```text
absolute(Σ signᵢ × balanceᵢ) ≤ tolerance
```

A one-term invariant is valid, but it reduces to checking whether that signed balance is within tolerance and therefore overlaps with `account_threshold`.

For every asset listed in `tolerance`, Reconciliation checks the signed sum against that tolerance. Assets not listed are outside this rule's scope.

Use metadata queries when membership is expected to evolve. New accounts matching a term are included automatically at the next evaluation.

Account threshold

Use `account_threshold` to ensure the aggregate balance of an account set remains above a minimum, below a maximum, or inside a range.

```json
{
  "templateKind": "account_threshold",
  "templateSpec": {
    "ledger": "main",
    "query": {
      "$match": { "metadata[treasury.role]": "operating" }
    },
    "mode": "aggregate",
    "bounds": {
      "USD/2": { "min": 100000, "max": 5000000 },
      "EUR/2": { "min": 50000 }
    }
  }
}
```

Each asset needs at least one bound. If both are present, `min` must be less than or equal to `max`.

`mode` accepts one value, `aggregate`: Reconciliation sums the balances of every account the query matches, then checks that total against the bounds. Creating a rule with any other `mode` returns a validation error. Each evaluation produces one result per asset, so its size and evidence stay the same however many accounts the query matches.

Because balances are summed, a breach shows that the account set is out of bounds, not which account caused it. To watch an individual account, create a rule whose query selects only that address.

Source parity

Use `source_parity` when two independently maintained balance sources should agree. Either side can be:

- a Ledger account set, identified by a Ledger and query;
- a Payments cash pool, identified by its pool ID.

```json
{
  "templateKind": "source_parity",
  "templateSpec": {
    "left": {
      "kind": "ledger",
      "ledger": "main",
      "query": { "$match": { "address": "control:stripe" } }
    },
    "right": {
      "kind": "payments_pool",
      "poolID": "<POOL_ID>"
    },
    "scope": "aggregate",
    "tolerance": {
      "USD/2": 0,
      "EUR/2": 50
    }
  }
}
```

The template checks `absolute(left - right) ≤ tolerance` for the union of assets observed on both sides. A missing asset is treated as zero, ensuring that a one-sided balance is detected.

`scope` accepts one value, `aggregate`: each side is summed per asset before the two totals are compared. Creating a rule with any other `scope` returns a validation error. Both Ledger-to-Ledger and pool-to-pool comparisons are supported; each source is still read independently.

A mismatch shows that the two sides disagree on an asset, not which account holds the difference. To narrow it down, create rules whose Ledger queries each select a single account.

## Set tolerances deliberately

Tolerances use integer asset units and must be non-negative. They are useful when systems observe the same economic event at slightly different times or when a known rounding convention creates small residuals.

Prefer the smallest tolerance justified by the business process:

1. identify the expected timing or rounding difference;
2. express the maximum accepted amount per asset;
3. keep evidence and alerting enabled above that amount;
4. review tolerances when settlement behavior changes.

A tolerance prevents a difference from opening an alert. It is not the same as [accepting an alert](https://docs.formance.com/v3.2/modules/reconciliation/alerts#accept-a-known-discrepancy), which records a human decision after evidence exists.

## Align sources with different timestamps

Multi-source templates support an independent point in time for every source. First evaluate the rule once and inspect `pitPerSource` to discover its stable keys, such as:

```json
{
  "ledger:main#0": "2026-07-20T23:59:59Z",
  "pool:01J...#0": "2026-07-20T23:30:00Z"
}
```

You can then replay or align the sources explicitly:

```json
{
  "at": "2026-07-20T23:59:59Z",
  "sourcePITs": {
    "ledger:main#0": "2026-07-20T23:59:59Z",
    "pool:01J...#0": "2026-07-20T23:30:00Z"
  }
}
```

Values in `sourcePITs` are already effective timestamps, so the evaluation's safety margin is not subtracted from them. Unknown source keys and future timestamps are rejected.

## Choose the account sets carefully

A control is only as meaningful as its source selection. Before enabling a schedule:

- run the underlying Ledger and cash-pool queries independently;
- confirm that every intended account is included and no unrelated account matches;
- verify sign conventions with a known-good balance;
- test each configured asset;
- run a known failing scenario and inspect its evidence.

Ledger metadata queries at a historical point in time require Ledger 2.4.11 or later, which is the minimum supported by this Reconciliation release.
