OpenAI API Pricing and How to Calculate Cost Automatically
OpenAI API is a popular tool for accessing AI services like ChatGPT and DALL·E 3. However, usage requires payment. This guide covers the pricing for each OpenAI model and explains how to automatically calculate token usage and costs.
OpenAI Pricing Breakdown
- GPT-4o: Fastest, best vision, and multilingual performance. Available to paying customers.
- GPT-4 Turbo: Priced at $10.00 per 1M input tokens and $30.00 per 1M output tokens.
- GPT-4: Standard version at $30.00 per 1M tokens input and $60.00 per 1M tokens output. Advanced gpt-4–32k is priced at $60.00 per 1M input tokens and $120.00 per 1M output tokens.
- GPT-3.5 Turbo: Two models, with pricing ranging from $0.5 to $2 per 1M tokens.
- Assistants API: Prices based on token fees and additional charges for features like file storage.
- Fine-tuning Models: Fees vary based on model and token usage.
- Embedding and Base Models: Affordable pricing, with examples like text-embedding-3-small at $0.02 per 1M tokens.
- Image Generation Models (DALL·E 3): Costs vary by resolution, from $0.02 to $0.08 per image.
- Voice Models: Whisper and TTS have specific per-minute and per-model costs.
Using Apidog to Calculate Openai API Cost Automatically
Apidog simplifies API development and token cost calculations. Follow these steps to set up automated cost calculation:
Step 1: Preparation
- Install the OpenAI GPT Token Counter Library: Convert text to token counts during API debugging.
Examples of Node.js code:
const openaiTokenCounter = require('openai-gpt-token-counter');
const text = process.argv[2]; // Get the test content from command line arguments
const model = "gpt-4"; // Replace with the OpenAI model you want to useconst tokenCount = openaiTokenCounter.text(text, model);
const characterCount = text.length; // Calculate the number of charactersconsole.log(`${tokenCount}`);
You should then rename the Node.js script as gpt-tokens-counter.js
, and place it in the external program directory of Apidog for calling.
Next, you will need to install OpenAI GPT Token Counter on your computer. To do so, you can use the following command in your terminal:
npm install openai-gpt-token-counter
- Use Real-Time Exchange Rate API: Obtain real-time currency conversion rates. (here we will call the Currencylayer API to get the real-time exchange rate, so you need to sign up first to get the API key.)
Step 2: Converting Input Values into Tokens Using Apidog
Input values can be understood as questions and prompts when provided by the user during the query of the AI application. To gain advantage of this, a custom script needs to be added in the Pre-Processors to extract the query
parameter from the request body
, followed by its conversion to token values.
This is the sample code for adding the token value conversion script in the Pre-Processors section:
try {
var jsonData = JSON.parse(pm.request.body.raw);
var content = jsonData.messages[0].content; // obtains the content of messages
var result_input_tokens_js = pm.execute('./gpt-tokens/gpt-tokens-counter.js',[content])
console.log(content);
pm.environment.set("RESULT_INPUT_TOKENS", result_input_tokens_js);
console.log("Input Tokens count: " + pm.environment.get("RESULT_INPUT_TOKENS"));
} catch (e) {
console.log(e);
}
After pressing Send
, the calculated input values should be visible in the Apidog console section.
Step3: Convert Tokens into JPY Cost
After obtaining the value of Tokens consumed from the input, it is necessary to request a real-time exchange rate API to obtain a conversion factor. This factor is then multiplied by the Tokens value to calculate the actual cost in JPY. Add the following script to the pre-operation:
pm.sendRequest("http://apilayer.net/api/live?access_key=YOUR-API-KEY¤cies=JPY&source=USD&format=1", (err, res) => {
if (err) {
console.log(err);
} else {
const quotes = res.json().quotes;
const rate = parseFloat(quotes.USDJPY).toFixed(3);
pm.environment.set("USDJPY_RATE", rate);
var USDJPY_RATE = pm.environment.get("USDJPY_RATE");
// Retrieve the RESULT_INPUT_TOKENS variable from the previous script
var RESULT_INPUT_TOKENS = pm.environment.get("RESULT_INPUT_TOKENS");
// Calculate the tokens exchange rate value
const tokensExchangeRate = 0.03; // Price of 1000 tokens in USD (with GPT-4-8k context input pricing as reference) // Calculate the estimated price in JPY
const JPYPrice = ((RESULT_INPUT_TOKENS / 1000) * tokensExchangeRate * USDJPY_RATE).toFixed(2); pm.environment.set("INPUT_PRICE", JPYPrice); console.log("Estimated cost: " + "¥" + JPYPrice);
}
});
Step 4: Extracting Response from API Request
Apidog automatically parses the returned data as an SSE (Server-Sent Events) event when the content-type parameter in the response returned by the API contains something like text/event-stream
.
Begin by going to the Post-Processors section in the API definition and add a custom script for extracting the response content and concatenation completion.
// Get the response text
const text = pm.response.text()
// Split the text into lines
var lines = text.split('\n');
// Create an empty array to store the "content" parameter
var contents = [];
// Iterate through each line
for (var i = 0; i < lines.length; i++) {
const line = lines[i];
// Skip lines that do not start with "data:"
if (!line.startsWith('data:')) {
continue;
}
// Try to parse the JSON data
try {
var data = JSON.parse(line.substring(5).trim()); // Remove the leading "data: "
// Get the "content" parameter from the "choices" array and add it to the array
contents.push(data.choices[0].delta.content);
} catch (e) {
// Ignore the current line if it is not valid JSON data
}
}
// Join the "content" parameters using the join() method
var result = contents.join('');
// Display the result in the "Visualize" tab of the body
pm.visualizer.set(result);
// Print the result to the console
console.log(result);
After creating the request, you can retrieve the complete response content in the console!
Step5: Converting Tokens from Output Value Using Apidog
Once you have received the response content, it is necessary to convert it into the Tokens value. This is made possible with a third-party library.
Add the custom script in the post-processing operation so Apidog can call the external gpt-toejsn-counter.js
script to obtain the Tokens value.
Use this page to see the specific code: openai-gpt-token-counter
With the numbers you obtain from the console, you can estimate how much it will cost!
// Get the response text
const text = pm.response.text()
// Split the text into lines
var lines = text.split('\n');
// Create an empty array to store the "content" parameter
var contents = [];
// Iterate through each line
for (var i = 0; i < lines.length; i++) {
const line = lines[i];
// Skip lines that do not start with "data:"
if (!line.startsWith('data:')) {
continue;
}
// Try to parse the JSON data
try {
var data = JSON.parse(line.substring(5).trim()); // Remove the leading "data: "
// Get the "content" parameter from the "choices" array and add it to the array
contents.push(data.choices[0].delta.content);
} catch (e) {
// Ignore the current line if it is not valid JSON data
}
}
// Join the "content" parameters using the join() method
var result = contents.join('');
// Display the result in the "Visualize" tab of the body
pm.visualizer.set(result);
// Print the result to the console
console.log(result);
// Calculate the number of output tokens.
var RESULT_OUTPUT_TOKENS = pm.execute('./gpt-tokens/gpt-tokens-counter.js', [result])
pm.environment.set("RESULT_OUTPUT_TOKENS", RESULT_OUTPUT_TOKENS);console.log("Output Tokens count: " + pm.environment.get("RESULT_OUTPUT_TOKENS"));
Step 6: Convert Output Tokens into JPY Cost
Similar to the cost calculation scheme mentioned in the previous section, the actual cost (JPY) is obtained by multiplying the Tokens value with the exchange rate.
Add the following script in the post-processing operation:
pm.sendRequest("http://apilayer.net/api/live?access_key=YOUR-API-KEY¤cies=JPY&source=USD&format=1", (err, res) => {
if (err) {
console.log(err);
} else {
const quotes = res.json().quotes;
const rate = parseFloat(quotes.USDJPY).toFixed(3);
pm.environment.set("USDJPY_RATE", rate);
var USDJPY_RATE = pm.environment.get("USDJPY_RATE");
// Get the RESULT_OUTPUT_TOKENS variable from the previous postman script
var RESULT_OUTPUT_TOKENS = pm.environment.get("RESULT_OUTPUT_TOKENS");
// Calculate tokens exchange rate
const tokensExchangeRate = 0.06; // USD price per 1000 tokens (based on GPT-4-8k context input pricing) // Calculate estimated price in JPY
const JPYPrice = ((RESULT_OUTPUT_TOKENS / 1000) * tokensExchangeRate * USDJPY_RATE).toFixed(2); pm.environment.set("OUTPUT_PRICE", JPYPrice); console.log("Output cost (JPY): " + JPYPrice + "円");
}
});
Step 7: Calculate the Total Cost in JPY
inally, add a custom script in the post-processing phase that can automatically calculate the total cost of inputs and outputs.
// Summing up input and output costs
const INPUTPrice = Number(pm.environment.get("INPUT_PRICE"));
// Get the input price variable and convert it to a numberconst OUTPUTPrice = Number(pm.environment.get("OUTPUT_PRICE"));
// Get the output price variable and convert it to a numberconsole.log("Total cost: " + "¥" + (INPUTPrice + OUTPUTPrice));
// Print the total cost: the sum of the input price and output price.
Allowing to estimate the approximate cost of the current request during the process of debugging the API.