How AI billing works
Each usage event has two components that must be tracked together:- Tokens: The amount consumed, typically measured and reported by your AI provider
- Dollars: The monetary value of that consumption based on your pricing model
- Usage tracking: The AI provider reports token consumption (input and output tokens)
- Cost calculation: The token count is converted to USD based on your pricing model
- Atomic recording: Both tokens and dollars are recorded together in a single transaction
- Balance management: For prepaid, funds are deducted from the customer wallet. For postpaid, balances move into overdraft
- Invoice generation: At the end of the billing period, generate invoices from accumulated usage
Prerequisites
Before starting, ensure you have:- A running Blnk server instance (e.g. at
http://localhost:5001). - An API key for Blnk (replace
YOUR_API_KEYin the code examples). Required for authenticated requests. - The Blnk CLI installed or a connected Blnk Cloud workspace to view your ledger data.
1: Create customer wallet
Each customer needs a wallet to track their usage and balance. Each wallet will be represented with a ledger and a balance.1
Create a ledger
Create a ledger for customer wallets. This groups all customer balances in one place:
Save the
ledger_id from the response. You’ll need it to create balances for customer wallets.
2
Create customer identity
Create a customer identity to link all balances to the same customer:
Save the
identity_id from the response. You’ll use this ID to link the customer wallet to this identity.3
Create customer wallet balance
Create a balance for the customer wallet:
Save the
balance_id from the response. You’ll need it for wallet funding and usage transactions.
2: Wallet top-up
Customers can fund their wallets before using your AI product. This is required for prepaid billing models.1
Money movement map
When a customer funds their wallet, money moves from the external world to the customer’s wallet:

@World-USD is an internal balance that represents money entering and leaving your system. Internal balances use the @ prefix and don’t require a balance ID.2
Fund the customer wallet
Create a transaction to add funds to the customer’s wallet:

Blnk enforces double-entry accounting through the source and destination fields. The source represents where funds are deducted from, while the destination represents where they are credited to.
3: Track usage and billing
To record usage in your ledger, you need to know how many tokens your AI model consumed and what that usage is worth in USD. Both components are recorded together atomically.1
Money movement map
When a customer uses your AI product, two things happen simultaneously:

- Tokens are moved from the customer’s token allocation to your system’s token pool
- The corresponding USD value is debited from the customer’s wallet and credited to your revenue account
2
Connect to your LLM provider
Connect to your AI provider. For this example, we’ll use the OpenAI TypeScript SDK:
3
Capture token usage
Make a completion request and extract token usage after each event:
4
Calculate USD value
Each model has its own rate for input and output tokens. Calculate the cost:
5
Record usage atomically
Record both token usage and dollar cost together using the bulk transactions endpoint with 
atomic: true:
The response includes a
batch_id that links both legs of the transaction in your ledger.4: Prepaid vs postpaid billing
Your system can support both prepaid and postpaid billing models.1
Prepaid billing
With prepaid billing, customers fund their wallet before using your product:
- The customer must have sufficient balance before usage
- The corresponding dollar cost is deducted from the customer wallet
- If the wallet balance becomes insufficient or reaches zero, the transaction fails
- The customer must top up their wallet to continue using the product
allow_overdraft: false on the USD leg to enforce prepaid behavior.2
Postpaid billing
With postpaid billing, customers don’t need to fund their wallet in advance:
- Usage accumulates with the balance moving into overdraft (using
allow_overdraft: trueon the USD leg) - The overdraft balance represents how much the customer owes
- At the end of the billing period, retrieve the overdraft amount to generate an invoice
- Once payment is made, the balance resets to zero and the next cycle begins.
Conclusion
You now have a fully functional AI billing system built with Blnk Finance. This system can:- Create and manage customer wallets for tracking usage per customer
- Fund customer wallets for prepaid billing models
- Track token usage and calculate costs atomically
- Support both prepaid and postpaid billing models
- Generate accurate invoices from accumulated usage