How to implement a credits system to track usage across multiple different features.
When you have multiple features that cost different amounts, you can use a credit system to deduct usage from a single balance. This can be great to simplify billing and usage tracking, especially when you have lots of features.
Let’s create our free product. Since we’re not charging for this product, we’re not creating any pricing. Since we want to give $5 credits for free, and each credit is 1 cent, we’ll give 500 credits for free.
Make sure we set the free product as a default product so it is
automatically assigned to any new customers.
Every time our user sends a message to the chatbot, we’ll first check if they have enough credits to send the message. This is so our free users don’t exceed their 500 credit limit.
Our pro users will always have allowed: true as they have overage pricing enabled.
Note how we’re interacting with the underlying features (small-model-tokens,
medium-model-tokens, large-model-tokens) here—not the credit system.
If we call the check endpoint again (as we should before using any of the models), we can see that the balance and allowance fields have been updated. Since allowed is false, we should prompt the user to upgrade.
We can prompt the user to upgrade. When they click our “upgrade” button, we can use the attach route to get a Stripe Checkout URL for them to make a payment.
Since we have overage pricing enabled, the check route will always return allowed: true for our pro users. Let’s use up some more credits to see what happens:
Small model: 50,000 tokens * 0.1 cents per token = 5,000 cents (50 USD)
Medium model: 10,000 tokens * 1 cent per token = 10,000 cents (100 USD)
Large model: 1,000 tokens * 5 cents per token = 5,000 cents (50 USD)