OpenMeter billing platform integration
Use this guide after your framework-level integration is complete. This page covers OpenMeter-specific setup:- Create the
@ai-billing/openmeterdestination. - Map usage events to OpenMeter customers via CloudEvents
subject. - Sync customer records when users sign up and assign subject keys.
Metering strategy
OpenMeter integrations with Narev use nanos metering only: one meter sumscost_nanos from each CloudEvent. Narev resolves the dollar cost at request time, so you do not copy token rates into OpenMeter plans.
Before you start
Create a meter in the OpenMeter Dashboard with event typellm_usage. The eventType in your destination config must match that type.
Install OpenMeter dependencies
Configure environment variables
Set these on your server:Create OpenMeter billing destinations
Createlib/ai/destinations.ts:
lib/ai/destinations.ts
customerIdKey: 'userId'. Your billing tags must include userId, which becomes the CloudEvents subject.
Sync OpenMeter customers when users sign up
Createlib/openmeter-client.ts:
lib/openmeter-client.ts
subject matches a customer key or an assigned subject key.
Required tag contract
In your generation routes, include:userIdinai-billing-tagsmust match the CloudEventssubjectand the customer subject key.- Each user should have one OpenMeter customer record.
- If
userIdis missing, the destination skips the event to avoid ingesting anonymous usage.
The OpenMeter destination sends
cost_nanos and currency in the CloudEvents data payload. If your provider does not return pricing natively, attach a price resolver (for example, Narev) in your framework middleware. See @ai-billing/openmeter reference.Verify OpenMeter metering
- Register a new user and confirm an OpenMeter customer exists with
key = userId. - Trigger a model call and confirm a CloudEvent with type
llm_usageappears in OpenMeter. - Validate the
datapayload containscost_nanos,currency, and your route context tags. - Confirm
OPENMETER_API_URLpoints at the OpenMeter instance you are checking.
Common issues
Events appear but don’t map to customers
- Confirm
userIdexists inai-billing-tags. - Confirm destination uses
customerIdKey: 'userId'. - Confirm customer creation assigns
subjectKeys: [userId]inusageAttribution.
No events arrive in OpenMeter
- Confirm
OPENMETER_API_KEYis present at runtime. - Confirm the meter event type matches your OpenMeter configuration.
- Confirm
OPENMETER_API_URLmatches your deployment (OpenMeter Cloud vs self-hosted). - Confirm framework routes call the billing-wrapped model helper.
- Confirm each billing event includes a defined
cost(attach a price resolver if needed).
Events are skipped silently
- OpenMeter skips events when no customer identity tag is found. Confirm
userIdis set on every generation call.