Realtime API cost mismatch between the bill and the calculated cost

Hello everybody,

I’ve realized OpenAI has billed me around twice as cheap as the cost I calculated based on number usage.

I’m not sure if this is a bug or if I missed something in my project dashboard.

For the calculation, what I do is to accumulate all the tokens response.done, namely the following information:

{
  "usage": {
    "total_tokens": 9492,
    "input_tokens": 8223,
    "output_tokens": 1269,
    "input_token_details": {
      "text_tokens": 5378,
      "audio_tokens": 2845,
      "cached_tokens": 8192,
      "cached_tokens_details": {
        "text_tokens": 5376,
        "audio_tokens": 2816
      }
    },
    "output_token_details": {
      "text_tokens": 250,
      "audio_tokens": 1019
    }
  }
}

and then to calculate at the end of the conversation the total cost using the given pricing.

To see the cost that was billed, I go to the usage tab on my dashboard:

I tried comparing all the calls that I made to the gpt-4o-mini-realtime-preview-2024-12-17 model over the last three days. The details are as follows (for all calls combined of the day):

  • Jan 27th: Calculated: $0.86, Billed: $0.41.
  • Jan 26th: Calculated: $0.19, Billed: $0.12.
  • Jan 25th: Calculated: $1.23, Billed: $0.50.

What did I do wrong please? Thank you very much in advance for your help!

1 Like

+1 to this. I’m looking for the best way to calculate usage in order to pass the cost on to users.

I have two problems;

  1. I swear I’m being double charged.
  2. securely transmitting usage server-side (without client-side meddling)

This is how I’m roughly calculating usage;

case 'response.done':
  let u = message.response.usage
  let input_tokens = u.input_token_details.audio_tokens + u.input_token_details.text_tokens
  let input_tokens_cached = u.input_token_details.cached_tokens
  let output_tokens = u.output_tokens

  // Post token usage to billing endpoint
  if (this.hasBillingUrlValue) {
    const modelName = this.sessionModel || this.transcriptionModel || this.modelValue || 'gpt-4o-mini-realtime-preview';
    this.postTokenUsage({
      model_name: modelName,
      input_tokens: input_tokens,
      input_tokens_cached: input_tokens_cached,
      output_tokens: output_tokens
    });
  }

And on the server side I calc cost like this using rates here https://platform.openai.com/docs/pricing

Cost = ((input_tokens × input_rate) + 
       (input_tokens_cached × cached_rate) + 
       (output_tokens × output_rate)) ÷ 1,000,000

Question 1: I can’t get costs to add up right. Am I doing something wrong? Is there a way to just get per user session costs via API, rather than trying to total tokens and deriving costs?

Question 2: If not, how do I secure client side billing when the user can always meddle with JS to fiddle with token usage? Just obfuscation, SRI and CSP?