- Automatic token metering — no manual counting, no usage cron jobs.
- Usage-based invoices — customers pay for exactly what they consume.
- Live margin visibility — billed revenue vs. upstream model cost, per customer.
Before you start
You’ll need:- A Macropay account with an organization.
- An Organization Access Token for API calls.
- A model provider key (OpenAI, Anthropic, etc.).
Build against the Sandbox first. It mirrors
production but never moves real money, so you can replay token events freely
while you tune pricing.
How the pieces fit together
Three objects do the work. Once they exist, your app only emits events:1
Install the SDK
2
Create a meter
In the dashboard, open Usage Based Billing → Meters
and click Create Meter. For a support copilot that bills on tokens:
- Name:
copilot-tokens - Filter: events named
copilot-tokens - Aggregation:
sumover thetotalTokensproperty
3
Attach a metered price
Go to Products, create a new subscription product (or edit one), and add a
Metered Price:
- Meter:
copilot-tokens - Unit price: the price of one metered unit — here, one token
- Billing interval: Monthly
4
Wrap your model
The
@macropayments/ingestion library tracks token counts off any @ai-sdk/*
model and ships them to the meter for you — no manual event payloads.5
Call it from your route
Pass
customerId so usage lands on the right account. Every generateText /
streamText call emits an event automatically.Watch it land
Events show up within seconds. Open Usage Based Billing → Meters, pickcopilot-tokens, and you’ll see per-customer totals and the running aggregate for
the current period. Customers see their own projected charges in the
customer portal — no support ticket required.
Billing runs itself
At the close of each period, Macropay:- Reads each customer’s aggregated meter value.
- Multiplies it by your metered price.
- Charges the payment method on file.
- Issues a tax-compliant invoice as Merchant of Record.
Protect your margin
Token revenue only helps if it beats token cost. Record your upstream spend in a_cost block on the event, and Macropay tracks it next to the billed amount:
src/lib/llm.ts
Routing through the AI Proxy instead?
Then skip all of this — the proxy writes
_cost for you, with the provider cost
and your margin already separated.Option B: let the AI Proxy meter for you
Everything above assumes you count tokens yourself and ingest your own events. If you route model calls through the AI Proxy instead, metering is automatic — and you should not hand-roll a meter, because the proxy emits its own event shape and a meter filtered on anything else will silently match nothing and bill zero. The contract. Every completion writes one event:
Property names are
snake_case. totalTokens will not match anything.
You get two meters for free. The first time you add a model config, Macropay
creates both of these for the organization:
- AI Usage — sums
_cost.amount_cents, i.e. what the call cost including your margin. Attach a metered price with a unit price of 0.012 to take a further 20% on top of the margin already configured per model. - AI Token Usage — sums
total_tokens. Attach this instead if you’d rather price per token and ignore what the model actually cost you.
Where to go next
Usage-based billing
The full model behind meters, events, and aggregation.
Ingestion strategies
Stream, S3, delta-time, and custom event sources.
Prepaid credits
Grant credits and draw them down against any meter.
Cost insights
Track upstream model spend and per-customer margin.