Skip to main content
This template builds token-based billing with Lago: one metered product, priced separately per model and token type through product filters. It suits LLM and generative AI companies whose price depends on the model and on whether tokens are input or output. Legacy (v1): Clone OpenAI pricing with subscriptions.

Pricing structure

OpenAI prices tokens per 1,000, with a different price per model and per token type.

How it maps to Lago

Get started

1

Set up Lago

Use https://api.eu.getlago.com for the EU cluster. API endpoints live under /api/v2 and require the product catalog to be enabled on your organization.
2

Create a billable metric

Create one billable metric that sums tokens. Declare the model and type dimensions so product filters can use them.
  1. Set aggregation_type to sum_agg
  2. Set field_name to tokens
  3. Set recurring to false
  4. Add filters for model and type
Refer to the API reference and the billable metrics guide.
3

Create a metered product

Create the product you sell. A metered product requires billable_metric_code. Neither the type nor the metric can change after creation.
Refer to the API reference and the metered products guide.
4

Create product filters

Create one product filter per model and token type. Each filter value must be a key and a configured value of the billable metric.
Repeat for the other three combinations:Refer to the API reference and the product filters guide.
5

Create a rate card per filter

Create one rate card per filter. The rate card targets the filter with product_filter_code and holds the currency and billing timing. Its initial rate uses the package model: amount per package_size tokens.
Repeat for the other filters:Refer to the API reference, the rate cards guide and the package model.
6

Create a plan and apply the rate cards

Create the plan in the same currency as the rate cards.
Apply each of the four rate cards. Only one rate card can price each product and filter combination.
Refer to the API reference and the plans guide.
7

Create a customer

An existing external_id returns 422 instead of updating the customer. Refer to the API reference to create a customer.
8

Create a contract

Create a contract for the customer. Replace __PLAN_CODE__ with openai_payg.
The plan supplies the contract’s initial applied rate cards. A future started_at creates a pending contract that becomes active on that date. Only one pending or active contract can use a given external_id at a time. Refer to the API reference to create a contract.
The example dates are in the future. To test right away, set each rate’s effective_from to today and omit started_at, which defaults to now.
9

Ingest usage via events

Send usage events that target the contract.
  1. Set code to the billable metric code
  2. Set external_contract_id to the contract’s external_id
  3. Send the token count in tokens, plus model and type
external_subscription_id is not supported on events and returns 422. Refer to the API reference and the usage ingestion guide.
10

Monitor current usage

Track usage across the contract’s open billing periods.
The response returns amount_cents, taxes_amount_cents, total_amount_cents and a rate_cards array with usage per applied rate card. Pending contracts have no usage. Totals can span several invoices, so this is not a preview of one invoice. Filter with rate_card_code, product_code, product_filter_code or product_type. Refer to the API reference to get current usage.

Worked example

Over one full monthly period, the customer sends 500,000 8K input tokens and 200,000 8K output tokens. Token counts are exact multiples of the package size.

What changed from v1

  • Charge filters on a plan become product filters on a metered product. Each filter gets its own rate card.
  • Prices live on rates with an effective_from date. A new rate changes the price for every plan and contract that does not override it.
  • Events target external_contract_id instead of external_subscription_id.

Product filters

Price subsets of metered usage separately.

Package model

Charge a fixed amount per bundle of units.

Contracts

Apply a plan to a customer.

Create a rate card (API)

POST /api/v2/rate_cards