How To Charge Customers for API usage in my Custom Windows Application

I have a commercial windows desktop application for many customers.
I want to provide AI assistance type of feature in my app.

How do I charge my customers for API usage?

for example,
I want to provide my customers with a subscription from which I will take
some portion.
so say my customer buys $10 worth of API usage per month (this is the min $$ they can subscribe),
I will take $5, so effectively they are buying $5 worth of usage per month.

but I need a way to track their usage in my app.
Has someone already done this?
Any help will be appreciated.

You will need the application to communicate with your own servers that use client accounts and secure connections.

You would never put your API key into a client application and allow direct communications with OpenAI, because then you have given the hacker the gift of unlimited use. Many that thought they were clever have done this…

2 Likes

of course that would the right thing to do.
But my question is how do I calculate the usage for each customer?

Example:
My app allows user to buy a subscription for any amount greater than say $20.
I would take say $5 for providing the feature. So the customer effectively has $15 worth of credit for API usage.

Now for each API call to my server, my server will call openAI on behalf of the customer. So I need to determine the usage for each API call, then I would store that on my server. When the $15 is exhausted, my app will show msg, “You have used up your quota for the month” etc…

I’m not sure whether a model where you change based on usage it’s a good one. I think you should charge in any case for a subscription, and make sure you know on average how much users are consuming. That said, you must track the cost per user to make sure you’re making money. Each API call returns the inputTokens, outputTokens which you can use to calculate cost as per logic below:

// Prices in $ as per Pricing hence per 1k tokens. calculateCost() will divide by 1k
const modelCosts = {
‘gpt-3.5-turbo’: {
inputCostPer1kToken: 0.0005,
outputCostPer1kToken: 0.0015,
},
‘gpt-3.5-turbo-0125’: {
inputCostPer1kToken: 0.0005,
outputCostPer1kToken: 0.0015,
},
‘gpt-4’: {
inputCostPer1kToken: 0.03,
outputCostPer1kToken: 0.06,
},
‘gpt-4-turbo’: {
inputCostPer1kToken: 0.01,
outputCostPerToken: 0.03,
},
‘gpt-4-turbo-preview’: {
inputCostPer1kToken: 0.01,
outputCostPer1kToken: 0.03,
},
// Add other models as necessary
};

// Calculate API cost
function calculateCost(model, inputTokens, outputTokens) {
// Example: Fetch model-specific costs from modelCosts
const costs = modelCosts[model] || modelCosts[‘default’];
// divide by 1k (token) and multiply by 100 to get price in cents
const inputCostCents = (inputTokens / 1000 * 100) * costs.inputCostPer1kToken;
const outputCostCents = (outputTokens / 1000 * 100) * costs.outputCostPer1kToken;
return inputCostCents + outputCostCents;
}

1 Like

Yes this is what i was looking for.
A formula,
Thank you for proving the calculations.