- 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: your rate per 1,000 tokens — say
$0.02 - 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. Attach a.cost() callback and
Macropay records your upstream spend next to the billed amount:
src/lib/llm.ts
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.