by: Maria de los Ángeles Giménez

We need to understand that the cost of using the OpenAI API is complex. Although we will end with a formula for the calculation of the cost, everything depends on the following:

·  If we have free credits

·  The model that we use

·  If we use voice, image, or text

·  The type of response

·  If we use an assistant or not

Also, know that the formula is based on tokens, not words. Although I will add a simple way to transform words into tokens, this transformation depends on the language and the special characters in the text.

The cost contains two parts: the input text and the output text. For the total costs, you need to have the output text present, which could change depending on the accuracy of the model and the type of response. If you ask for a JSON response, it is not the same as the plain text.

Calculate tokens from words

For accuracy, it is recommended to use the library “tiktoken,” but you also could do it by hand.

How to calculate by hand

In general, four characters correspond to one token in English text (if you use another language with special characters or non-latin alphabets, the count could differ). Common words and punctuation are typically one token each.

For example, the sentence “OpenAI’s models are powerful tools for natural language processing” has nine words and 60 characters (spaces don’t count), roughly equating to about 15 tokens.

Using the library “tiktoken”

Although we could use the OpenAI API to count the tokens in a phrase, it is not recommended because of its costs. To use this library, we need to know the model’s encoding. Generally, we use “cl100k_base” for GPT 3.5 or 4.

When we execute this script in Python, we can see that the number of tokens is 12.

As we can see, it is more accurate than the manual approximation but requires some programming knowledge.

Calculate the cost

The first step in calculating the cost is to determine which model to use. Each model has a different cost per 1000 tokens and a different price for input and output.

I will use the GPT-4o for the example because it is the new model, but all the models use the same formula- the only thing changing is the cost.  

If we go to the OpenAI pricing page, we can see that the cost for the GPT-4o is $0,005 per 1000 tokens in the input and $0,0015 per 1000 tokens of output.

The second step is calculating the tokens of the prompt sent to the API call and the response we receive.

We will consider that our prompt contains 500 tokens and the response or output contains 300 tokens.

The formula that we must use for calculating the cost for the input or the output is:

In our hypothetical case, the cost for inputs will be:

The cost for the output will be:

The total cost will be the sum of these two costs:

This is only for one call to the API for text. Simple plain text.

What happens if we use the voice to ask the API questions?

If we want the API’s voice to ask and respond, we need to add a new step to the calculation. From voice, the API internally changes the audio to text, then answers the question and transforms the text that is generated to audio. These two transformations also have a cost, so we need to add these costs to the total cost.

The transformation from audio to text costs $0,006 per minute in the input. So, if we use our example audio of 1 minute, the cost will be $0,006 to pass the audio to text.

In the case of the response in audio, the conversion is by character. The rate in this case is $0,015 per 1000 characters.

If we use our previous example, the cost will be (considering that one minute of audio transforms into 500 tokens for the input and the response is 300 tokens or 1200 characters):

So, using these values in the example below, the final cost will be: $0,006 (COST INPUT AUDIO) + $0,0025 (COST INPUT TEXT) + $0,0045 (COST OUTPUT TEXT) + $0,018 (COST OUTPUT AUDIO) = $0,031

What happens with images?

The price is on the page OpenAI. The cost is per image, and the price depends on the resolution and the model we use.

When we talk about using the API from OpenAI, we must also consider other costs, such as the subscription plan, the legal consultation for compliance costs, the customer support, or the API rate limit upgrade.

Use this formula for a rough cost estimate,  but do not rely on it for the actual cost.