# 57. Metering and receipts

> **Status: unimplemented — design draft.** This chapter is a plan for the
> owner to edit. It ships no code yet, and paths marked planned do not exist.

## Premise

Paid calls happen. This chapter counts them in a way nobody has to take on
trust: every paid call writes a signed receipt, and the totals can be
recomputed by anyone.

## Draft

An author who is paid "for all paid usage" has one question: how do I know the
count is right? On a closed platform you can't know. Here the count is built
from receipts.

A receipt is a signed record of one paid call. It names the release that was
called, by hash. It gives the price, the time, and the split of the price
among the people owed. It carries proof of the payment. It does not carry what
the call was about. Your request and the result stay private.

A meter turns receipts into totals: per release, per author, per day. The
meter is a plain program over public records, so an author can run it and get
the same total we do. [Episode 37](../../docs/transcripts/037.md) asks for
this at the start: a flow of funds with public accounting.

Three cases need rules. A call that fails after payment is refunded or not
charged, and the receipt says which. A plugin that calls another paid plugin
produces a chain of receipts, and each author is owed for their link. And our
own use is marked: calls made by us, by our agents and by tests are excluded
from the numbers that describe real use, though authors are still paid for
them if they were paid calls.

Receipts are also what the reputation of chapter 60 reads. Paid use is
evidence that people found a member worth money, and it is costly to fake.

## What we build

A plan for what this chapter ships once the draft is approved:

- A receipt record for every paid call: release hash, price, split and payment
  proof, with no private content.
- A meter that computes totals from receipts, shipped so that authors can run
  it themselves.
- Rules for failed calls, chained calls and our own use.

It builds on what exists today:

- [`coax/lib/x402`](../../coax/lib/x402)
- [`coax/lib/nostr/event_sign.coax`](../../coax/lib/nostr/event_sign.coax)
- [`coax/lib/quantity.coax`](../../coax/lib/quantity.coax)
- [`coax/lib/gateway/usage-observation-checks.coax`](../../coax/lib/gateway/usage-observation-checks.coax)

On the protocol ([chapter 65](ch065-one-schema-three-carriages.md)):

| Record | Schema | Carriage | Today |
| --- | --- | --- | --- |
| Receipt for a call | `openagents.receipt.v1` `ExecutionReceipt` ([spec 16.3](../../docs/protocol/openagents-protocol-spec.md#163-openagentsreceiptv1)) | HTTP: the `X-Receipt` header or a response field; Nostr: inside kinds 3204 and 3201 | Spec sketch; no `.proto` file |
| Proof of payment | `openagents.payment.v1` `PaymentReceipt`, `PaymentEvidence` ([spec 26.5](../../docs/protocol/openagents-protocol-spec.md#265-openagentspaymentv1)) | With the receipt | Spec sketch; no `.proto` file |
| Totals | `openagents.ledger.v1` `Entry`, `Statement` ([spec 26.6](../../docs/protocol/openagents-protocol-spec.md#266-openagentsledgerv1-and-openagentsbillingv1)) | HTTP | Spec sketch; no `.proto` file |

### The chapter's check

- **Source:** `pay/receipt-policy.coax` and `pay/call-meter.coax`, run on a
  case of four paid calls to a fake x402 service with recorded answers: a
  plain call, a call that fails after payment, a plugin that calls another
  paid plugin, and a test call of our own.
- **Trace:** under the controlled clock, the five receipts and the meter's
  totals per release, per author and per day. Each receipt prints the
  release hash, the price in millisats, the split and the payment proof, and
  no request text. The failed call's receipt reads `refunded`. Our test call
  is left out of the use totals and kept in the author totals.
- **Refusal:** a receipt whose split does not add up to its price, or whose
  signature does not verify, is not counted. The meter prints the receipt's
  ID and `refused: split does not equal price` or `refused: bad signature`,
  and the totals do not change.
- **Independent check:** the totals, worked out by hand from the five
  receipts and written in the case file, and a second meter of a few dozen
  lines, written from spec 26.6 alone, that sums the same receipts and
  prints the same totals.

Shipped paths: [`pay/receipt-policy.coax`](../../pay/receipt-policy.coax) (`#14060`, canonical JCS `openagents.capability.receipt.v1` codec and public call meter, exercised in [`openagents/examples/capability-economy-e2e.coax`](../examples/capability-economy-e2e.coax)).

Planned paths: `pay/call-meter.coax`, beside
the speech meter in [`pay/voice-meter.coax`](../../pay/voice-meter.coax).

## Sources

- **Episodes:**
  - [Episode 37, *Flow of Funds*](../../docs/transcripts/037.md): flow of funds with public accounting
  - [Episode 95, *Streaming Money*](../../docs/transcripts/095.md)
  - [Episode 96, *Payments & Payouts*](../../docs/transcripts/096.md)
  - [Episode 171, *Visualizing Agent Payments*](../../docs/transcripts/171.md)
  - [Episode 252, *Observer, Automated QA*](../../docs/transcripts/252.md): exact receipts
  - [Episode 289, *OpenAgents*](../../docs/transcripts/289.md): 2:00, the dots going back and forth
- **Old code at `coder-v1.0.0`, crates:** `receipts`, `pay-ledger`, `commercial-spend`, `hook-log`
- **NIPs:** [NIP-X402](../../nostr/nips/openagents/NIP-X402.md), [NIP-XP](../../nostr/nips/openagents/NIP-XP.md), [NIP-MKT](../../nostr/nips/openagents/NIP-MKT.md)
- **Promises covered** ([audit](../../docs/audits/2026-11-10-episode-289-port-audit.md#1-what-episode-289-promised)): P11

## Open questions for the owner

1. How public is the ledger: every receipt, or totals per release with
   receipts shown only to the author and the payer?
