Skip to content
Create account or Sign in
The Stripe Docs logo
/
Ask AI
Create accountSign in
Get started
Payments
Revenue
Platforms and marketplaces
Money management
Developer resources
APIs & SDKsHelp
Overview
Billing
OverviewAbout the Billing APIs
Subscriptions
Invoicing
Usage-based billing
    Overview
    How usage-based billing works
    Use cases
      Pay as you go
      Flat fee and overages
      Credit-based pricing model
    Record usage for billing
    Offer billing credits
    Monitor usage
    Manage usage-based billing setup
    Advanced usage-based billing
    Billing for LLM tokens
Quotes
Customer management
Billing with other products
Revenue recovery
Automations
Test your integration
Tax
Overview
Use Stripe tax
Manage compliance
Reporting
Overview
Select a report
Configure reports
Reports for multiple accounts
Reports API
Revenue recognition
Data
Overview
Query business data
Sigma
Data Pipeline
Import external data
United States
English (United States)
HomeRevenueUsage-based billingUse cases

Set up a pay-as-you-go pricing model

Charge customers based on their usage of your product or service.

Integrate in VS Code

Pay-as-you-go pricing is a flexible, scalable model that lets you charge customers in arrears for the usage they accrue. AI businesses, SaaS platforms, and cloud services often use this pricing model.

What you’ll build

This guide describes how to implement pay-as-you-go pricing on Stripe for a fictional company called Llama.ai. Llama AI charges their customers the following rates for their LLM models:

Usage Fee
Token0.04 USD per 100 tokens

To implement this pricing model, you create a meter, set up pricing and billing, and send meter events to record customer usage using Products and Prices.

Create a meter

Meters specify how to aggregate meter events over a billing period. Meter events represent all actions that customers take in your system (for example, API requests). Meters attach to prices and form the basis of what’s billed.

For the Alpaca AI example, meter events are the number of tokens a customer uses in a query. The meter is the sum of tokens over a month.

You can use the Stripe Dashboard or API to configure a meter. To use the API with the Stripe CLI to create a meter, get started with the Stripe CLI.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/billing/meters \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d display_name="Alpaca AI tokens" \ -d event_name=alpaca_ai_tokens \ -d "default_aggregation[formula]"=sum \ -d "customer_mapping[event_payload_key]"=stripe_customer_id \ -d "customer_mapping[type]"=by_id \ -d "value_settings[event_payload_key]"=value

Create a pricing model

Use the Stripe Dashboard or API to create a pricing model that includes your Products and their pricing options. Prices define the unit cost, currency, and billing period.

For the Alpaca AI example, you create a product with a metered price of 0.04 USD per hundred units, billed at a monthly interval. You use the meter that you created in the previous step.

You can locate your meter ID on the meter details page.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/prices \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d currency=usd \ -d unit_amount=4 \ -d billing_scheme=per_unit \ -d "transform_quantity[divide_by]"=100 \ -d "transform_quantity[round]"=up \ -d "recurring[usage_type]"=metered \ -d "recurring[interval]"=month \ -d "recurring[meter]"={{METER_ID}} \ -d "product_data[name]"="Alpaca AI tokens"

Create a customer

Next, create a customer.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/customers \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d name="John Doe"

Create a subscription

Subscriptions allow you to charge recurring amounts by associating a customer with a specific price.

Use the Stripe Dashboard or API to create a subscription that includes your customer, product, and usage-based price.

For the Alpaca AI example, you create a subscription for the Alpaca AI product, with a metered price of 0.04 USD per 100 units, billed monthly to John Doe.

Note

You can associate a single metered price with one or more subscriptions.

When you create a billing_mode=flexible subscription, Stripe excludes metered line items from the first invoice since no prior usage exists to bill. Stripe creates an invoice only if the subscription is backdated with previously accrued usage or if pending invoice items exist. When you create a billing_mode=classic subscription, Stripe generates a zero monetary value invoice line item for each metered subscription item.

You can locate your customer ID on the customer details page. To locate your price ID, go to the product details page and click the overflow menu () under Pricing. Select Copy price ID.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/subscriptions \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d customer={{CUSTOMER_ID}} \ -d "items[0][price]"={{PRICE_ID}}

Send a test meter event

Use Meter Events to record customer usage for your meter. At the end of the billing period, Stripe bills the reported usage.

You can test your usage-based billing by sending a meter event through the Stripe Dashboard or API. When using the API, specify the customer ID and value for the payload.

After you send meter events, you can view usage details for your meter on the Meters page in the Dashboard.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/billing/meter_events \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d event_name=alpaca_ai_tokens \ -d "payload[stripe_customer_id]"={{CUSTOMER_ID}} \ -d "payload[value]"=25

Create a preview invoice

Create a preview invoice to see a preview of a customer’s invoice that includes details such as the meter price and usage quantity.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl https://api.stripe.com/v1/invoices/create_preview \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d subscription=
"{{SUBSCRIPTION_ID}}"

OptionalRetrieve usage for a custom time period

Use the Meter Event Summary to retrieve total usage for a custom time period. The meter event summary returns a customer’s aggregated usage for a period, based on the aggregation formula defined by the meter.

In the Alpaca AI example, the meter event summary returns the sum of tokens for a specific customer, meter, and time window.

Because Stripe processes meter events asynchronously, meter event summaries might not immediately reflect recently received meter events.

Command Line
cURL
Stripe CLI
Ruby
Python
PHP
Java
Node.js
Go
.NET
No results
curl -G https://api.stripe.com/v1/billing/meters/{{METER_ID}}/event_summaries \ -u "
sk_test_BQokikJOvBiI2HlWgH4olfQ2
:"
\ -d customer={{CUSTOMER_ID}} \ -d start_time=1717249380 \ -d end_time=1717249440

Next steps

  • Accept payments with Stripe Checkout
  • Set up alerts and thresholds
  • Set up billing credits
Was this page helpful?
YesNo
  • Need help? Contact Support.
  • Check out our changelog.
  • Questions? Contact Sales.
  • LLM? Read llms.txt.
  • Powered by Markdoc