In this article, you will learn how to build a billing system with Lago based on tokens. This template is suitable for Large Language Model (LLM) and Generative AI companies whose pricing can vary based on the application or model used.

Pricing structure

For OpenAI, pricing depends on the language model used. Here are several price points they offer: “Prices are per 1,000 tokens. You can think of tokens as pieces of words, where 1,000 tokens is about 750 words (learn more here).”
ModelInputOutput
8K context$0.03 / 1,000 tokens$0.06 / 1,000 tokens
32K context$0.06 / 1,000 tokens$0.12 / 1,000 tokens

Get started

1

Set up Lago

LAGO_URL="https://api.getlago.com"
API_KEY="__API_KEY__"
2

Create a billable metric

Create a single billable metric to track token usage across different models and input/output types.
  1. Add a unique code for your billable metric
  2. Set aggregation_type to sum_agg
  3. Set field_name to tokens
  4. Set recurring to false
  5. Set filters to distinguish between models and token types
curl --location --request POST "$LAGO_URL/api/v1/billable_metrics" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "billable_metric": {
      "name": "AI Tokens",
      "code": "__BILLABLE_METRIC_CODE__",
      "description": "Token usage across OpenAI models",
      "aggregation_type": "sum_agg",
      "field_name": "tokens",
      "recurring": false,
      "filters": [
        {
          "key": "model",
          "values": ["8k", "32k"]
        },
        {
          "key": "type", 
          "values": ["input", "output"]
        }
      ]
    }
  }'
Refer to the API reference and guide on filters to learn more.
3

Create a plan

Create a plan to price packages of tokens used with OpenAI’s specific pricing tiers.
  1. Add a unique code for your plan
  2. Under charges, configure pricing tiers for different models and token types
curl --location --request POST "$LAGO_URL/api/v1/plans" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "plan": {
      "name": "OpenAI Pricing",
      "code": "__PLAN_CODE__",
      "amount_cents": 0,
      "amount_currency": "USD",
      "interval": "monthly",
      "pay_in_advance": false,
      "charges": [
        {
          "billable_metric_id": "__BILLABLE_METRIC_ID__",
          "charge_model": "package",
          "filters": [
            {
              "invoice_display_name": "8k-input",
              "properties": {
                "amount": "0.03",
                "free_units": 0,
                "package_size": 1000000
              },
              "values": {
                "model": ["8k"],
                "type": ["input"]
              }
            },
            {
              "invoice_display_name": "8k-output", 
              "properties": {
                "amount": "0.06",
                "free_units": 0,
                "package_size": 1000000
              },
              "values": {
                "model": ["8k"],
                "type": ["output"]
              }
            },
            {
              "invoice_display_name": "32k-input",
              "properties": {
                "amount": "0.06", 
                "free_units": 0,
                "package_size": 1000000
              },
              "values": {
                "model": ["32k"],
                "type": ["input"]
              }
            },
            {
              "invoice_display_name": "32k-output",
              "properties": {
                "amount": "0.12",
                "free_units": 0, 
                "package_size": 1000000
              },
              "values": {
                "model": ["32k"],
                "type": ["output"]
              }
            } 
          ]
        }
      ]
    }
  }'
Refer to the API reference and guide on charges with filters to learn more.
4

Create a customer

Create a customer with a unique external_id.
curl --location --request POST "$LAGO_URL/api/v1/customers" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "customer": {
      "external_id": "__EXTERNAL_CUSTOMER_ID__",
      "name": "Acme Inc",
      "email": "john@acme.com",
      "currency": "USD",
      "timezone": "America/New_York"
    }
  }'
Refer to the API reference to create a customer.
5

Create a subscription

Create a subscription for the customer with the plan’s code.
curl --location --request POST "$LAGO_URL/api/v1/subscriptions" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "subscription": {
      "external_customer_id": "__EXTERNAL_CUSTOMER_ID__",
      "plan_code": "__PLAN_CODE__",
      "external_id": "__EXTERNAL_SUBSCRIPTION_ID__"
    }
  }'
Refer to the API reference to create a subscription.
6

Ingest usage via events

Send usage events to Lago to track usage.
  1. Reference your billable metric with code
  2. Reference the customer’s subscription with external_subscription_id
  3. Include usage and filters data in properties
LAGO_URL="https://api.getlago.com"
API_KEY="__API_KEY__"

curl --location --request POST "$LAGO_URL/api/v1/events" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "event": {
      "transaction_id": "__TRANSACTION_ID__",
      "code": "__BILLABLE_METRIC_CODE__",
      "external_subscription_id": "__EXTERNAL_SUBSCRIPTION_ID__",
      "properties": {
        "total": 5000,
        "model": "8k",
        "type": "input"
      }
    }
  }'
Refer to the API reference to create an event.
7

Monitor current usage

Track real-time customer usage for the current billing period.
curl --location --request GET "$LAGO_URL/api/v1/customers/__EXTERNAL_CUSTOMER_ID__/current_usage?external_subscription_id=__EXTERNAL_SUBSCRIPTION_ID__" \
--header 'Authorization: Bearer __API_KEY__' \
--header 'Content-Type: application/json'
Refer to the API reference to get the current usage.

Wrap-up

Per-token pricing offers flexibility and visibility, and allows LLM and Generative AI companies like OpenAI to attract more customers. With Lago, you can create your own metric dimensions to adapt this template to your products and services. Give it a try, click here to get started!