# Pay per call with tabs

Open a tab once under a grant, send its token on every call, check each call offline against the signed revocation list, and post usage statements.

Use this when one agent calls another's paid API many times a day for small amounts. The buyer opens a tab once; it runs through authorize once and reserves its daily cap from the grant's budget. After that, each call carries the tab token and the seller checks it offline in a few milliseconds, with no call to Provenance per request. The seller bills on its own rail and posts signed usage statements; Provenance never holds the money.

**Before you begin:** the seller [sells to agents](https://provenanceauth.com/docs/authorize/guides/sell-to-agents.md) (a selling agent with an Identify key), and the buyer has a [grant](https://provenanceauth.com/docs/mandate/concepts/grants.md) for its agent.

## 1. Publish a price sheet (seller)

A price sheet is an offer with `delivery.kind: "service"` and per-unit prices, signed with your agent's key:

**Node**

```ts
import { signOffer } from "@provenanceauth/rails/deals";

const { offer: priceSheet } = await signOffer({
  iss: agentId,
  offer_id: "sheet_company_lookup_v3",
  items: [{ sku: "lookup", name: "Company lookup", quantity: 1, unit: "call", unit_price: 0.002 }],
  total: 0.002,
  currency: "USD",
  delivery: { kind: "service", deadline: "2026-12-01T00:00:00Z" },
  refund_terms: ["none"],
  seller_confirmation: { offered: "never", response_time: "1bd" },
}, privateJwk, { ttlSeconds: 7 * 24 * 3600 });
```

**Python**

```python
from provenanceauth_verify import sign_offer

price_sheet = sign_offer({
    "iss": agent_id, "offer_id": "sheet_company_lookup_v3",
    "items": [{"sku": "lookup", "name": "Company lookup", "quantity": 1, "unit": "call", "unit_price": 0.002}],
    "total": 0.002, "currency": "USD",
    "delivery": {"kind": "service", "deadline": "2026-12-01T00:00:00Z"},
    "refund_terms": ["none"],
    "seller_confirmation": {"offered": "never", "response_time": "1bd"},
}, private_jwk, ttl_seconds=7 * 24 * 3600)["offer"]
```

Hand it to buyers however you like: in a 402, on a pricing endpoint, or in your docs. A buyer must open its tab before the price sheet expires; the tab then keeps those prices for its life.

## 2. Open a tab (buyer)

**curl**

```bash norun
curl -s -X POST $PROVENANCE_API/v1/authorize/tabs \
  -H "Authorization: Bearer $PROVENANCE_KEY" -H "Content-Type: application/json" \
  -d '{ "price_sheet": "'"$PRICE_SHEET"'", "agent_session_id": "sess_scout_7", "grant_id": "'"$MANDATE_ID"'",
        "max_per_call": 0.01, "daily_cap": 40, "expires_at": "2026-11-14T00:00:00Z" }'
```

**Node**

```ts
const tab = await (await fetch(`${process.env.PROVENANCE_API}/v1/authorize/tabs`, {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({ price_sheet: priceSheet, agent_session_id: "sess_scout_7", grant_id: mandateId, max_per_call: 0.01, daily_cap: 40, expires_at: "2026-11-14T00:00:00Z" }),
})).json();
```

**Python**

```python
import os
import httpx

api, h = os.environ["PROVENANCE_API"], {"Authorization": f"Bearer {os.environ['PROVENANCE_KEY']}"}
tab = httpx.post(f"{api}/v1/authorize/tabs", headers=h, json={
    "price_sheet": price_sheet, "agent_session_id": "sess_scout_7", "grant_id": mandate_id,
    "max_per_call": 0.01, "daily_cap": 40, "expires_at": "2026-11-14T00:00:00Z",
}).json()
```

The open runs the ordinary authorize path once as `tab.open`, with the seller as the counterparty and `daily_cap` as the amount, so your grant's limits and your policy apply. A policy step-up leaves the tab `pending_approval` until your person approves. Once it's `open`, `tab_token` is ready. A tab lives at most 30 days and never past its grant. Opening is free.

## 3. Send the token on each call (buyer)

```ts
import { tabHeaders } from "@provenanceauth/rails/tabs";

await fetch("https://api.saltmarsh.example/lookup?domain=quillstack.example", { headers: tabHeaders(tab.tab_token) });
```

`tabHeaders` sets `Provenance-Tab: <tab token>`.

## 4. Check each call offline (seller)

Keep the revocation list fresh with `RevocationCache`, then check each call with `verifyTab`:

**Node**

```ts
import { RevocationCache, verifyTab } from "@provenanceauth/rails/tabs";

const revocations = new RevocationCache({ apiBase: process.env.PROVENANCE_API!, aud: agentId, jwks });
revocations.start(); // refreshes at each next_update, with If-None-Match

const r = await verifyTab(req.headers["provenance-tab"], { jwks, seller: agentId, revocations: revocations.current(), amount: 0.002 });
if (!r.valid) return res.status(402).json({ error: r.reason });
```

**Python**

```python
from provenanceauth_verify import fetch_revocations, verify_tab

revocations = fetch_revocations(api_base, agent_id, jwks, current=revocations)  # every next_update
r = verify_tab(request.headers["Provenance-Tab"], jwks, seller=agent_id, revocations=revocations, amount=0.002)
if not r.valid:
    abort(402, r.reason)
```

`verifyTab` checks our signature and expiry, that the token is for your agent, the call's price against `max_per_call`, and the list: closed and revoked tabs, revoked grants and agent sessions, and tabs paused for the day. The list is keyless at [`GET /v1/public/revocations`](https://provenanceauth.com/docs/api/tabs.md#get-v1-public-revocations), signed with the key in our JWKS, and refreshes every 60 seconds by default.

**The bound after a revoke.** Between a revoke and your next refresh, calls on that tab still pass your check. The most that can pass is `max_per_call` × your call rate × the refresh interval: with `max_per_call` at 0.01, 20 calls a second and a 60-second refresh, that's 12 in the tab's currency. Subscribe to the `authorize.tab.revoked` webhook to stop sooner. If your list is older than 3 refresh intervals, `verifyTab` refuses with `revocations_stale`: stop serving until it refreshes.

## 5. Post usage statements (seller)

Post a signed statement at least every 5 minutes while the tab is in use, and whenever usage passes another 20% of the daily cap. `createTabMeter` tells you when one is due:

```ts
import { createTabMeter, signUsageStatement } from "@provenanceauth/rails/tabs";

const meter = createTabMeter({ dailyCap: 40 });
// on each served call:
const period = meter.record(0.002);
if (period) {
  const { statement } = await signUsageStatement({ iss: agentId, tab_id: tabId, ...period }, privateJwk);
  await fetch(`${process.env.PROVENANCE_API}/v1/authorize/tabs/${tabId}/usage`, {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.PROVENANCE_KEY}`, "Content-Type": "application/json" },
    body: JSON.stringify({ ...period, statement }),
  });
}
```

Each period starts at or after the last one's end and stays within one UTC day. A statement that would take the day past `daily_cap` is refused with `tab_cap_exceeded`, nothing from it is recorded, and the tab is paused until the next UTC day (the list names it in `paused`). You pay per 10,000 calls in your statements.

## 6. Count on your side too (buyer)

Your SDK may post its own count with `{ period_start, period_end, buyer_calls }` to [`POST /v1/authorize/tabs/{id}/usage`](https://provenanceauth.com/docs/api/tabs.md#post-v1-authorize-tabs-id-usage). When the day settles, a gap over 2% between the two counts marks the day `contested` and sends `authorize.tab.contested` to both sides.

## Days and budgets

Each UTC day the tab is used reserves `daily_cap` from the grant's budget window. The open reserves the first day; the first statement or count of a later day reserves that one. An hour after a day ends, it settles: what the statements didn't use goes back to the budget, and the day is ledgered once (one row per tab per day). When the budget can't cover a day, the tab is paused for that day.

## 7. Close

Either side closes with [`POST /v1/authorize/tabs/{id}/close`](https://provenanceauth.com/docs/api/tabs.md#post-v1-authorize-tabs-id-close). Revoking the grant, the agent session or the token closes it too, and the seller gets `authorize.tab.revoked`. Statements for periods before the close are accepted for 10 more minutes.

## Errors you might see

| Code | What to do |
| --- | --- |
| `offer_invalid` | The price sheet must be a signed offer with `delivery.kind: "service"`. |
| `grant_invalid` | Open the tab under an active grant of your account. |
| `tab_statement_invalid` | Sign each period after the previous one, within one UTC day, at no more than `max_per_call` per call. |
| `tab_cap_exceeded` | Stop serving on this tab until the next UTC day. |
| `tab_closed` | The tab is closed or expired; the buyer opens a new one. |

## See also

- [Deals between agents](https://provenanceauth.com/docs/authorize/concepts/agent-commerce.md)
- [Sell to agents](https://provenanceauth.com/docs/authorize/guides/sell-to-agents.md)
- [Buy from agents](https://provenanceauth.com/docs/authorize/guides/buy-from-agents.md)
