Introducing the Chargebee CLI

Prepaid credits: Offer shared credit pool and bill overages

Overview

If your business sells prepaid credits where every product draws from a common pool, and you invoice customers for overages beyond the grant, this guide shows you how to build that model in Chargebee Billing. You define one credit unit. Each plan grants a balance of that unit. Every product spends from the same balance, at its own rate. A metered addon invoices the credits consumed past the grant.

This guide is for billing administrators and developers who are implementing the model end-to-end in the Chargebee Billing app and with the API. See Usage-based billing use cases for other billing cadence combinations.

Throughout this guide, you configure the model for a single running example. Acme Inc. sells three products: Article generation, Image generation, and Video generation that draw down from a common credit balance called AI Credits. Each plan decides how large that balance is.

John Doe subscribes to Growth: $12,000 per year, with 10,000 AI Credits granted each month. Article generation draws 1 AI Credit, image generation draws 10, and video generation draws 50. Unused credits expire at the end of the grant period. If John uses 12,400 AI Credits in a month, the 2,400 credits past the grant are invoiced that month at $0.10 each ($240). Enterprise is $30,000 per year, with 50,000 AI Credits granted each month, and uses the same overage rate.

Use Prepaid credits: Offer shared credit pool for all the products when the balance should stop at zero and excess usage should not be invoiced. Use this guide when consumption past the grant should be invoiced.

How this billing model works

The illustration below explains how this billing model works:

Before you start

If you don't already have a Chargebee Billing account, click here to sign up.

Confirm your access. You need an admin role to change site configuration and build the Product Catalog.

Advanced Usage-Based Billing is enabled by default for new Chargebee Billing sites. If you are an existing user, enable it before you build the model.

Multi-Frequency Billing is enabled by default for new Chargebee Billing sites. If you are an existing user, enable it before you build the model.

Prepaid Credits is in early access. Contact Chargebee Support to request access before you build the model.

Validate before go-live

Before enabling this on a live site, validate one grant cycle on a test site with Time Machine. Create a Growth subscription with the overage addon attached. Confirm the monthly grant of 10,000 AI Credits. Capture article, image, and video debits that stay inside the grant, then capture debits that take the month past 10,000. Advance to the next month and confirm an overage invoice for the excess at $0.10 per AI Credit, and a fresh grant of 10,000 AI Credits. A test site offers a one-year Time Machine option when you also want to validate annual renewal.

When you enter Time Machine, existing subscriptions and customer details on the test site are erased. Export the data first if you need a backup.

When you are ready to go live, copy the configuration and Product Catalog from the test site to the live site with Transfer Configurations. To transfer customer, subscription, or invoices data, you can import them on your live site.

Implementation steps

Follow these steps in order. Each step shows how to do the task in the Chargebee Billing app and with the API.

1. Define the credit unit

A credit unit is the named unit or currency your customers spend. Create it once. Grants, captures, and credit-balance alerts all use this unit. Article generation, image generation, and video generation share it.

Consumption past the grant has to be allowed, or the overage addon has nothing to invoice. This example does not put a fixed cap on that consumption. The overage addon prices every AI Credit past the monthly grant.

Using the app
  1. Click Settings > Configure Chargebee > Credit Units.
  2. Select Create a credit unit.
  3. Fill in these fields.
  4. Click Create.
FieldValue
External nameAI Credits
Internal nameAI Credits
Credit unit IDai_credits
Allowed negative balanceBlank. Do not enter a fixed cap.

A blank Allowed negative balance is documented as no negative balance for customers without on-demand credit pricing. Confirm the saved credit unit has uncapped consumption past the grant (is_unlimited = true) before you attach the overage addon. Create the credit unit with the API when you need that setting.

Using the API

Create the credit unit with Create a credit unit. Set is_unlimited to true so consumption can continue with no cap after the allocated grants are exhausted. Omit overdraft_amount. The overage addon bills that consumption.

curl  https://{site}.chargebee.com/api/v2/credit_units \
     -u {site_api_key}:\
     -d id="ai_credits" \
     -d name="AI Credits" \
     -d external_name="AI Credits" \
     -d is_unlimited=true

Set is_unlimited to false and pass overdraft_amount only when you want a cap on how many credits a customer can consume past the grant. overdraft_amount is a positive decimal string. That cap is the Allowed negative balance field in the app, and it applies to customers without on-demand credit pricing.

2. Define the credit grants for your plans

The plan is the yearly fee. The credit grant on the plan price is the monthly allowance and the rollover policy. Growth and Enterprise grant different quantities of the same credit unit.

2a. Create the plans

Create Growth and Enterprise. You link pricing and the credit grant in the next steps.

Using the app
  1. Go to Product Catalog > Plans > + Create Plan.
  2. Select a product family, enter the plan details, and click Create. Repeat for the second plan.
Internal NamePlan ID
Growthgrowth
Enterpriseenterprise
Using the API

Create the plan item with Create an item. Replace {item_family_id} with your product family ID.

curl  https://{site}.chargebee.com/api/v2/items \
     -u {site_api_key}:\
     -d id="growth" \
     -d name="Growth" \
     -d type="PLAN" \
     -d item_family_id="{item_family_id}"

Create Enterprise with id="enterprise" and name="Enterprise".

2b. Define the plan price points

Once each plan is saved, create a yearly price point for it. Growth is $12,000. Enterprise is $30,000.

Using the app
  1. Open the plan details page. In the Pricing section, click Set Price for the yearly frequency and currency you want.
  2. Configure the pricing as follows, then click Create.
FieldGrowthEnterprise
Pricing modelFlat feeFlat fee
Price$12,000$30,000
Billing frequencyYearlyYearly
Using the API

Create the yearly price point with Create an item price. price is in the minor unit of the currency: 1200000 is $12,000.00.

curl  https://{site}.chargebee.com/api/v2/item_prices \
     -u {site_api_key}:\
     -d id="growth-USD-yearly" \
     -d item_id="growth" \
     -d name="Growth USD yearly" \
     -d pricing_model="FLAT_FEE" \
     -d price=1200000 \
     -d currency_code="USD" \
     -d period_unit="YEAR" \
     -d period=1

Create the Enterprise yearly price the same way. Use id="enterprise-USD-yearly", item_id="enterprise", name="Enterprise USD yearly", and price=3000000.

2c. Configure the monthly credit grant and rollover

Grant frequency can be shorter than the billing frequency. These plans bill yearly and issue credits monthly. Both grants use AI Credits. Only the quantity differs.

SettingGrowthEnterprise
Credit unitAI Credits. This cannot be changed after the grant is saved.AI Credits. This cannot be changed after the grant is saved.
Grant quantity10,00050,000
Grant frequencyMonthlyMonthly
RolloverNo rollover. Unused credits expire at the end of the grant period.No rollover. Unused credits expire at the end of the grant period.
Using the app
  1. Open the Growth yearly item price.
  2. In the Credit Grant section, click Configure Grant.
  3. Select AI Credits, set the grant quantity to 10,000, set the grant frequency to Monthly, and select No rollover so unused credits expire at the end of the grant period. Click Save.
  4. Repeat for the Enterprise yearly item price, with a grant quantity of 50,000.
Using the API

The recurring grant is saved on the item price in the app. Create an item price and Update an item price do not accept credit unit, grant quantity, grant frequency, or rollover. Chargebee Billing reads the grant you saved with Configure Grant and issues it when the customer subscribes.

Growth issues 10,000 AI Credits each month. Enterprise issues 50,000. With No rollover, unused credits expire at the end of the grant period.

Edits to a saved grant create a new version. Existing subscriptions keep the grant version they already have. See Prepaid credits.

3. Define how to bill overages past the credit grant

Metered addons are catalog items that let you measure and rate metered features beyond the included usage granted through the plan. In this case, the addon bills AI Credits consumed after the monthly grant is exhausted. Customers are charged the overage rate only after they exceed their allocated credit grant.

3a. Create the overage addon

Using the app
  1. Click Product Catalog > Addons > Create a new addon.
  2. Enter the addon details.
FieldValue
Internal nameOn-demand AI Credits
Addon IDon-demand-ai-credits
  1. Select This is a metered addon billing on-demand usage.
  2. Under How would you want to use this metered addon?, select Bill overage for credit grant.
  3. Under Choose credit unit of the grant, select AI Credits.
  4. Save the addon.
Using the API

Create the metered addon with Create an item. Set metered to true. Replace {item_family_id} with your product family ID.

curl  https://{site}.chargebee.com/api/v2/items \
     -u {site_api_key}:\
     -d id="on-demand-ai-credits" \
     -d name="On-demand AI Credits" \
     -d type="ADDON" \
     -d item_family_id="{item_family_id}" \
     -d metered=true

This request does not accept the credit-grant overage model or the credit unit. After it succeeds, open the addon in the app, select Bill overage for credit grant, and select AI Credits.

3b. Define the addon price point

Once the addon is created, create a monthly price point for it. Growth and Enterprise share this price. The plan only changes how many credits were granted.

Using the app
  1. Open the addon details page. In the Pricing section, click Set Price for the monthly frequency.
  2. Configure the pricing as follows, then click Create.
FieldValue
Pricing modelPer Unit
Price$0.10 per AI Credit
Billing frequencyMonthly

With Multi-Frequency Billing, only on-demand addons with per-unit pricing are supported for included usage and overages.

Using the API

Create the monthly price point with Create an item price. price is in the minor unit of the currency: 10 is $0.10. Select Bill overage for credit grant and AI Credits on the addon in the app before you create this price, so the price bills that credit unit.

curl  https://{site}.chargebee.com/api/v2/item_prices \
     -u {site_api_key}:\
     -d id="on-demand-ai-credits-USD-monthly" \
     -d item_id="on-demand-ai-credits" \
     -d name="On-demand AI Credits USD monthly" \
     -d pricing_model="PER_UNIT" \
     -d price=10 \
     -d currency_code="USD" \
     -d period_unit="MONTH" \
     -d period=1

4. Draw down the shared credit pool

When the customer uses a product, draw credits from the shared pool in one of two ways.

  • Capture when you already know how many credits the action uses. Capture moves credits from the usable balance to consumed and writes an immutable ledger operation.
  • Authorize, then capture when a job is still in progress and the final amount is not known yet. Authorize moves credits from the usable balance to a held amount. Other operations cannot spend held credits, and nothing is consumed yet. When the job finishes, Capture authorization converts the held credits into consumed credits. If the job used fewer credits than you reserved, capture only the amount used. The unused remainder returns to the usable balance. If the job is canceled, Release authorization returns the entire hold to the usable balance.

Pass amount as a decimal string. ledger_operation_timestamp is a Unix timestamp in seconds.

Compute the credit amount in your application, then send that amount to Chargebee Billing. Your application decides how many credits to draw from the ledger balance for each feature. For example, one article generation draws 1 AI Credit, one image generation draws 10 AI Credits, and one video generation draws 50 AI Credits.

Article generation, image generation, and video generation use the same credit unit, ai_credits. One article sends amount "1". One image sends amount "10". One video sends amount "50". Each call draws from the same balance. The plan only changes how many credits were granted.

Credits within the monthly grant are covered by the plan fee. With is_unlimited set to true on the credit unit, Capture can continue after that grant is exhausted. Those additional credits are invoiced on the On-demand AI Credits addon at $0.10 each. On Growth, a month that consumes 12,400 AI Credits invoices 2,400 credits ($240). On Enterprise, overages start after 50,000 AI Credits.

Using the API

Capture one generated image when you already know it draws 10 AI Credits. Use the same request for an article, with amount set to "1", or for a video, with amount set to "50", and a new id.

curl  https://{site}.chargebee.com/api/v2/ledger_operations/capture \
     -u {site_api_key}:\
     -d id="op-image-0001" \
     -d subscription_id="{subscription_id}" \
     -d unit_id="ai_credits" \
     -d amount="10" \
     -d ledger_operation_timestamp=1737612931

See Capture. The capture amount is evaluated against the current usable balance. After the provisioned grant is exhausted, further consumption draws from the overdraft balance.

Authorize, then capture the same image when the job must finish before you know the final amount. Authorize holds 10 AI Credits. Set auto_release_timestamp when the hold needs to last longer than the default. If you never capture or release the hold, it returns to the usable balance when it expires.

curl  https://{site}.chargebee.com/api/v2/ledger_operations/authorize \
     -u {site_api_key}:\
     -d id="op-image-0001-hold" \
     -d subscription_id="{subscription_id}" \
     -d unit_id="ai_credits" \
     -d amount="10" \
     -d ledger_operation_timestamp=1737612931 \
     -d auto_release_timestamp=1737616531

When the image is generated, capture the authorization. Pass the authorize operation id as authorization_id.

curl  https://{site}.chargebee.com/api/v2/ledger_operations/capture_authorization \
     -u {site_api_key}:\
     -d id="op-image-0001-capture" \
     -d authorization_id="op-image-0001-hold" \
     -d amount="10" \
     -d ledger_operation_timestamp=1737613200

See Authorize and Capture authorization.

At the end of the grant period, any credits that are still unused are expired. Fresh credits are granted for the new period. Overage credits already captured in the closed period remain on that period's monthly addon invoice.

5. Take action on a defined threshold using alerts

Use a credit alert to watch the shared AI Credits balance, and a spend alert to watch the dollar amount invoiced past the grant.

Create credit alerts

A credit alert fires when the shared balance drops to or below a number of AI Credits. It is a site-level rule. Chargebee Billing evaluates it as ledger operations update the balance. The threshold is absolute, so the same alert fires at that floor on Growth and on Enterprise. An alert at 2,000 remaining AI Credits is an early signal, before overages start. An alert at 0 marks an empty grant.

Using the app
  1. Go to Usages > Alerts and click Create Alert.
  2. Enter the alert name AI credits low and a description.
  3. Select the alert type as Credit alert.
  4. Set the threshold to 2,000 remaining AI Credits.
  5. Under Apply alert to, choose All subscriptions. That applies the same floor on Growth and on Enterprise. Filter to one plan price only when the alert should not cover every plan.
  6. Click Manage connected webhooks and select the endpoint that will receive alert_status_changed.
  7. Click Save Alert.
  8. Repeat with a threshold of 0 and the name AI credits exhausted.
Using the API

Create a credit_balance_dropped alert on ai_credits with Create an alert. threshold[mode] is absolute.

curl  https://{site}.chargebee.com/api/v2/alerts \
     -u {site_api_key}:\
     -d type="credit_balance_dropped" \
     -d name="AI credits low" \
     -d description="Notify when AI Credits drop to 2000" \
     -d unit_id="ai_credits" \
     -d "threshold[mode]"="absolute" \
     -d "threshold[value]"=2000

Do not send metered_feature_id or currency_code on this alert type. Create a second alert with "threshold[value]"=0 and name="AI credits exhausted" when you want a signal that the grant is used up and further credits will be invoiced. To limit an alert to one plan price, pass filter_conditions for plan_price_id. To scope an alert to one subscription, pass subscription_id and omit filter_conditions.

Create spend alerts

A spend alert fires when accrued overage charges on the On-demand AI Credits addon reach a dollar amount. The threshold is an absolute amount in the major units of the currency: 200 is $200. At $0.10 per AI Credit, $200 is 2,000 credits past the grant. The alert does not cap that spend. The addon still invoices every credit past the grant.

Using the app
  1. Go to Usages > Alerts and click Create Alert.
  2. Enter the alert name AI credits overage spend and a description.
  3. Select the alert type as Spend alert and set the currency to USD.
  4. Set the spend threshold to $200.
  5. Under Apply alert to, choose All subscriptions. Filter to one plan price only when the alert should not cover every plan.
  6. Click Manage connected webhooks and select the endpoint that will receive alert_status_changed.
  7. Click Save Alert.
Using the API

Create a spend_exceeded alert with Create an alert. Provide currency_code. threshold[mode] is absolute.

curl  https://{site}.chargebee.com/api/v2/alerts \
     -u {site_api_key}:\
     -d type="spend_exceeded" \
     -d name="AI credits overage spend" \
     -d description="Notify when overage spend reaches 200 USD" \
     -d currency_code="USD" \
     -d "threshold[mode]"="absolute" \
     -d "threshold[value]"=200

Do not send unit_id or metered_feature_id on this alert type. To limit an alert to one plan price, pass filter_conditions for plan_price_id. To scope an alert to one subscription, pass subscription_id and omit filter_conditions.

Be notified when the threshold is reached

When a credit balance drops to the floor you set, or when overage spend reaches the amount you set, the alert status changes between within_limit and in_alarm. Chargebee sends an alert_status_changed webhook. Use that webhook to email the customer, show an upgrade prompt, or start another workflow. For example, when remaining AI Credits drop to 2,000, prompt an upgrade from Growth to Enterprise. When overage spend reaches $200, tell the customer that further credits are invoiced at $0.10 each.

Using the app
  1. Go to Settings > Configure Chargebee > API Keys and Webhooks.
  2. Open the Webhooks tab.
  3. Add or select the endpoint that should receive alert_status_changed.

See Webhook Settings.

6. Create the customer and subscription

With the catalog and the credit-balance alert in place, create the customer and their subscription. A customer record must exist before a subscription can be created. Create the subscription for Acme's contact, John Doe, on the Growth yearly plan, and attach the On-demand AI Credits monthly addon. The plan grant of 10,000 AI Credits per month is provisioned when the customer subscribes. Without the addon on the subscription, consumption past the grant is not invoiced.

Using the app
  1. Go to Customers and create a customer record for John Doe (Acme Inc.) if one doesn't already exist.
  2. From the customer, select Create a subscription.
  3. Select the product family and the Growth yearly plan. The subscription shows the credit grant of 10,000 AI Credits per month.
  4. Attach the On-demand AI Credits monthly addon.
  5. Select Create.

To raise the grant for this subscription only, open the three-dot menu next to the plan and select Manage Credit Grants. Leave the credit unit unchanged. The override applies only to this subscription. The Growth default of 10,000 stays in place for every other customer. See Override credit grant settings for a specific subscription.

Using the API

Create the customer with Create a customer:

curl  https://{site}.chargebee.com/api/v2/customers \
     -u {site_api_key}:\
     -d first_name="John" \
     -d last_name="Doe" \
     -d company="Acme Inc." \
     -d email="jdoe@example.com"

Create the subscription with Create a subscription for items. Attach both the yearly plan price and the monthly overage addon price:

curl  https://{site}.chargebee.com/api/v2/customers/{customer_id}/subscription_for_items \
     -u {site_api_key}:\
     -d "subscription_items[item_price_id][0]"="growth-USD-yearly" \
     -d "subscription_items[item_price_id][1]"="on-demand-ai-credits-USD-monthly"

The grant quantity override is set in the app with Manage Credit Grants while you create the subscription.

Your active subscription looks like this:

Line itemBilling frequencyWhat the customer receives
Growth (non-metered)Yearly$12,000 invoiced for the year. 10,000 AI Credits granted each month. Article generation, image generation, and video generation draw from that balance. Unused credits expire at the end of the grant period.
On-demand AI Credits (metered)Monthly$0.10 per AI Credit invoiced monthly, only after the monthly grant is exhausted. A month with 12,400 AI Credits on Growth invoices 2,400 credits ($240).

7. Monitor prepaid credits and overages during the term

Check the shared credit balance during the term. On Growth, the grant is 10,000 AI Credits each month. Credits past that grant accrue on the On-demand AI Credits addon until the monthly invoice is generated.

Using the app

Open the subscription and view the ledger and credit balance under the Credit balance section.

You can share the Usage PDF with your customers after an invoice is raised so they can see the usage accounted for.

Using the API

Read the live balance from the ledger. provisioned_balance.usable_balance is the amount of the prepaid grant still available to spend on article generation, image generation, and video generation. provisioned_balance.hold_amount is reserved by authorize operations and is not usable. provisioned_balance.total_balance is usable balance plus held amount. overdraft_balance is the balance consumption draws from after the provisioned grant is exhausted. Credit amounts are decimal strings. Keep them as strings when you format or calculate the balance.

Use this balance for the headline total. Grants expire on their own, and the grant list is paginated, so a sum of one page of grant blocks can miss credits.

curl  https://{site}.chargebee.com/api/v2/ledger_account_balances \
     -G \
     -u {site_api_key}:\
     --data-urlencode "subscription_id[is]"="{subscription_id}" \
     --data-urlencode "unit_id[is]"="ai_credits"

See List ledger account balances. List ledger operations when you need the customer's credit history. Grants, captures, rollovers, and expiries appear there as separate operation types.

You can allow your customers to see the remaining usage for each feature in your product portal using the same APIs. See Show prepaid credit balances, grants, and transactions in your customer portal.

Manage overage invoices

At the end of each monthly billing period, Chargebee Billing invoices the On-demand AI Credits addon for credits consumed past the grant. The customer is not charged an overage for credits that the monthly grant still covers.

Metered addon invoices are created in a Pending state. While an invoice is pending, it does not trigger payment collection. You manage how pending overage invoices close under Settings > Configure Chargebee > Billing LogIQ > Usage Based Billing. Two behaviors are available:

  • Manual close. Review usage, then close the invoice yourself.
  • Auto-close. Chargebee Billing closes the invoice automatically at a time you define.

Once an invoice is closed, payment collection begins. If you need to correct a closed invoice, void it and regenerate it.

See Actions for pending invoices.

Summary

This guide showed you how to:

  • Define one AI Credits unit that article generation, image generation, and video generation share.
  • Grant 10,000 credits each month on a $12,000 yearly Growth plan and 50,000 on a $30,000 yearly Enterprise plan, with unused credits expired at the end of the grant period.
  • Attach a monthly metered addon that invoices AI Credits past the grant at $0.10 each.
  • Draw all three products from the same ledger with Capture, or with Authorize followed by Capture authorization, read the usable balance, and use a credit alert and a spend alert before overages grow.

FAQs

How do I make sure a customer has enough credits before starting a job, without other requests spending the same credits?

Call Authorize. It reserves the credits by moving them from the usable balance to a held amount. Other operations cannot spend held credits, so concurrent requests cannot spend the same credits in the meantime. Nothing is consumed at this stage.

Use Authorize while a job is still in progress and the final amount is not known yet. When you already know how many credits to draw, call Capture.

How do I reserve credits when a job starts and consume them only when it completes?

Call Authorize when the job starts to hold the credits. When the job finishes, call Capture authorization to convert the held credits into consumed credits. If the job used fewer credits than you reserved, capture only the amount used. The unused remainder is automatically released back to the usable balance.

For example, authorize 100 credits, and the job uses 70. Capture 70. Result: 70 credits are consumed, and 30 are released.

The customer canceled, or my process failed after I reserved credits. How do I give the credits back?

Call Release authorization with the authorization_id from the original Authorize operation. The entire held amount returns to the usable balance, and nothing is consumed. Partial releases are not supported.

What happens if I authorize credits and never capture or release them?

The hold is automatically released back to the usable balance when it expires. By default this is about 10 minutes after the Authorize request is processed. You can set a different expiration with auto_release_timestamp.

See also