# Set sub-agent limits

Set once for your account the cap your agents hand their sub-agents, how they share it, which merchants they may buy from and when a person is asked, from the API or from a rules component in your own admin.

Use this when your agents hand grants to sub-agents and you want one set of limits for all of them: the most a parent hands on in all, how sub-agents share it, which kinds of merchant and which merchants they may buy from, and what happens past a cap. Reject first, then ask a person.

**Before you begin:** your agents delegate (see [delegate to subagents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md)).

## 1. Save your limits

[`PUT /v1/mandate/settings/limits`](https://provenanceauth.com/docs/api/grants.md#put-v1-mandate-settings-limits) saves the limits as a new live version. Export the sandbox account key first:

**curl**

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

curl -s -X PUT $PROVENANCE_API/v1/mandate/settings/limits \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "document": {
      "cap": { "enabled": true, "amount": 2400, "currency": "USD" },
      "budget": { "allocation": "shared", "at_limit": "continue" },
      "allowed_mcc": { "enabled": true, "action": "refuse", "mcc": [{ "group": "airlines" }, { "group": "lodging" }, { "group": "car_rental" }] },
      "named_merchants": { "enabled": false, "action": "refuse", "merchants": [] },
      "blocked_mcc": { "enabled": false, "mcc": [] },
      "blocked_merchants": { "enabled": false, "merchants": [] },
      "refuse_over_cap": { "enabled": true, "unit": "percent", "value": 20 },
      "ask_over_cap": { "enabled": true },
      "refuse_over_share": { "enabled": false, "unit": "percent", "value": 20 }
    }
  }'
```

**Node**

```ts
const current = await prov.mandate.grants.limits();
await prov.mandate.grants.setLimits({ ...current.document, cap: { enabled: true, amount: 2400, currency: "USD" } });
```

**Python**

```python
current = prov.mandate.grants.limits()
prov.mandate.grants.set_limits({**current["document"], "cap": {"enabled": True, "amount": 2400, "currency": "USD"}})
```

| Row | What it is | When it's on |
| --- | --- | --- |
| `cap` | The most a parent hands its sub-agents in all | Each delegation's budget, with the parent's live children's, stays within it |
| `budget` | `allocation`: `shared` (Flexible share) or `reserved` (Reserved); `at_limit`: `stop`, `ask` or `continue` (Keep going) | The [budget settings](https://provenanceauth.com/docs/mandate/concepts/grants.md#budgets) each sub-agent gets when its delegation doesn't say |
| `allowed_mcc` | Kinds of merchant, by group or MCC range | Any other kind is refused (or asked, `action: "ask"`) |
| `named_merchants` | Merchants by ID or origin | Any other merchant is refused (or asked) |
| `blocked_mcc`, `blocked_merchants` | Kinds of merchant and merchants | Always refused |
| `refuse_over_cap` | A margin past a budget: a percentage (20% by default) or a fixed amount | Past it, refused without asking |
| `ask_over_cap` | Asking a person | Below the margin, a person is asked; off, refused at the budget |
| `refuse_over_share` | With `at_limit: "ask"`, a margin past a sub-agent's own limit | Past it, refused without asking; off, a person is asked at any amount over |

Until you save a version, [`GET /v1/mandate/settings/limits`](https://provenanceauth.com/docs/api/grants.md#get-v1-mandate-settings-limits) answers the built-in limits (`is_default: true`), which apply nothing.

## 2. Know what a published version does

| What | How |
| --- | --- |
| Your account's over-limit settings | The cap rows (and the merchant rows, when on) become your account's settings for those locks, as [`PUT /v1/mandate/settings/delegation`](https://provenanceauth.com/docs/api/grants.md#put-v1-mandate-settings-delegation) would. A grant's own settings still come first |
| Each new delegation | [`POST /v1/mandate/grants/{id}/delegate`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants-id-delegate) and a delegation proposal hand each sub-agent the budget settings (its allocation, and `stop` or `ask` as its own budget setting), the allow lists (when its parent has none) and the block lists, wherever the request leaves them out. What the request states wins |
| The overall cap | A delegation that would take the sub-agents' budgets past the cap is refused, `400 mandate_limits_cap_exceeded`, with what's left in `error.details.available` |
| The record | Every publish writes an audit row and sends `mandate.policy.published`; the delegation's ledger row records `mandate_limits_version` |

A running grant keeps what it was handed: a later publish changes the next delegation, not the last one.

## 3. Let your team change them in your admin

Mint a `rules` session with `object_id: "mandate_limits"` and drop `<provenance-rules>` into your admin. Your team sees "Mandate rules", changes the cap or the margin within your bounds, saves a draft and publishes. See [the rules component](https://provenanceauth.com/docs/embeds/rules.md#mandate-limits) for the scope and `loadValues`. A draft waits until someone publishes it: [`POST /v1/mandate/settings/limits/versions/{version}/publish`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-settings-limits-versions-version-publish), a `publish` session, or the console.

## 4. Try a draft in sandbox

A sandbox session minted with `rules.draft_group` saves its drafts in that group. Add the draft's version and its group (`"limits_version": 7, "draft_group": "visitor-4f2a"`) to the body of a delegation or a proposal to apply that draft instead of the live limits.

In live, `limits_version` is `400`. A draft of another group is `400` too, so one visitor's call can't use another's.

## Errors you might see

| Error | Why |
| --- | --- |
| `policy_invalid` | A list row is on with no list, an unknown key, or a percentage over 1000 |
| `mandate_limits_cap_exceeded` | The sub-agents' budgets together would pass the cap, or a sub-agent under a cap has no budget |
| `rules_embed_out_of_scope` | A session changed a row or a value its scope doesn't allow |

## See also

- [Delegate to subagents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md)
- [The rules component](https://provenanceauth.com/docs/embeds/rules.md)
- [Grants](https://provenanceauth.com/docs/mandate/concepts/grants.md)
