Lago billing platform integration
Use this guide after your framework-level integration is complete. This page covers Lago-specific setup:- Create the
@ai-billing/lagodestination. - Map usage events to Lago customers.
- Sync user identity with
external_id.
Metering strategy
Lago integrations with Narev use nanos metering only: one billable metric aggregatescost_nanos from each event. Narev resolves the dollar cost at request time, so you do not copy token rates into Lago plans.
The removed Narev Cloud Connect flow provisioned a metric named LLM Usage Nanos with code llm_usage_nanos, aggregating the cost_nanos field. You can create the same metric manually or use any code. Keep meterCode in your destination config aligned with it.
Narev Cloud no longer hosts a Connect dashboard for Lago. Credentials and billable metrics live in your Lago workspace and application environment variables. Itemized per-model product sync was never supported for Lago.
Before you start
Create a billable metric in the Lago Dashboard with codellm_usage. The meterCode in your destination config must match that code.
Install Lago dependencies
Configure environment variables
Set these on your server:Create Lago billing destinations
Createlib/ai/destinations.ts:
lib/ai/destinations.ts
externalCustomerIdKey: 'userId'. Your billing tags must include userId.
Sync Lago customers when users sign up
Createlib/lago-client.ts:
lib/lago-client.ts
external_id, so repeated calls with the same userId update the existing record.
Required tag contract
In your generation routes, include:userIdinai-billing-tagsmust match the Lago customerexternal_id.- Each user should have one Lago customer record.
- If
userIdis missing, the destination skips the event to avoid ingesting anonymous usage.
The Lago destination sends
cost_nanos and currency in the event properties. If your provider does not return pricing natively, attach a price resolver (for example, Narev) in your framework middleware. See @ai-billing/lago reference.Verify Lago metering
- Register a new user and confirm a Lago customer exists with
external_id = userId. - Trigger a model call and confirm an event with code
llm_usageappears in Lago. - Validate properties contain
cost_nanos,currency,userId, and your route context tags. - Confirm
LAGO_API_URLpoints at the Lago instance you are checking.
Common issues
Events appear but don’t map to customers
- Confirm
userIdexists inai-billing-tags. - Confirm destination uses
externalCustomerIdKey: 'userId'. - Confirm customer creation uses
external_id = userId.
No events arrive in Lago
- Confirm
LAGO_API_KEYis present at runtime. - Confirm the billable metric code matches your Lago configuration.
- 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
- Lago skips events when no customer identity tag is found. Confirm
userIdis set on every generation call.