Signing receipts and billing by outcome
A receipt is your signed statement of what happened to one admitted request and what it costs. The gateway issues exactly one per admitted Hold, persists it before the buyer sees it, and serves it again on request. Receipts carry metadata only: no prompt, output, tool argument or credential.
The receipt (IM/RECEIPT/1, 382 bytes)
| Field | Bytes | Value |
|---|---|---|
| tag | 16 | IM/RECEIPT/1, zero padded |
| domain | 64 | genesis hash ‖ program ID |
| seller_id | 32 | your listing public key |
| key_epoch | 4 | epoch of the signing receipt key |
| route_id, model_identity_id | 32 + 32 | the route and identity served |
| request_id, request_commitment | 32 + 32 | copied from the Hold |
| channel, channel_epoch | 32 + 8 | copied from the Hold |
| status | 1 | 0 ok, 1 cancelled, 2 failed, 3 not_served, 4 policy_unhonored |
| usage_input, usage_cached_input, usage_output_visible, usage_output_hidden | 4 × 8 | token counts |
| usage_source | 1 | 0 upstream, 1 estimated, 2 no usage measured |
| charge_micros | 8 | min(charge, cap_micros) |
| cap_micros | 8 | the Hold's cap |
| schedule_digest | 32 | the schedule the charge used |
| ttft_ms, duration_ms | 4 + 4 | time to first token, total duration |
| created_at | 8 | unix seconds |
Integers are little-endian. The signature is Ed25519 over these exact bytes by the receipt key published in your listing's receipt_keys at key_epoch. Buyers accept a receipt only under a key and epoch listed in your current signed listing (A14).
How the status is decided
| What happened | HTTP to the buyer | Status | Charged |
|---|---|---|---|
Upstream finished with usage, first token within ttft_max_ms |
200 | ok |
full charge |
| Buyer disconnected after at least one streamed frame was forwarded | (connection gone) | cancelled |
input + fee, no output |
| Buyer disconnected before any byte was forwarded | (connection gone) | not_served |
0 |
Body is not JSON, has no messages array, or uses options the adapter rejects |
400 invalid_request |
not_served |
0 |
| Upstream answered 4xx (for example its own 429) | same status, backend_rejected |
not_served |
0 |
| Upstream answered 5xx, or the stream ended without a final usage report | 5xx / 502 backend_failed |
failed |
0 |
No first token within ttft_max_ms, or the whole request exceeded timeoutMs |
504 backend_timeout |
failed |
0 |
An adapter forwarded its own data: [DONE] |
stream ends | failed |
0 |
| The charge overflows u64 | 502 backend_failed |
failed |
0 |
| The gateway was stopped mid-request, or crashed and recovered the claim | 503 / none | failed |
0 |
Once a stream has started, the buyer has already received HTTP 200; a later failure arrives as the final im_receipt event carrying the failed status and charge 0.
policy_unhonored exists in the protocol for a seller who breaks a declared policy; the reference gateway never issues it, because it cannot observe retention.
For a cancelled request without upstream usage, input is estimated at four prompt characters per token and usage_source is 1. The reference gateway never bills output on cancellation; decision 0003 records that billing only forwarded output would also conform, while billing unforwarded output never does.
Delivery
Non-streaming: the receipt rides on the response as
X-IM-Receipt: base64(receipt).base64(sig). Error responses issued after admission carry it too.Streaming: after the last upstream frame the gateway writes one event, then the terminator:
event: im_receipt data: {"receipt":"<base64>","sig":"<base64>"} data: [DONE]Lookup:
GET /im/v1/requests/{request_id_hex}/receiptreturns{"receipt","sig"}withcache-control: no-store, or 404not_found. Buyers use it after a lost connection.
Per A29, once the buyer has received a 2xx status the request is billable according to your signed receipt, even if the buyer cancels or times out; the buyer is expected to recover the receipt and voucher it.
Timing fields
ttft_ms: from upstream dispatch to the first frame carrying a token (streaming) or to the first non-empty upstream body byte (buffered, A26).ttft_max_msis enforced on that same measurement.duration_ms: from the moment the admitted request is handed to the proxy (after the claim) to the end.
Probers and buyers compare these with what they observe themselves; they are your claim, not independent measurements.
Durability guarantees
- The Hold's claim is written and synced to disk before any completion byte leaves the gateway. A crash after that point finalizes the request as
failedat 0 on restart; it is never dispatched again. - The receipt is persisted (SQLite WAL sync) before it is sent, so the receipt a buyer saw is byte-identical to the one you can later present.
- On restart the gateway refuses to open a ledger with an unfinished request whose route or schedule is no longer configured. Keep old routes configured until their requests are finalized.
Equivocation
If you return a non-2xx answer and later present a receipt with a charge for the same request_id, the buyer keeps both as signed evidence of equivocation and never vouchers it (A22). The reference gateway never does this: every pre-admission refusal has no receipt, and every post-admission error receipt is failed or not_served at 0.