How to accurately get the cost of each API call?

Hi,

Is there a way to get the cost of each API call accurately? Currently I can only see the costs in the account that is an accumulated value of the whole day.

2 Likes

To do it, you’ll need to calculate it based on the usage reported in the API response.

  "id": "chatcmpl-123",
  "object": "chat.completion",
  "created": 1677652288,
  "model": "gpt-3.5-turbo-0613",
  "system_fingerprint": "fp_44709d6fcb",
  "choices": [{
    "index": 0,
    "message": {
      "role": "assistant",
      "content": "\n\nHello there, how may I assist you today?",
    },
    "logprobs": null,
    "finish_reason": "stop"
  }],
  "usage": {
    "prompt_tokens": 9,
    "completion_tokens": 12,
    "total_tokens": 21
  }
}
1 Like

To do it when using the completions or the chat completions endpoints in their most useful mode (where they immediately begin streaming the output to you), you’ll need to measure the size of input and output language usage in tokens, calculated by a token encoding library such as tiktoken.

If using assistants…there is no report, nor any calcuation possible of what the AI has been loaded with for context or documents by the back end. No accounting of what it has been performing autonomously with iterative function calls you are billed for.

2 Likes