# Set limits and policy

Limit what your agent can buy and from whom, send larger amounts to a person, give it a grant with a budget and an end date, narrow a sub-agent's grant, and simulate the policy before you activate it.

Use this guide to put one agent's spending under rules: who it may buy from, the amount a person must approve, a monthly budget with an end date, and a narrower grant for a sub-agent. You simulate the policy before it decides anything real.

**Before you begin:** the [Mandate quickstart](https://provenanceauth.com/docs/mandate/quickstart.md) shows one rule end to end. [Policies](https://provenanceauth.com/docs/concepts/policies.md) and [grants](https://provenanceauth.com/docs/mandate/concepts/grants.md) explain the two objects this guide combines. Lists are available on every plan.

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

| Limit | Where it lives | Checked when |
| --- | --- | --- |
| Who the agent may buy from | A policy list and a rule | Every decision |
| The amount a person approves | A policy rule | Every decision |
| Per-order cap, budget and end date | A grant | Every decision under the grant |
| What a sub-agent may do | A delegated grant, never wider than its parent | Every decision under the sub-grant |

## 1. List the stores the agent may use

A policy list holds values a rule can test with `in_list` or `not_in_list`. Save the stores your agent buys from with [`PATCH /v1/account/policies/lists/{name}`](https://provenanceauth.com/docs/api/policy-builder.md#patch-v1-account-policies-lists-name).

**curl**

```bash
curl -s -X PATCH $PROVENANCE_API/v1/account/policies/lists/approved-suppliers \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "kind": "allow", "entries": ["brindlecote-office.example", "paper.example"] }'
```

**Node**

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

const prov = new Provenance({ apiKey: process.env.PROVENANCE_KEY!, baseUrl: process.env.PROVENANCE_API });
await fetch(`${process.env.PROVENANCE_API}/v1/account/policies/lists/approved-suppliers`, {
  method: "PATCH",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ kind: "allow", entries: ["brindlecote-office.example", "paper.example"] }),
});
```

**Python**

```python
import os, httpx
from provenanceauth import Provenance

prov = Provenance(os.environ["PROVENANCE_KEY"], base_url=os.environ["PROVENANCE_API"])
httpx.patch(f"{os.environ['PROVENANCE_API']}/v1/account/policies/lists/approved-suppliers",
          headers={"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"},
          json={"kind": "allow", "entries": ["brindlecote-office.example", "paper.example"]})
```

```json
{
  "object": "policy_list",
  "name": "approved-suppliers",
  "kind": "allow",
  "count": 2
}
```

## 2. Simulate the policy

Write the rules, then try them against facts with [`POST /v1/mandate/policies/simulate`](https://provenanceauth.com/docs/api/policies.md#post-v1-mandate-policies-simulate) before anything depends on them. This document refuses a store that isn't on the list and sends anything over 250 to a person. Simulating decides nothing and records nothing.

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/mandate/policies/simulate \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "document": { "rules": [
      { "id": "known-suppliers-only", "when": [{ "fact": "counterparty", "op": "not_in_list", "value": "approved-suppliers" }], "require": { "deny": true }, "reason": "counterparty_not_approved" },
      { "id": "ask-above-250", "when": [{ "fact": "transaction_amount", "op": "gt", "value": 250 }], "require": { "approval": true } }
    ] },
    "facts": { "counterparty": "brindlecote-office.example", "transaction_amount": 400 }
  }'
```

**Node**

```ts
const rules = {
  rules: [
    { id: "known-suppliers-only", when: [{ fact: "counterparty", op: "not_in_list", value: "approved-suppliers" }], require: { deny: true }, reason: "counterparty_not_approved" },
    { id: "ask-above-250", when: [{ fact: "transaction_amount", op: "gt", value: 250 }], require: { approval: true } },
  ],
};
const sim = await prov.mandate.policies.simulate({ document: rules, facts: { counterparty: "brindlecote-office.example", transaction_amount: 400 } });
```

**Python**

```python
rules = {"rules": [
    {"id": "known-suppliers-only", "when": [{"fact": "counterparty", "op": "not_in_list", "value": "approved-suppliers"}], "require": {"deny": True}, "reason": "counterparty_not_approved"},
    {"id": "ask-above-250", "when": [{"fact": "transaction_amount", "op": "gt", "value": 250}], "require": {"approval": True}},
]}
sim = prov.mandate.policies.simulate(document=rules, facts={"counterparty": "brindlecote-office.example", "transaction_amount": 400})
```

```json
{
  "object": "policy_simulation",
  "allowed": false,
  "reason": "approval_required",
  "requires_approval": true,
  "policy": {
    "id": "pol_3F9A1C7E52B04D8E6A1F27C9B4",
    "version": 0,
    "rule": {
      "id": "ask-above-250",
      "name": null,
      "phase": "assess",
      "action": "challenge",
      "conditions": [
        { "fact": "transaction_amount", "op": "gt", "value": 250, "actual": 400 }
      ]
    }
  }
}
```

Try the other cases the same way:

| Facts | Result |
| --- | --- |
| A listed store, 120 | `allowed: true` |
| A listed store, 400 | `approval_required` from `ask-above-250` |
| A store not on the list, any amount | `counterparty_not_approved` from `known-suppliers-only` |

## 3. Activate the policy

Save the same document with [`PUT /v1/mandate/policies`](https://provenanceauth.com/docs/api/policies.md#put-v1-mandate-policies). It becomes the account's active version, and every decision from now on records which version decided it.

**curl**

```bash
curl -s -X PUT $PROVENANCE_API/v1/mandate/policies \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "note": "known suppliers, a person above 250",
    "document": { "rules": [
      { "id": "known-suppliers-only", "when": [{ "fact": "counterparty", "op": "not_in_list", "value": "approved-suppliers" }], "require": { "deny": true }, "reason": "counterparty_not_approved" },
      { "id": "ask-above-250", "when": [{ "fact": "transaction_amount", "op": "gt", "value": 250 }], "require": { "approval": true } }
    ] }
  }'
```

**Node**

```ts
await prov.mandate.policies.set(rules, { note: "known suppliers, a person above 250" });
```

**Python**

```python
prov.mandate.policies.set(rules, note="known suppliers, a person above 250")
```

```json
{
  "object": "policy_version",
  "domain": "intent",
  "active": true,
  "note": "known suppliers, a person above 250"
}
```

Your agent sends `counterparty` and `transaction_amount` in the `context` of each decision, as in the [quickstart](https://provenanceauth.com/docs/mandate/quickstart.md).

An action no rule decides is allowed. To ask a person about it instead, or refuse it, set the policy's fallback row: `"fallback": { "assess": "review" }` (or `"reject"`) beside `rules`. The row always runs last and can't be deleted; the decision names it `rul_default_fallback_assess`. The [rules embed](https://provenanceauth.com/docs/embeds/rules.md#full-control) shows it with its outcome as a choice.

The policy and an agent's grant are two layers: the grant's limits only narrow what the policy allows, and every decision checks both.

## 4. Give the agent a grant with a budget

A [grant](https://provenanceauth.com/docs/mandate/concepts/grants.md) is standing permission: the agent acts within it without asking each time. Create one with [`POST /v1/mandate/grants`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants): a cap per order, a monthly budget, the actions and stores it covers, and an end date.

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/mandate/grants \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "device_id": "dev_00000000000000000000000000000001",
    "agent_session_id": "sess_office_buyer",
    "purpose": "Keep the office stocked with paper and toner",
    "constraints": {
      "max_amount": 150,
      "budget_amount": 600,
      "budget_window": "month",
      "allowed_action_types": ["quote", "purchase"],
      "allowed_counterparties": ["brindlecote-office.example", "paper.example"],
      "expires_at": "2027-06-30T23:59:59Z"
    }
  }' | tee grant.json
```

**Node**

```ts
const grant = await prov.mandate.grants.create({
  deviceId: "dev_00000000000000000000000000000001",
  agentSessionId: "sess_office_buyer",
  purpose: "Keep the office stocked with paper and toner",
  constraints: {
    maxAmount: 150,
    budgetAmount: 600,
    budgetWindow: "month",
    allowedActionTypes: ["quote", "purchase"],
    allowedCounterparties: ["brindlecote-office.example", "paper.example"],
    expiresAt: "2027-06-30T23:59:59Z",
  },
});
```

**Python**

```python
grant = prov.mandate.grants.create(
    device_id="dev_00000000000000000000000000000001",
    agent_session_id="sess_office_buyer",
    purpose="Keep the office stocked with paper and toner",
    constraints={
        "max_amount": 150,
        "budget_amount": 600,
        "budget_window": "month",
        "allowed_action_types": ["quote", "purchase"],
        "allowed_counterparties": ["brindlecote-office.example", "paper.example"],
        "expires_at": "2027-06-30T23:59:59Z",
    },
)
```

```json
{
  "object": "grant",
  "status": "active",
  "remaining_budget": 600,
  "purpose": "Keep the office stocked with paper and toner"
}
```

The agent sends `mandate_token` with each intent. A purchase over 150, past the month's budget, at another store or after the end date is refused under the grant; the policy's rules still apply on top.

```bash
export GRANT_ID=$(jq -r .id grant.json)
```

## 5. Narrow a sub-agent's grant

When the agent hands a task to a sub-agent, delegate a grant that can only be narrower: [`POST /v1/mandate/grants/{id}/delegate`](https://provenanceauth.com/docs/api/grants.md#post-v1-mandate-grants-id-delegate). This sub-agent only compares prices, with a smaller cap and budget. Stores and the end date are inherited when you leave them out.

**curl**

```bash
curl -s -X POST $PROVENANCE_API/v1/mandate/grants/$GRANT_ID/delegate \
  -H "Authorization: Bearer $PROVENANCE_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_session_id": "sess_price_checker",
    "purpose": "Compare toner prices at two stores",
    "purpose_tags": ["quote"],
    "constraints": { "max_amount": 40, "budget_amount": 60, "allowed_action_types": ["quote"] }
  }'
```

**Node**

```ts
const sub = await prov.mandate.grants.delegate(grant.id, {
  agentSessionId: "sess_price_checker",
  purpose: "Compare toner prices at two stores",
  purposeTags: ["quote"],
  constraints: { maxAmount: 40, budgetAmount: 60, allowedActionTypes: ["quote"] },
});
```

**Python**

```python
sub = prov.mandate.grants.delegate(
    grant["id"],
    agent_session_id="sess_price_checker",
    purpose="Compare toner prices at two stores",
    purpose_tags=["quote"],
    constraints={"max_amount": 40, "budget_amount": 60, "allowed_action_types": ["quote"]},
)
```

```json
{
  "object": "grant",
  "status": "active",
  "depth": 1,
  "remaining_budget": 60,
  "purpose_tags": ["quote"]
}
```

`chain` lists the grants from the root to this one, so a store can see who delegated what. A delegation wider than its parent is refused; see [delegate to sub-agents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md).

## 6. Put the sandbox account back

The sandbox account is shared. When you're done, return it to the default policy with [`DELETE /v1/mandate/policies`](https://provenanceauth.com/docs/api/policies.md#delete-v1-mandate-policies).

**curl**

```bash
curl -s -X DELETE $PROVENANCE_API/v1/mandate/policies \
  -H "Authorization: Bearer $PROVENANCE_KEY"
```

**Node**

```ts
await prov.mandate.policies.reset();
```

**Python**

```python
prov.mandate.policies.reset()
```

```json
{
  "object": "policy",
  "is_default": true
}
```

## Errors you might see

| Error | Cause | Fix |
| --- | --- | --- |
| `400` [`parameter_invalid`](https://provenanceauth.com/docs/platform/errors.md#parameter_invalid) on a grant | A cap or budget of 0, or a sub-grant wider than its parent | Send positive amounts no wider than the parent grant. |
| `400` [`policy_invalid`](https://provenanceauth.com/docs/platform/errors.md#policy_invalid) | A rule names an unknown fact, operator or list | Simulate first: it reports the same problem without saving. |

## See also

- [Build a policy](https://provenanceauth.com/docs/mandate/guides/policy-builder.md)
- [Policies](https://provenanceauth.com/docs/concepts/policies.md)
- [Grants](https://provenanceauth.com/docs/mandate/concepts/grants.md)
- [Automate approvals](https://provenanceauth.com/docs/mandate/guides/automate-approvals.md)
- [Delegate to sub-agents](https://provenanceauth.com/docs/mandate/guides/give-subagents-their-own-limits.md)
