Skip to main content

Lago billing platform integration

Use this guide after your framework-level integration is complete. This page covers Lago-specific setup:
  • Create the @ai-billing/lago destination.
  • Map usage events to Lago customers.
  • Sync user identity with external_id.
For the server middleware setup, use Node.js billing integration.

Metering strategy

Lago integrations with Narev use nanos metering only: one billable metric aggregates cost_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 code llm_usage. The meterCode in your destination config must match that code.

Install Lago dependencies

Configure environment variables

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

Create Lago billing destinations

Create lib/ai/destinations.ts:
lib/ai/destinations.ts
The key mapping is externalCustomerIdKey: 'userId'. Your billing tags must include userId.

Sync Lago customers when users sign up

Create lib/lago-client.ts:
lib/lago-client.ts
Call this when a user signs up:
The Lago customer API upserts on external_id, so repeated calls with the same userId update the existing record.

Required tag contract

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

  1. Register a new user and confirm a Lago customer exists with external_id = userId.
  2. Trigger a model call and confirm an event with code llm_usage appears in Lago.
  3. Validate properties contain cost_nanos, currency, userId, and your route context tags.
  4. Confirm LAGO_API_URL points at the Lago instance you are checking.

Common issues

Events appear but don’t map to customers

  • Confirm userId exists in ai-billing-tags.
  • Confirm destination uses externalCustomerIdKey: 'userId'.
  • Confirm customer creation uses external_id = userId.

No events arrive in Lago

  • Confirm LAGO_API_KEY is 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 userId is set on every generation call.