Use our AI assistant in VS Code, Cursor, or Windsurf to generate SDK/API code, LLM Blueprint integration code, webhooks, and more - just by describing what you want.Try Sentra: AI-Powered Integration →
- Powers AI conversations using Google Gemini (AI SDK)
- Automatically tracks token usage (no manual code)
- Charges customers based on actual token consumption
- Includes a beautiful chat interface

What We’re Building
Let’s start by understanding our AI chat service:- Service: AI-powered chat using Google Gemini (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, automatic token tracking
- A Dodo Payments account
- A Google AI API key (Get one from aistudio)
- Node.js v16+ installed
Step 1: Create Your Usage Meter
We’ll start by creating a meter in your Dodo Payments dashboard that will track AI token usage.Open the Meters section
- Log into your Dodo Payments dashboard
- Click on Products in the left sidebar
- Click on Meters
- Click the Create Meter button

Fill in the basic meter information
AI Token Usage Meter
Description → Tracks token consumption from AI chat conversations using AI SDK
Event Name → ai_chat_usage
ai_chat_usage
must match exactly what we’ll send from our application code later. Event names are case-sensitive!Configure how we count tokens
totalTokens
Measurement Unit: Type → tokens
totalTokens
in each event.Create your meter
- Double-check all your settings match the values above
- Click Create Meter

Step 2: Get Your API Keys
Before we build the app, let’s gather the API keys we’ll need.Get Dodo Payments API Key
- In your Dodo Payments dashboard, go to Developers → API Keys
- Click Create API Key
- Copy the API key - it will look like
test_abc123...
.env
file later.Get Google AI API Key
- Visit aistudio.google.com
- Click Get API Key
- Create a new API key or use an existing one
- Copy the key
.env
file.Step 3: Create Your Billing Product
Now we need to create a product that defines our pricing ($0.01 per 1,000 tokens with 10,000 free tokens). This connects our meter to actual billing.Navigate to Products
- In your Dodo Payments dashboard, click Products in the left sidebar
- Click Create Product
- Select Usage-Based as the product type
Enter product details
AI Chat Service
Description: → AI-powered chat service with automatic token-based billing
Product Image: Upload a relevant imageConnect your meter
0
to ensure customers are only charged based on their usage, with no base fee.Now, link the meter you just created:- Scroll down to the Associated Meter section
- Click Add Meters
- From the dropdown, select “AI Token Usage Meter” (the one you created earlier)
- Confirm that it appears in your product configuration
Set your pricing
0.00001
(this is 0.00001 per token)Free Threshold: Enter → 10000
(customers get 10,000 free tokens per month)
Save your product
- Review all your settings:
- Name: AI Chat Service
- Meter: AI Token Usage Meter
- Price: $0.01 per 1,000 tokens
- Free tier: 10,000 tokens
- Click Save Changes
Step 4: Make a Test Purchase
Before we start building the app, let’s create a test customer by making a purchase.Get your payment link
- In your Dodo Payments dashboard, go to Products
- Find your “AI Chat Service” product
- Click the Share button next to your product
- Copy the payment link that appears
Complete a test purchase
- Open the payment link in a new browser tab
- Enter test payment details and complete purchase
Find your customer ID
- Go back to your Dodo Payments dashboard
- Navigate to Sales -> Customers in the left sidebar
- Find the customer you just created (with the test email)
- Copy the customer ID - it will look like
cus_123
Step 5: Build the Chat Application
Now we have our billing setup complete and a test customer created. Let’s build the AI chat application with automatic token tracking.Set up your project
Install dependencies
Configure TypeScript
tsconfig.json
:package.json
to add the module type and scripts:Create project structure
Set up environment variables
.env
file in your project root:Create the backend server
src/server.ts
and copy this complete server code:Step 6: Add the Chat Interface
Now let’s add a beautiful chat interface with full conversation history! Createpublic/index.html
:
Step 7: Test Your Chat Application
Time to test our AI chat app and see the billing in action! Let’s make sure everything works end-to-end.Start the server
- Verify your
.env
file has all the API keys from Step 2 - Start the development server:
Open the chat interface
- Open your browser
- Navigate to
http://localhost:3000
- You should see the beautiful chat interface
CUSTOMER_ID
in server.ts
with your actual test customer ID from Step 4.Have your first conversation
- “What is artificial intelligence?”
- “How does machine learning work?”
- “Can you explain neural networks?”
Check your Dodo Payments dashboard
- Open your Dodo Payments dashboard
- Go to Usage Billing → AI Token Usage Meter
- Click on the Events tab
- You should see your chat events listed
- Event names:
ai_chat_usage
- Customer ID: Your test customer id

Verify token counting
- In your meter, go to the Customers tab
- Find your test customer
- Check the “Consumed Units” column - it should show the total tokens used

totalTokens
values automatically!Test the free tier
- Have several more conversations (aim for ~15,000+ total tokens)
- Check your Customers tab in meter dashboard again
- You should now see:
- Consumed Units: 15,000+ tokens
- Chargeable Units: 5,000 (10,000 free tokens applied)
- Total Price: ~$0.05

Troubleshooting
Common issues and their solutions:Events not appearing in dashboard
Events not appearing in dashboard
- 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
- Verify event name matches meter configuration exactly (case-sensitive:
ai_chat_usage
) - Check that customer ID exists in Dodo Payments dashboard
- Test API key with a simple API call
- Check server logs for error messages
Token counts showing zero
Token counts showing zero
- Model not returning usage information
- Incorrect SDK version
- Test if model returns usage:
- Update to latest Blueprints SDK:
npm install @dodopayments/ingestion-blueprints@latest
API key invalid error
API key invalid error
- Wrong API key for environment
- Extra spaces or quotes in
.env
file
- Ensure test key starts with
test_
, live key starts withlive_
- Remove any quotes around keys in
.env
file - Generate a new key if needed