Skip to main content
To have your coding agent write the integration, install the Dodo Agent Plugin. It adds the Dodo Payments skills and MCP servers to Claude Code, Codex CLI, Cursor, VS Code / GitHub Copilot, Kiro, and OpenCode.
You’ll build an AI chat application that bills customers for the tokens they use. You create the billing meter, the product, and the application code that runs conversations and reports token usage as it happens.
This tutorial builds a complete application with a backend and a frontend. The chat app uses Google Gemini, and the LLM Blueprint reports token usage for each response, so you don’t count tokens yourself.
When you finish, you’ll have a chat application that:
  • Runs AI conversations with Google Gemini through the AI SDK.
  • Tracks token usage automatically, with no counting code.
  • Charges customers for the tokens they actually use.
  • Includes a chat interface that keeps the conversation history.
Chat interface of the finished AI chat app, showing a conversation with token counts under each response

What We’re Building

The chat service works like this:
  • Service: AI chat with Google Gemini through the AI SDK
  • Pricing Model: Pay per token ($0.01 per 1,000 tokens)
  • Free Tier: 10,000 free tokens per customer per month
  • Features: Conversation history and automatic token tracking
Before you start, you need:

Step 1: Create Your Usage Meter

Create a meter in the Dodo Payments dashboard to track AI token usage.
What you’re building: A meter named “AI Token Usage Meter” that adds up the tokens used in chat conversations.
1

Open the Meters Section

  1. Log in to the Dodo Payments dashboard.
  2. Click Products in the sidebar.
  3. Click Meters.
  4. Click Create Meter.
Meters page in the Dodo Payments dashboard with the Create Meter button
The meter form opens.
2

Enter the Meter Details

Enter the details for the chat service:Meter Name → AI Token Usage MeterDescription → Tracks token consumption from AI chat conversations using AI SDKEvent Name → ai_chat_usage
The event name ai_chat_usage must match the event name the application code sends. Event names are case-sensitive.
3

Configure How Tokens Are Counted

Set the aggregation, which controls how the meter combines events:Aggregation Type: SumOver Property: totalTokensMeasurement Unit: tokens
Sum adds up the tokens from every chat message. The LLM Blueprint sends inputTokens, outputTokens, and totalTokens in each event’s metadata, and this meter sums totalTokens.
4

Create the Meter

  1. Check that every setting matches the values above. You can’t edit a meter after you create it.
  2. Click Create Meter.
Meter form filled in with the AI Token Usage Meter name, the ai_chat_usage event, and Sum aggregation
Meter created. The “AI Token Usage Meter” is ready to count tokens. Next, connect it to a billing product.

Step 2: Get Your API Keys

Collect the two API keys the app needs.
1

Get a Dodo Payments API Key

  1. In the Dodo Payments dashboard, go to Developer → API Keys.
  2. Click Add API Key.
  3. Copy the API key.
Create the key in test mode: a test mode key works only with test mode. You add the key to the .env file in Step 5.
2

Get a Google AI API Key

  1. Go to aistudio.google.com.
  2. Click Get API Key.
  3. Create an API key or use an existing one.
  4. Copy the key.
Keep this key private. You add it to the .env file in Step 5.

Step 3: Create Your Billing Product

Create a product that defines the pricing: $0.01 per 1,000 tokens, with 10,000 free tokens. The product connects the meter to billing.
What you’re building: A product called “AI Chat Service” that charges for token usage above a free tier.
1

Navigate to Products

  1. In the Dodo Payments dashboard, click Products in the sidebar.
  2. Click Add Product.
  3. Under Pricing Type, select Usage Based Billing.
With this pricing type, Dodo Payments bills from meter usage instead of a fixed subscription price.
2

Enter Product Details

Fill in the required details:Product Name: → AI Chat ServiceDescription: → AI-powered chat service with automatic token-based billingProduct Image: Upload a relevant image.
The name and description appear on customer invoices.
3

Connect Your Meter

Check that the product’s pricing type is Usage Based Billing.Set the Price to 0, so customers pay only for usage, with no base fee.Then link the meter you created:
  1. Scroll to the Associated Meters section.
  2. Click Add meters.
  3. Select “AI Token Usage Meter” from the dropdown.
  4. Confirm that the meter appears in the product configuration.
The meter is connected to the product.
4

Set Your Pricing

Define the per-token price and the free tier:Price per unit: → 0.00001. That’s $0.00001 per token, or $0.01 per 1,000 tokens.Free Threshold: → 10000. Each customer gets 10,000 free tokens per month.
Meter pricing on the AI Chat Service product, with price per unit and free threshold filled in
How billing works: A customer who uses 25,000 tokens in a month pays for 15,000 tokens (25,000 − 10,000 free). 15,000 × $0.00001 = $0.15.
5

Save Your Product

  1. Review the settings:
    • Name: AI Chat Service
    • Meter: AI Token Usage Meter
    • Price: $0.01 per 1,000 tokens
    • Free tier: 10,000 tokens
  2. Click Save Changes.
Product created. Billing is configured, and customers are charged for the tokens they use.

Step 4: Make a Test Purchase

Create a test customer by buying the product. The app sends usage events for this customer.
1

Get Your Payment Link

  1. In the Dodo Payments dashboard, go to Products.
  2. Find the “AI Chat Service” product.
  3. Click Share next to the product.
  4. Copy the payment link.
2

Complete a Test Purchase

  1. Open the payment link in a new browser tab.
  2. Enter test payment details and complete the purchase.
After the payment succeeds, the purchase creates a customer. The application code uses that customer’s ID.
3

Find Your Customer ID

  1. Go back to the Dodo Payments dashboard.
  2. Go to Sales → Customers in the sidebar.
  3. Find the customer you created, by its test email address.
  4. Copy the customer ID. It starts with cus_.
Save this customer ID. You use it when you test the chat application.

Step 5: Build the Chat Application

With billing set up and a test customer created, build the chat application.
1

Set Up Your Project

Create a directory and initialize the project:
2

Install Dependencies

Install the packages:
3

Configure TypeScript

Create tsconfig.json:
tsconfig.json
Update package.json to add the module type and scripts:
package.json
4

Create the Project Structure

Create the source and static-file folders:
5

Set Up Environment Variables

Create a .env file in the project root:
.env
Replace the placeholder values with the API keys from Step 2. The Google provider for the AI SDK reads GOOGLE_GENERATIVE_AI_API_KEY by default.
6

Create the Backend Server

Create src/server.ts and paste in the server code below.
The complete chat server with billing, plus the matching .env and package.json:

Step 6: Add the Chat Interface

Add a chat interface that keeps the full conversation history. Create public/index.html:
public/index.html

Step 7: Test Your Chat Application

Test the chat app end to end and watch the usage reach Dodo Payments.
What you’re testing: You chat with the AI, confirm that the token events reach Dodo Payments, and check the billing calculation.
1

Start the Server

  1. Check that the .env file has both API keys from Step 2.
  2. Start the development server:
The server logs:
The server is running.
2

Open the Chat Interface

  1. Open your browser.
  2. Go to http://localhost:3000. The chat interface loads.
Before you chat, set CUSTOMER_ID in server.ts to the test customer ID from Step 4, then restart the server.
3

Have Your First Conversation

Send these messages:
  1. “What is artificial intelligence?”
  2. “How does machine learning work?”
  3. “Can you explain neural networks?”
Each response shows the number of tokens it used.
If the AI responds and token counts appear, the app works.
4

Check Your Dodo Payments Dashboard

Confirm that Dodo Payments receives the events:
  1. Open the Dodo Payments dashboard.
  2. Go to Products → Meters and open AI Token Usage Meter.
  3. Select the Events tab.
  4. Find your chat events in the list.
What to look for:
  • Event name: ai_chat_usage
  • Customer ID: your test customer ID
Events tab of the AI Token Usage Meter listing ai_chat_usage events
The list has one event for each message you sent.
5

Verify Token Counting

Send a few more messages, then check the aggregation:
  1. In the meter, select the Customers tab.
  2. Find your test customer.
  3. Check the Consumed Units column. It shows the total tokens used.
Customers tab of the meter showing consumed units for the test customer
The meter sums the totalTokens value from every event.
6

Test the Free Tier

Use enough tokens to pass the free tier:
  1. Have several more conversations, until the total passes about 15,000 tokens.
  2. Open the meter’s Customers tab again.
  3. Check the values:
    • Consumed Units: 15,000+ tokens
    • Chargeable Units: 5,000 (10,000 free tokens applied)
    • Total Price: ~$0.05
Customers tab of the meter showing consumed units, chargeable units, and total price after the free tier
Usage-based billing works. Customers are charged for the tokens they use.

Troubleshooting

Common problems and their fixes:
Possible causes:
  • Event name doesn’t match meter configuration exactly
  • Customer ID doesn’t exist in your account
  • API key is invalid or expired
  • Network connectivity issues
Solutions:
  1. Check that the event name matches the meter exactly. It’s case-sensitive: ai_chat_usage.
  2. Check that the customer ID exists in the Dodo Payments dashboard.
  3. Test the API key with a simple API call.
  4. Check the server logs for errors.
Possible causes:
  • Model not returning usage information
  • Incorrect SDK version
Solutions:
  1. Check whether the model returns usage:
  1. Update the Blueprints package: npm install @dodopayments/ingestion-blueprints@latest
Possible causes:
  • The API key belongs to the other mode.
  • The .env file has extra spaces or quotes.
Solutions:
  • A key works only in the mode it was created in. Use a test mode key with DODO_PAYMENTS_ENVIRONMENT=test_mode, and a live mode key with live_mode.
  • Remove any quotes around keys in the .env file.
  • Generate a new key if needed.
Need help?

Congratulations! You Built an AI Chat App

You have a working AI chat application that tracks token usage and bills customers for it through Dodo Payments.

Learn More

LLM Blueprint

Track LLM token usage with the LLM Ingestion Blueprint.

Ingestion Blueprints SDK

More examples in the GitHub repository.

Join Community

Get help and share your projects on Discord.
Last modified on September 26, 2026