Token-Based AI Chat
Experience variable pricing with a pay-per-token AI chatbot.
What is Token-Based Pricing?
Traditional AI API pricing charges based on tokens - the units of text processed by language models. A token is roughly 4 characters or 0.75 words. For example:
- "Hello, how are you?" ≈ 6 tokens
- A detailed response might use 200-500 tokens
The x402 starter kit demonstrates pay-per-token pricing where you only pay for the actual tokens consumed in each interaction.
The "Upto" Payment Scheme
Unlike the "exact" scheme used in human payment mode (pay exactly $0.01), token-based chat uses the "upto" scheme:
- Authorize maximum: You sign for up to $0.50 per message
- Pay actual cost: You're only charged for tokens used (e.g., $0.03)
- Refund difference: The unused authorization is never spent
This protects you from unexpected costs while enabling variable pricing.
Pricing Details
- Rate: $0.001 per 1,000 tokens
- Maximum: $0.50 per message (500,000 tokens cap)
- Typical cost: $0.01-0.05 for most interactions
Step-by-Step: Using Token-Based Chat
Step 1: Switch to AI Agent Mode
- Open the starter kit at
http://localhost:3000 - Click the "AI Agent" tab at the top
- Select "Token-Chat" from the sub-navigation
Step 2: Connect Your Wallet
- Click "Connect Wallet"
- Ensure you're on Avalanche Fuji Testnet
- Verify you have test USDC (get from Circle Faucet if needed)
Step 3: Send a Message
- Type a message in the chat input (e.g., "What is blockchain?")
- Click Send or press Enter
- Your wallet will prompt for a signature
Step 4: Observe Variable Pricing
After signing, watch the response:
- The AI generates a response
- Token count is displayed (input + output tokens)
- Actual cost shows what you paid (e.g., "$0.02")
- This is typically much less than the $0.50 maximum authorized
Step 5: Compare Different Messages
Try sending messages of varying complexity:
- Short question: "What is AVAX?" → Low token cost
- Detailed request: "Explain how smart contracts work with examples" → Higher token cost
Notice how the cost scales with response length.
How It Works
The token-based chat uses OpenRouter to access AI models. The payment flow:
- Request: Client sends message to
/api/ai-chat - 402 Response: Server returns payment requirements with "upto" scheme
- Authorization: User signs for maximum amount ($0.50)
- Processing: Server calls OpenRouter, counts tokens
- Settlement: Only actual token cost is settled on-chain
- Response: AI response delivered to user
The "upto" scheme is defined in the x402 specification for scenarios where the exact cost isn't known until after processing.
Verifying Your Payment
After each chat interaction:
- Check the Transaction Log in the starter kit UI
- View the actual amount charged vs. authorized
- Verify on Thirdweb Dashboard transaction history
You'll see that even though you authorized $0.50, only the actual token cost was transferred.
Is this guide helpful?
