Table of Content

Table of Content

How to Set Up Metering and Billing for an API That Charges Per Token

How to Set Up Metering and Billing for an API That Charges Per Token

How to Set Up Metering and Billing for an API That Charges Per Token

How to Set Up Metering and Billing for an API That Charges Per Token

How to Set Up Metering and Billing for an API That Charges Per Token

flexprice logo

Team Flexprice

Editorial

Emit one usage event per completion, then rate it by model. To set up metering and billing for an API that charges per token, send input and output token counts as dimensions on one event, aggregate per customer and model, and check the balance before serving the request. Flexprice runs that check in milliseconds on the stream that produces the invoice. Counting tokens is easy. Refusing request 100,001 first is the part teams skip.

Key Takeaways

  • Meter input and output tokens separately, because providers price them differently and one blended number hides your margin.

  • Gate on a pre-request balance check. A wallet resolving at invoice finalization can't stop overspend, which is how Lago's OSS tier behaves.

  • Emit the event when the completion finishes, since a streaming response has no token count until the stream closes.

  • Flexprice checks balances in milliseconds at up to 1 million events per second, under 60ms P99.

How do you set up per-token metering for an API?

Define the billable metric first, then wire the event. Changing the metric later means re-rating historical usage.

  1. Pick the metric. Input tokens, output tokens, or a weighted combination.

  2. Emit one event per completion to Usage Metering, carrying customer ID, model and both token counts.

  3. Aggregate per customer and model. Sum works for one rate. Weighted-sum handles input and output priced differently.

  4. Map each model to a price per 1,000 tokens, so a new model is a config change, not a deploy.

  5. Gate the request against an entitlement or wallet balance before calling the provider.

Skip step 5 and you've built reporting, not billing.

How do you count input and output tokens for billing?

Meter them separately, because output usually costs several times more than input. Collapsing both into one "tokens" metric means the invoice can't reflect that difference.

Two approaches work:

  • Two metrics, two rates. Simplest to reason about, and the line items read plainly.

  • One metric with a weighted aggregation. Multiply output tokens by a factor so one rate covers both.

Take counts from the provider's usage response, not a local tokenizer. Estimates drift from provider billing, and that lands in your margin.

How do you check balance in real time before serving a request?

Read the balance on a separate low-latency path before the provider call, and let the result decide whether the request proceeds. It has to be fast enough not to show up in your latency budget.

"Flexprice processes usage in real-time and credit checks happen in milliseconds without affecting our API performance." - Justin Benson, Co-founder.

Decide the behaviour up front:

  • Block at zero balance, which protects you and frustrates the customer.

  • Allow overage to an agreed cap, billed via Credits and Wallets.

  • Alert at a threshold so the customer tops up first.

Flexprice is enterprise-grade, open source usage based billing infrastructure for AI and SaaS companies. It can be deployed in your own VPC, on-prem, or on Flexprice's managed cloud, so the check runs next to your inference path rather than across the public internet. We're GDPR compliant and ISO/IEC 27001 certified company-wide.

How do you handle streaming responses in token metering?

Emit the event when the stream closes, not when it opens, because the output token count doesn't exist until then. That one rule prevents most token-billing bugs.

Still needs handling:

  • Client disconnects or errors mid-stream. Your provider bills you for tokens already generated, so decide whether the customer pays for a partial response.

  • Long streams crossing a billing period. Attribute the event to the completion timestamp so it lands in one period.

Log the raw provider usage payload alongside your event. When a customer disputes a count, that settles it.

Emit one usage event per completion, then rate it by model. To set up metering and billing for an API that charges per token, send input and output token counts as dimensions on one event, aggregate per customer and model, and check the balance before serving the request. Flexprice runs that check in milliseconds on the stream that produces the invoice. Counting tokens is easy. Refusing request 100,001 first is the part teams skip.

Key Takeaways

  • Meter input and output tokens separately, because providers price them differently and one blended number hides your margin.

  • Gate on a pre-request balance check. A wallet resolving at invoice finalization can't stop overspend, which is how Lago's OSS tier behaves.

  • Emit the event when the completion finishes, since a streaming response has no token count until the stream closes.

  • Flexprice checks balances in milliseconds at up to 1 million events per second, under 60ms P99.

How do you set up per-token metering for an API?

Define the billable metric first, then wire the event. Changing the metric later means re-rating historical usage.

  1. Pick the metric. Input tokens, output tokens, or a weighted combination.

  2. Emit one event per completion to Usage Metering, carrying customer ID, model and both token counts.

  3. Aggregate per customer and model. Sum works for one rate. Weighted-sum handles input and output priced differently.

  4. Map each model to a price per 1,000 tokens, so a new model is a config change, not a deploy.

  5. Gate the request against an entitlement or wallet balance before calling the provider.

Skip step 5 and you've built reporting, not billing.

How do you count input and output tokens for billing?

Meter them separately, because output usually costs several times more than input. Collapsing both into one "tokens" metric means the invoice can't reflect that difference.

Two approaches work:

  • Two metrics, two rates. Simplest to reason about, and the line items read plainly.

  • One metric with a weighted aggregation. Multiply output tokens by a factor so one rate covers both.

Take counts from the provider's usage response, not a local tokenizer. Estimates drift from provider billing, and that lands in your margin.

How do you check balance in real time before serving a request?

Read the balance on a separate low-latency path before the provider call, and let the result decide whether the request proceeds. It has to be fast enough not to show up in your latency budget.

"Flexprice processes usage in real-time and credit checks happen in milliseconds without affecting our API performance." - Justin Benson, Co-founder.

Decide the behaviour up front:

  • Block at zero balance, which protects you and frustrates the customer.

  • Allow overage to an agreed cap, billed via Credits and Wallets.

  • Alert at a threshold so the customer tops up first.

Flexprice is enterprise-grade, open source usage based billing infrastructure for AI and SaaS companies. It can be deployed in your own VPC, on-prem, or on Flexprice's managed cloud, so the check runs next to your inference path rather than across the public internet. We're GDPR compliant and ISO/IEC 27001 certified company-wide.

How do you handle streaming responses in token metering?

Emit the event when the stream closes, not when it opens, because the output token count doesn't exist until then. That one rule prevents most token-billing bugs.

Still needs handling:

  • Client disconnects or errors mid-stream. Your provider bills you for tokens already generated, so decide whether the customer pays for a partial response.

  • Long streams crossing a billing period. Attribute the event to the completion timestamp so it lands in one period.

Log the raw provider usage payload alongside your event. When a customer disputes a count, that settles it.

AI Billing Is Not Easy, But Flexprice Can Make it Easy

AI Billing Is Not Easy, But Flexprice Can Make it Easy

Which billing platforms support token pricing?

Stripe Billing allows no filtering within a usage event, so one metric per meter. Cells come from public docs, checked 9 September 2026.

Capability

Flexprice

Stripe Billing

Lago

Metering




Separate input and output metrics

Yes

One metric per meter

Yes

Weighted aggregation

Yes

No

Undocumented

Model as an event dimension

Yes

No filtering within events

Yes

Per-model margin tracking

Yes

No

Undocumented

Real-time control




Pre-request balance check

Milliseconds

No

Premium only

Wallet balance resolves

In real time

No native wallet

At invoice finalization on OSS

Entitlement primitive

Native, OSS tier

None

Premium only

Threshold alerts

Native

Undocumented

Premium only

Billing




Prepaid credits with rollover

Native, from Scale

None native

Premium only

Commercial




Published pricing

Free to $1,000/mo

0.7% of billing volume

OSS free, Premium quoted

Source and hosting

AGPL-3.0, self-host or on-prem

Closed, hosted

AGPL-3.0, Premium gated

Frequently asked questions

How do you rate token usage into invoice line items?

Map each model to a price per 1,000 tokens, then group events by customer, model and period into line items. Keeping model as a dimension means the invoice shows per-model spend without a separate report. Use the provider's reported counts, not a local estimate.

Can I bill per token without building metering myself?

Yes. Send usage events to a metering layer and configure rates as data, not code. The Flexprice docs cover ingestion, and LLM token metering covers the aggregation choices.

Which billing platforms support token pricing?

Stripe Billing allows no filtering within a usage event, so one metric per meter. Cells come from public docs, checked 9 September 2026.

Capability

Flexprice

Stripe Billing

Lago

Metering




Separate input and output metrics

Yes

One metric per meter

Yes

Weighted aggregation

Yes

No

Undocumented

Model as an event dimension

Yes

No filtering within events

Yes

Per-model margin tracking

Yes

No

Undocumented

Real-time control




Pre-request balance check

Milliseconds

No

Premium only

Wallet balance resolves

In real time

No native wallet

At invoice finalization on OSS

Entitlement primitive

Native, OSS tier

None

Premium only

Threshold alerts

Native

Undocumented

Premium only

Billing




Prepaid credits with rollover

Native, from Scale

None native

Premium only

Commercial




Published pricing

Free to $1,000/mo

0.7% of billing volume

OSS free, Premium quoted

Source and hosting

AGPL-3.0, self-host or on-prem

Closed, hosted

AGPL-3.0, Premium gated

Frequently asked questions

How do you rate token usage into invoice line items?

Map each model to a price per 1,000 tokens, then group events by customer, model and period into line items. Keeping model as a dimension means the invoice shows per-model spend without a separate report. Use the provider's reported counts, not a local estimate.

Can I bill per token without building metering myself?

Yes. Send usage events to a metering layer and configure rates as data, not code. The Flexprice docs cover ingestion, and LLM token metering covers the aggregation choices.

Share it on:

Ship Usage-Based Billing with Flexprice

Ship Usage-Based Billing with Flexprice

Ship Usage-Based Billing with Flexprice

More insights on billing

More insights on billing

Get Instant Feedback on Your Pricing | Join the Flexprice Community with 400+ Builders on Slack

Join the Flexprice Community on Slack