Check users api costs via the api

For my application I am considering using open ai’s administration api to create project users for each of my customers. It will be a pay as go type thing for them, and I want how much they are spending to be visible to them. Looking at the api docs I don’t see an obvious way to get an api’s spending from the api.

  1. Is this possible?
  2. Is setting up a project user for each of my users the best way to go about it?
1 Like

I have developed a token count system for my chatbot, this is valid for ChatGPT-4o-mini but you can easily adapt it:

// manages token from the response
    const tokenCount = document.createElement("p");
        

    // Verify that "completion_tokens" is present in the API response
    if (data.usage.completion_tokens) {
            const requestTokens = data.usage.prompt_tokens;
      		const responseTokens = data.usage.completion_tokens;
      		const totalTokens = data.usage.total_tokens;      
			const pricepertokenprompt = 0.15/1000000; //uses gpt-4o-mini price of 0.15/Mt USD
			const pricepertokenresponse = 0.60/1000000; //uses gpt-4o-mini price of 0.15/Mt USD            
			const priceperrequest = pricepertokenprompt*requestTokens;
            const priceperresponse = pricepertokenresponse*responseTokens;
            const totalExpense = priceperrequest + priceperresponse;
      tokenCount.innerHTML = `<hr>Your request used ${requestTokens} tokens and costed ${priceperrequest.toFixed(6)}USD<br>This response used ${responseTokens} tokens and costed ${priceperresponse.toFixed(6)}USD<br>Total Tokens: ${totalTokens}. This interaction costed you: ${totalExpense.toFixed(6)}USD.`;
    } else {
      tokenCount.innerHTML = "Unable to track the number of used tokens.";
    }

    showMessage("VivacityGPT", cleanResponse, tokenCount.innerHTML);