Stripe billing platform integration
Use this guide after your framework-level integration is complete. This page covers Stripe-specific setup:- Choose a metering strategy (nanos or itemized).
- Create the
@ai-billing/stripedestination. - Map usage events to Stripe customers.
- Sync customer records when users sign up and store the Stripe customer ID.
Metering strategy
Stripe supports two ways to bill LLM usage with Narev:
The rest of this guide covers nanos metering: one
llm_usage meter, cost in value, customer in stripe_customer_id.
Itemized metering (advanced)
Itemized billing creates one Stripe product per model with up to six meters (input_tokens, output_tokens, reasoning_tokens, cache_read_tokens, cache_write_tokens, request_count). Each meter gets a metered price whose unit_amount_decimal reflects Narev’s per-token USD rate converted to cents:
Before you start
Create a Stripe Meter in the Stripe Dashboard with an event name such asllm_usage. The meterName in your destination config must match that event name.
Install Stripe dependencies
Configure environment variables
Set these on your server:sk_test_...) until your metering flow is verified.
Create Stripe billing destinations
Createlib/ai/destinations.ts:
lib/ai/destinations.ts
stripe_customer_id from ai-billing-tags on each generation call.
Sync Stripe customers when users sign up
Createlib/stripe-client.ts:
lib/stripe-client.ts
Required tag contract
In your generation routes, includestripe_customer_id in ai-billing-tags:
stripe_customer_idinai-billing-tagsmust match a Stripe Customer ID (cus_...).- Each user should have one Stripe customer record.
- Stripe limits meter event payloads to 10 keys. The destination prioritizes
value(cost) andstripe_customer_id, then core AI metrics and your custom tags.
The Stripe destination sends cost as the meter
value. If your provider does not return pricing natively, attach a price resolver (for example, Narev) in your framework middleware. See @ai-billing/stripe reference.Verify Stripe metering
- Register a new user and confirm a Stripe customer exists with ID
cus_.... - Trigger a model call and confirm a meter event named
llm_usageappears in Stripe. - Validate the payload contains
stripe_customer_id,value, and your route context tags. - Confirm your environment uses the expected Stripe mode (test or live key).
Common issues
Events appear but don’t map to customers
- Confirm
stripe_customer_idexists inai-billing-tags. - Confirm the tag value is a valid Stripe Customer ID, not your internal
userId. - Confirm the customer was created in the same Stripe account as your secret key.
No events arrive in Stripe
- Confirm
STRIPE_SECRET_KEYis present at runtime. - Confirm the meter event name matches your Stripe Meter configuration.
- Confirm framework routes call the billing-wrapped model helper.
- Confirm each billing event includes a defined
cost(attach a price resolver if needed).
Meter payload is truncated
- Stripe allows at most 10 payload keys. Keep custom tags minimal or use
mapMetadataincreateStripeDestinationto control which fields are sent.