Verifying receipts and recomputing charges
To verify a receipt, check three things: the signature is from a receipt key listed in the seller's current signed listing, every field is bound to the hold you sent, and the charge equals what the route's signed price schedule gives for the receipt's token counts and status, capped at your hold's cap. The SDK does all three on every request before it pays. This page shows the rule, a worked example, and how to check a receipt yourself.
The charge formula
charge = min(cap, ceil((fresh_in·r_in + cached·r_cached + out_visible·r_out_visible + out_hidden·r_out_hidden) / 1,000,000) + per_request_fee)
cachedis the receipt's cached input, clamped to at most the input count;fresh_inis input minus cached.- Rates (
r_…) are micro-USDC per million tokens, from the signed schedule named by the receipt'sschedule_digest. - The sum is divided and rounded up once per request, in integers wide enough not to overflow. The fee is added after rounding.
- For
cancelled, the output terms are zero: input, cached input and the fee only. - For
failed,not_servedandpolicy_unhonored, the charge is 0.
Worked example
The route's schedule:
| Rate | micro-USDC per million tokens | In dollars |
|---|---|---|
input |
300,000 | $0.30 / M |
cached_input |
150,000 | $0.15 / M |
output_visible |
1,200,000 | $1.20 / M |
output_hidden |
1,200,000 | $1.20 / M |
per_request_fee |
10 per request | $0.00001 |
The receipt says status ok, 1,234 input tokens of which 200 were cached, 350 visible output tokens and 0 hidden. Your hold's cap was 20,000.
| Step | Calculation | Result |
|---|---|---|
| Fresh input | (1,234 − 200) × 300,000 | 310,200,000 |
| Cached input | 200 × 150,000 | 30,000,000 |
| Visible output | 350 × 1,200,000 | 420,000,000 |
| Hidden output | 0 × 1,200,000 | 0 |
| Sum | 760,200,000 | |
| Divide by 1,000,000, round up | ceil(760.2) | 761 |
| Add the fee | 761 + 10 | 771 |
| Apply the cap | min(20,000, 771) | 771 |
The charge is 771 micro-USDC, or $0.000771. If the client had disconnected mid-stream, the receipt would be cancelled: 340,200,000 rounds up to 341, plus the fee, so 351. Had the cap been 500, the receipt would charge 500, and the seller would absorb the other 271.
The same numbers with the protocol package:
import { computeCharge, Status } from "@im/protocol";
const schedule = {
rates_per_mtok: { input: 300_000, cached_input: 150_000, output_visible: 1_200_000, output_hidden: 1_200_000 },
per_request_fee: 10,
};
const usage = { input: 1_234n, cachedInput: 200n, outputVisible: 350n, outputHidden: 0n };
console.log(computeCharge(schedule, usage, Status.Ok)); // 771n
console.log(computeCharge(schedule, usage, Status.Cancelled)); // 351n
console.log(computeCharge(schedule, usage, Status.Failed)); // 0n
computeCharge returns the uncapped amount; apply the cap yourself. It throws if the charge cannot fit in 64 bits, and a seller must then issue failed at charge 0.
Everything the SDK checks
For each receipt, against the hold it signed and the route's signed listing:
- the receipt is exactly 382 bytes and its signature verifies under the listed key for its
key_epoch, in your program's domain; seller_id,route_id,model_identity_idandschedule_digestmatch the route;channel,channel_epoch,request_id,request_commitmentandcap_microsmatch the hold;- the charge equals the recomputed charge, capped at the cap;
- for a request that got a non-2xx answer, only a zero-charge
failedornot_servedreceipt is accepted. A signed contradiction is kept as evidence insdk.equivocations()and never paid.
If a check fails, the request fails with MarketplaceError code invalid_receipt, and the receipt is not paid.
Checking a receipt yourself
Use verifyReceipt from the SDK with the hold you sent and the verified route. It runs the same checks, including the charge, and returns the decoded receipt:
import { Buffer } from "node:buffer";
import { decodeHold } from "@im/protocol";
import { MarketplaceError, verifyReceipt, type Route, type SignedReceipt } from "@im/sdk";
/** `holdBase64` is the X-IM-Hold header you sent; `route` comes from DiscoveryClient. */
export function auditReceipt(signed: SignedReceipt, holdBase64: string, route: Route): bigint {
const hold = decodeHold(Buffer.from(holdBase64, "base64"));
try {
const receipt = verifyReceipt(signed, hold, route);
return receipt.chargeMicros;
} catch (error) {
if (error instanceof MarketplaceError) console.error("rejected:", error.message);
throw error;
}
}
After a receipt-key rotation, pass the seller's current route as the fourth argument, verifyReceipt(signed, hold, oldRoute, currentRoute), so keys listed in the current listing are accepted.