#Discrepancy Between Calculated Cost and OpenAI Billing for GPT-3.5 Turbo Usage

1 messages · Page 1 of 1 (latest)

spare sparrow
#

Hi OpenAI community! I've noticed a discrepancy between my calculated cost for a gpt-3.5-turbo-0125 model usage and what's shown in my billing data, and I'm looking for insights.

Code snippet for cost calculation:

// OpenAI API call
const response = await openai.chat.completions.create({
  model: "gpt-3.5-turbo-0125",
  messages: [{ role: "system", content: systemPrompt }, { role: "user", content: userPrompt }],
  max_tokens: Math.min(Math.max(characterCount / 4, 100), 4096),
  n: 1,
});
console.log({ usage: response.usage });

// Cost calculation
function calculateOpenAICost(promptTokens: number, completionTokens: number): string {
  const inputCost = 0.0005, outputCost = 0.0015; // Cost per 1000 tokens
  const totalCost = ((promptTokens + completionTokens) / 1000) * (inputCost + outputCost);
  return totalCost.toFixed(6);
}
console.log(`Total Cost: $${calculateOpenAICost(138, 18)}`); // Outputs $0.000096

Pricing details:

  • Input tokens: $0.0005 / 1K tokens
  • Output tokens: $0.0015 / 1K tokens

Billing discrepancy:

  • Expected cost: $0.000096 (for 138 prompt tokens and 18 completion tokens)
  • Billed amount: $0.01065 (from billing data export)
{
  "timestamp": 1708992000.0,
  "currency": "usd",
  "GPT-4 Turbo": 0.0,
  "GPT-4": 0.0,
  "GPT-3.5 Turbo": 0.01065,
  "Assistants API": 0.0,
  "Fine-tuning models": 0.0,
  "Embedding models": 0.0,
  "Base models": 0.0,
  "Image models": 0.0,
  "Audio models": 0.0
}

I'm puzzled by the discrepancy between the calculated cost and the billed amount. Is there an aspect I'm overlooking, such as additional fees not covered in the token cost calculation? Any advice or insights would be much appreciated!

spare sparrow
#

gpt-3.5-turbo-0125 or gpt-3.5-turbo

Any model, but I need pricing to calculate the function as per openai document.
Only then can I provide the perfect pricing to my customers.

Please help me solve this problem.

spare sparrow
#
function calculateOpenAICost(promptTokens: number, completionTokens: number, perCallCharge = 0.0008): string {
  // Pricing details per 1000 tokens
  const inputTokenCost = 0.0005; // $0.0005 per 1000 prompt (input) tokens
  const outputTokenCost = 0.0015; // $0.0015 per 1000 completion (output) tokens
  const perCallCost = perCallCharge; // $0.0008 per call

  // Convert token counts to thousands to apply the cost per 1000 tokens
  const promptTokensInThousands = promptTokens / 1000;
  const completionTokensInThousands = completionTokens / 1000;

  // Calculate costs
  const promptCost = promptTokensInThousands * inputTokenCost;
  const completionCost = completionTokensInThousands * outputTokenCost;
  const totalCost = promptCost + completionCost + perCallCost; // Include per call charge in the total cost

  // Return the total cost in a fixed decimal format, converted to a string
  return totalCost.toFixed(5); // Adjust the decimal places as needed
}

// Example usage
const usage = { prompt_tokens: 138, completion_tokens: 18 };
console.log(
  `Total Cost: $${calculateOpenAICost(
    usage.prompt_tokens,
    usage.completion_tokens
  )}`,
);

as per this website.

https://docsbot.ai/tools/gpt-openai-api-pricing-calculator

DocsBot AI

Calculate and compare the cost of using OpenAI, Azure, Anthropic, Llama 2, Google Gemini, Mistral, and Cohere APIs with our powerful FREE pricing calculator.

#

it's too not working.

#

code output cost: $0.00089 (for 138 prompt tokens and 18 completion tokens)

Billed amount: $0.01065 (from billing data export)

it's not matching. why?

sonic stump
#

each message also costs also few tokens on top of the content

spare sparrow
#

Thanks for your prompt response! Let me share my code with payload data.