> ## Documentation Index
> Fetch the complete documentation index at: https://getlago.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Bill per token: replicate Mistral pricing with contracts

> Rebuild Mistral's per-token pricing in Lago with a metered product, one product filter per model and token type, package rates and a contract.

This template builds token-based billing like Mistral's with Lago: one metered product, priced per model and token type through product filters. It suits LLM and generative AI companies whose price depends on the model used and on input versus output tokens.

Legacy (v1): [Clone Mistral pricing with subscriptions](https://getlago.com/docs/templates/per-token/mistral.md).

## Pricing structure

Mistral prices tokens per million, with a different price per model and per token type.

| Model | Input | Output |
| - | - | - |
| mistral-small | \$1 / 1M tokens | \$3 / 1M tokens |
| mistral-medium | \$2.7 / 1M tokens | \$8.1 / 1M tokens |
| mistral-large | \$4 / 1M tokens | \$12 / 1M tokens |

## How it maps to Lago

| Pricing element | Lago object |
| - | - |
| Token usage | Billable metric `mistral_tokens` (`sum_agg` on `tokens`) with `model` and `type` dimensions |
| What you sell | Metered product `mistral_tokens_product` |
| Model and token type combinations | Six product filters |
| Price per million tokens | One rate card per filter, with a `package` rate |
| The offer | Plan `mistral_payg` with six applied rate cards |
| The customer agreement | Contract `contract_example` |

## Get started

**1. Set up Lago**

```bash cURL theme={"dark"}
LAGO_URL="https://api.getlago.com"
API_KEY="__YOUR_API_KEY__"
```

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, with `model` and `type` dimensions.

```bash cURL highlight={6,8-21} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/billable_metrics" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "billable_metric": {
      "name": "AI tokens",
      "code": "mistral_tokens",
      "description": "Token usage across models",
      "aggregation_type": "sum_agg",
      "field_name": "tokens",
      "recurring": false,
      "filters": [
        {
          "key": "model",
          "values": ["mistral-small", "mistral-medium", "mistral-large"]
        },
        {
          "key": "type",
          "values": ["input", "output"]
        }
      ]
    }
  }'
```

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/billable-metrics/create.md) and the [billable metrics guide](https://getlago.com/docs/v2/guide/billable-metrics/create-billable-metrics.md).

**3. Create a metered product**

```bash cURL highlight={7-8} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/products" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "product": {
      "name": "AI tokens",
      "code": "mistral_tokens_product",
      "product_type": "metered",
      "billable_metric_code": "mistral_tokens"
    }
  }'
```

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/products/create.md) and the [metered products guide](https://getlago.com/docs/v2/guide/product-catalog/metered-products.md).

**4. Create product filters**

Create one product filter per model and token type.

```bash cURL highlight={6,8-11} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/products/mistral_tokens_product/filters" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "filter": {
      "name": "mistral-small input",
      "code": "mistral_small_input",
      "invoice_display_name": "mistral-small, input tokens",
      "values": [
        { "key": "model", "value": "mistral-small" },
        { "key": "type", "value": "input" }
      ]
    }
  }'
```

Repeat for each combination:

| Filter `code` | `model` | `type` |
| - | - | - |
| `mistral_small_input` | `mistral-small` | `input` |
| `mistral_small_output` | `mistral-small` | `output` |
| `mistral_medium_input` | `mistral-medium` | `input` |
| `mistral_medium_output` | `mistral-medium` | `output` |
| `mistral_large_input` | `mistral-large` | `input` |
| `mistral_large_output` | `mistral-large` | `output` |

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/product-filters/create.md) and the [product filters guide](https://getlago.com/docs/v2/guide/product-catalog/product-filters.md).

**5. Create a rate card per filter**

Each rate card targets one filter. Its initial rate uses the `package` model with a package of 1,000,000 tokens.

```bash cURL highlight={7-11,14-23} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "rate_card": {
      "name": "mistral-small input USD",
      "code": "mistral_small_input_usd",
      "product_code": "mistral_tokens_product",
      "product_filter_code": "mistral_small_input",
      "currency": "USD",
      "billing_timing": "arrears",
      "proration": false,
      "rates": [
        {
          "code": "mistral_small_input_standard",
          "effective_from": "2027-01-01T00:00:00Z",
          "rate_model": "package",
          "rate_properties": {
            "amount": "1",
            "package_size": 1000000,
            "free_units": 0
          },
          "billing_interval_count": 1,
          "billing_interval_unit": "month"
        }
      ]
    }
  }'
```

Repeat for each filter:

| Rate card `code` | `product_filter_code` | `amount` per 1,000,000 tokens |
| - | - | - |
| `mistral_small_input_usd` | `mistral_small_input` | `"1"` |
| `mistral_small_output_usd` | `mistral_small_output` | `"3"` |
| `mistral_medium_input_usd` | `mistral_medium_input` | `"2.7"` |
| `mistral_medium_output_usd` | `mistral_medium_output` | `"8.1"` |
| `mistral_large_input_usd` | `mistral_large_input` | `"4"` |
| `mistral_large_output_usd` | `mistral_large_output` | `"12"` |

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/rate-cards/create.md) and the [package model](https://getlago.com/docs/v2/guide/rate-cards/pricing-models/package.md).

**6. Create a plan and apply the rate cards**

```bash cURL highlight={6-7} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/plans" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "plan": {
      "name": "Mistral pay as you go",
      "code": "mistral_payg",
      "currency": "USD"
    }
  }'
```

Apply each of the six rate cards:

```bash cURL highlight={6} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/plans/mistral_payg/applied_rate_cards" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "applied_rate_card": {
      "rate_card_code": "mistral_small_input_usd"
    }
  }'
```

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/plan-applied-rate-cards/create.md) and the [plans guide](https://getlago.com/docs/v2/guide/plans/overview.md).

**7. Create a customer**

```bash cURL highlight={6} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/customers" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "customer": {
      "external_id": "customer_example",
      "name": "Acme Corp",
      "email": "billing@example.com",
      "currency": "USD"
    }
  }'
```

An existing `external_id` returns `422` instead of updating the customer. Refer to the [API reference](https://getlago.com/docs/v2/api-reference/customers/create.md) to create a customer.

**8. Create a contract**

Replace `__PLAN_CODE__` with `mistral_payg`.

```bash cURL highlight={6-10} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/contracts" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "contract": {
      "external_id": "contract_example",
      "external_customer_id": "customer_example",
      "plan_code": "__PLAN_CODE__",
      "started_at": "2027-01-01T00:00:00Z",
      "billing_anchor_date": "2027-02-01"
    }
  }'
```

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](https://getlago.com/docs/v2/api-reference/contracts/create.md) to create a contract.

> [!NOTE]
>
> 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 with `external_contract_id`.

```bash cURL highlight={7-12} theme={"dark"}
curl --location --request POST "$LAGO_URL/api/v2/events" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json' \
  --data-raw '{
    "event": {
      "transaction_id": "__TRANSACTION_ID__",
      "code": "mistral_tokens",
      "external_contract_id": "contract_example",
      "properties": {
        "tokens": 2000000,
        "model": "mistral-small",
        "type": "input"
      }
    }
  }'
```

Refer to the [API reference](https://getlago.com/docs/v2/api-reference/events/create.md) and the [usage ingestion guide](https://getlago.com/docs/v2/guide/events/ingesting-usage.md).

**10. Monitor current usage**

Track usage across the contract's open billing periods.

```bash cURL highlight={1} theme={"dark"}
curl --location --request GET "$LAGO_URL/api/v2/contracts/contract_example/current_usage" \
  --header "Authorization: Bearer $API_KEY" \
  --header 'Content-Type: application/json'
```

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](https://getlago.com/docs/v2/api-reference/contracts/retrieve-current-usage.md) to get current usage.

## Worked example

Over one full monthly period, the customer sends 2,000,000 mistral-small input tokens and 3,000,000 mistral-large output tokens.

| Invoice line | Quantity | Packages | Price | Amount |
| - | - | - | - | - |
| mistral-small, input tokens | 2,000,000 tokens | 2 | \$1 per 1M | \$2.00 |
| mistral-large, output tokens | 3,000,000 tokens | 3 | \$12 per 1M | \$36.00 |
| **Total before tax** | | | | **\$38.00** |

## What changed from v1

* The v1 template prepaid usage with a wallet. The v2 API does not document wallets, so this template omits prepaid credits.
* Charge filters become product filters, each priced by its own rate card.
* Events target `external_contract_id` instead of `external_subscription_id`.

> [card] [Product filters](https://getlago.com/docs/v2/guide/product-catalog/product-filters.md)
>
> Price subsets of metered usage separately.

> [card] [Rates](https://getlago.com/docs/v2/guide/rate-cards/rates.md)
>
> Schedule price changes with effective dates.

> [card] [Contracts](https://getlago.com/docs/v2/guide/contracts/overview.md)
>
> Apply a plan to a customer.

> [card] [Send an event (API)](https://getlago.com/docs/v2/api-reference/events/create.md)
>
> `POST /api/v2/events`


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.