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.

2 Likes

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.

How do I charge my customers for API usage?

  1. Use an API Gateway:

As @_j mentioned above, you should not, under any circumstances, share your own OpenAI credentials with your end customer/developer.

I recommend first creating an API Gateway layer on top of OpenAI that proxies the upstream API calls and allows you to generate your own API Keys for each customer.

Each of the major cloud providers has its own API Gateway and will allow you to proxy the OpenAI calls. Microsoft Azure has an example of this on their site: azure-openai-api-from-specification

  1. Create an API Product
    @cesarer is correct to ask if a usage based revenue model is the best fit in this case. It will add additional complexity where a simple subscription (flat-fee) may be preferred. Here are some common API Product revenue models:

i) Flat-fee
If your call costs to OpenAI are static, you could use this as a basis and add your 25% markup.

ii) Usage Based
If your costs vary depending on which API is being called, then you may need to move to a usage-based revenue model. Here, you can say how much each API call costs and charge accordingly.

iii) Flat fee plus Overage
Many developers prefer this revenue model. They get a good deal on the API calls they make that are included in the plan, but the API does not stop working if they exceed their quota. They just get charged on an API call basis for the rest of the billing period, usually at a higher rate.

You code your own API Products and bake them into your application, or you may find it easier to use an existing API Portal Platform that allows you to monetize API products.

  1. Tracking API Usage

As developers make API calls to your product, you need to be able to charge them accordingly. Assuming you have connected an API Gateway as mentioned above, you can now monitor the logs for each of your API Consumers, usually identified via their API Key.

If you want to avoid coding this, some API Portal will monitor the API usage and bill your customers on your behalf.

1 Like