- 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.

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
- A Dodo Payments account.
- A Google AI API key. Get one from Google AI Studio.
- Node.js 22 or later.
Step 1: Create Your Usage Meter
Create a meter in the Dodo Payments dashboard to track AI token usage.Open the Meters Section
- Log in to the Dodo Payments dashboard.
- Click Products in the sidebar.
- Click Meters.
- Click Create Meter.

Enter the Meter Details
AI Token Usage MeterDescription → Tracks token consumption from AI chat conversations using AI SDKEvent Name → ai_chat_usageConfigure How Tokens Are Counted
totalTokensMeasurement Unit: tokensinputTokens, outputTokens, and totalTokens in each event’s metadata, and this meter sums totalTokens.Create the Meter
- Check that every setting matches the values above. You can’t edit a meter after you create it.
- Click Create Meter.

Step 2: Get Your API Keys
Collect the two API keys the app needs.Get a Dodo Payments API Key
- In the Dodo Payments dashboard, go to Developer → API Keys.
- Click Add API Key.
- Copy the API key.
.env file in Step 5.Get a Google AI API Key
- Go to aistudio.google.com.
- Click Get API Key.
- Create an API key or use an existing one.
- Copy the key.
.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.Navigate to Products
- In the Dodo Payments dashboard, click Products in the sidebar.
- Click Add Product.
- Under Pricing Type, select Usage Based Billing.
Enter Product Details
AI Chat ServiceDescription: → AI-powered chat service with automatic token-based billingProduct Image: Upload a relevant image.Connect Your Meter
0, so customers pay only for usage, with no base fee.Then link the meter you created:- Scroll to the Associated Meters section.
- Click Add meters.
- Select “AI Token Usage Meter” from the dropdown.
- Confirm that the meter appears in the product configuration.
Set Your Pricing
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.
Save Your Product
- Review the 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
Create a test customer by buying the product. The app sends usage events for this customer.Get Your Payment Link
- In the Dodo Payments dashboard, go to Products.
- Find the “AI Chat Service” product.
- Click Share next to the product.
- Copy the payment link.
Complete a Test Purchase
- Open the payment link in a new browser tab.
- Enter test payment details and complete the purchase.
Find Your Customer ID
- Go back to the Dodo Payments dashboard.
- Go to Sales → Customers in the sidebar.
- Find the customer you created, by its test email address.
- Copy the customer ID. It starts with
cus_.
Step 5: Build the Chat Application
With billing set up and a test customer created, build the chat application.Set Up Your Project
Install Dependencies
Configure TypeScript
tsconfig.json:package.json to add the module type and scripts:Create the Project Structure
Set Up Environment Variables
.env file in the project root:GOOGLE_GENERATIVE_AI_API_KEY by default.Create the Backend Server
src/server.ts and paste in the server code below..env and package.json:
Step 6: Add the Chat Interface
Add a chat interface that keeps the full conversation history. Createpublic/index.html:
Step 7: Test Your Chat Application
Test the chat app end to end and watch the usage reach Dodo Payments.Start the Server
- Check that the
.envfile has both API keys from Step 2. - Start the development server:
Open the Chat Interface
- Open your browser.
- Go to
http://localhost:3000. The chat interface loads.
CUSTOMER_ID in server.ts to the test customer ID from Step 4, then restart the server.Have Your First Conversation
- “What is artificial intelligence?”
- “How does machine learning work?”
- “Can you explain neural networks?”
Check Your Dodo Payments Dashboard
- Open the Dodo Payments dashboard.
- Go to Products → Meters and open AI Token Usage Meter.
- Select the Events tab.
- Find your chat events in the list.
- Event name:
ai_chat_usage - Customer ID: your test customer ID

Verify Token Counting
- In the meter, select the Customers tab.
- Find your test customer.
- Check the Consumed Units column. It shows the total tokens used.

totalTokens value from every event.Test the Free Tier
- Have several more conversations, until the total passes about 15,000 tokens.
- Open the meter’s Customers tab again.
- Check the values:
- Consumed Units: 15,000+ tokens
- Chargeable Units: 5,000 (10,000 free tokens applied)
- Total Price: ~$0.05

Troubleshooting
Common problems and their fixes: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
- Check that the event name matches the meter exactly. It’s case-sensitive:
ai_chat_usage. - Check that the customer ID exists in the Dodo Payments dashboard.
- Test the API key with a simple API call.
- Check the server logs for errors.
Token counts showing zero
Token counts showing zero
- Model not returning usage information
- Incorrect SDK version
- Check whether the model returns usage:
- Update the Blueprints package:
npm install @dodopayments/ingestion-blueprints@latest
API key invalid error
API key invalid error
- The API key belongs to the other mode.
- The
.envfile has extra spaces or quotes.
- 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 withlive_mode. - Remove any quotes around keys in the
.envfile. - Generate a new key if needed.