Prerequisites
- A Macropay account with an organization
- An Organization Access Token
- An AI/LLM API key (e.g., OpenAI)
We recommend using the Sandbox environment for this guide
so you can test without processing real payments.
Step 1: Install Dependencies
Step 2: Create a Meter
A Meter tracks usage events and computes aggregated values for billing. Create one in the Macropay dashboard:- Go to Usage Based Billing > Meters
- Click Create Meter
- Configure:
- Name:
llm-tokens - Filter: Match events named
llm-tokens - Aggregation:
sumon thetotalTokensproperty
- Name:
Step 3: Create a Metered Product
- Go to Products and create a new product (or edit an existing subscription product)
- Add a Metered Price:
- Meter: Select
llm-tokens - Unit price: Set your price per 1,000 tokens (e.g., $0.01 per 1k tokens)
- Billing interval: Monthly
- Meter: Select
Step 4: Wrap Your AI Model
TypeScript (Vercel AI SDK)
Use the@macropay/ingestion library to automatically track token usage from any @ai-sdk/* model:
Python (PydanticAI)
Step 5: Use in Your API Route
TypeScript (Next.js)
generateText (or streamText) automatically sends an ingestion event to Macropay with the token counts.
Python (FastAPI + PydanticAI)
Step 6: View Usage in Dashboard
Once events are flowing, you can monitor usage in the Macropay dashboard:- Go to Usage Based Billing > Meters
- Select your
llm-tokensmeter - View per-customer usage and aggregated totals
- Customers can also view their estimated charges in the Customer Portal
Step 7: Automatic Billing
At the end of each billing period, Macropay automatically:- Reads the aggregated meter value for each customer
- Calculates the charge based on your metered price
- Charges the customer’s payment method on file
- Generates an invoice
Adding Cost Tracking (Optional)
Track your upstream AI costs alongside customer billing to monitor margins:What’s Next?
Usage Based Billing
Deep dive into meters, events, and billing
Ingestion Strategies
Explore S3, streaming, and custom strategies
Meter Credits
Grant prepaid credits against meters
Cost Insights
Track upstream costs and margins