Skip to main content

OpenMeter billing platform integration

Use this guide after your framework-level integration is complete. This page covers OpenMeter-specific setup:
  • Create the @ai-billing/openmeter destination.
  • Map usage events to OpenMeter customers via CloudEvents subject.
  • Sync customer records when users sign up and assign subject keys.
For the server middleware setup, use Node.js billing integration.

Metering strategy

OpenMeter integrations with Narev use nanos metering only: one meter sums cost_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 type llm_usage. The eventType in your destination config must match that type.

Install OpenMeter dependencies

Configure environment variables

Set these on your server:
Use a sandbox or test organization until your metering flow is verified.

Create OpenMeter billing destinations

Create lib/ai/destinations.ts:
lib/ai/destinations.ts
The key mapping is customerIdKey: 'userId'. Your billing tags must include userId, which becomes the CloudEvents subject.

Sync OpenMeter customers when users sign up

Create lib/openmeter-client.ts:
lib/openmeter-client.ts
Call this when a user signs up:
OpenMeter attributes usage to a customer when the CloudEvents subject matches a customer key or an assigned subject key.

Required tag contract

In your generation routes, include:
Contract requirements:
  • userId in ai-billing-tags must match the CloudEvents subject and the customer subject key.
  • Each user should have one OpenMeter customer record.
  • If userId is 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

  1. Register a new user and confirm an OpenMeter customer exists with key = userId.
  2. Trigger a model call and confirm a CloudEvent with type llm_usage appears in OpenMeter.
  3. Validate the data payload contains cost_nanos, currency, and your route context tags.
  4. Confirm OPENMETER_API_URL points at the OpenMeter instance you are checking.

Common issues

Events appear but don’t map to customers

  • Confirm userId exists in ai-billing-tags.
  • Confirm destination uses customerIdKey: 'userId'.
  • Confirm customer creation assigns subjectKeys: [userId] in usageAttribution.

No events arrive in OpenMeter

  • Confirm OPENMETER_API_KEY is present at runtime.
  • Confirm the meter event type matches your OpenMeter configuration.
  • Confirm OPENMETER_API_URL matches 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 userId is set on every generation call.
Last modified on October 29, 2026