Research
Security News
Kill Switch Hidden in npm Packages Typosquatting Chalk and Chokidar
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
openai-cost-calculator
Advanced tools
openai-cost-calculator
is a reliable npm module that provides cost calculations for various AI models offered by OpenAI. Pricing is maintained regularly to stay on top of OpenAI's pricing changes. Whether you need to calculate costs for OpenAI Language Models, Fine Tuning Models, Embedding Models, Image Models, or Audio Models, this module has got you covered.
Using npm:
npm install openai-cost-calculator
Using yarn:
yarn add openai-cost-calculator
CommonJS:
const costCalculator = require('openai-cost-calculator');
ES6:
import * as costCalculator from 'openai-cost-calculator';
Usage:
const cost = costCalculator.calculateLanguageModelCost('gpt-4', { promptTokens: 5000, completionTokens: 10000 });
console.log(cost);
Example Output:
{
"promptCost": 0.15,
"completionCost": 0.3,
"totalCost": 0.45,
"formattedTotalCost": "$0.45"
}
You can combine the openai-gpt-token-counter
module with this one to estimate the cost of processing text using a specific OpenAI model.
Before we begin, make sure that you have openai-gpt-token-counter
installed. If not, you can install it using npm:
npm install openai-gpt-token-counter
// Importing the required modules
const tokenCounter = require('openai-gpt-token-counter');
const costCalculator = require('openai-cost-calculator');
const messages = [
{ role: "user", content: "This is a test prompt to test out the cost calculation functionality of openai-cost-calculator" },
// Add more messages if needed
];
const model = "gpt-4"; // Replace with your desired OpenAI chat model
const tokenCount = tokenCounter.chat(messages, model);
console.log(`Token count: ${tokenCount}`);
// Calculate the cost of processing the text
const cost = costCalculator.calculateLanguageModelCost(model, { promptTokens: tokenCount });
console.log(`Processing cost: ${cost.formattedTotalCost}`);
This script first counts the number of tokens in a text for a specific OpenAI model using the openai-gpt-token-counter
module. Then, it calculates the cost of processing these tokens using the openai-cost-calculator
module. The result is the estimated cost of processing your text with the chosen OpenAI model.
Remember to replace "This is a test sentence."
with your text and "gpt-4"
with your desired OpenAI model. You can use any model supported by openai-gpt-token-counter
and openai-cost-calculator
.
With this approach, you can estimate the cost of using OpenAI models in your projects and ensure that you stay within your budget.
Usage:
// 1st Param - Model
// 2nd Param - Number of tokens
// 3rd Param - Operation, can be either 'Training' or 'Usage'
const cost = costCalculator.calculateFineTuningModelCost('ada', 10000, 'Training');
console.log(cost);
Example Output:
{
"totalCost": 0.004,
"formattedTotalCost": "$0.004"
}
Usage:
// 1st Param - Model
// 2nd Param - Number of tokens
const cost = costCalculator.calculateEmbeddingModelCost('ada', 10000);
console.log(cost);
Example Output:
{
"totalCost": 0.001,
"formattedTotalCost": "$0.001"
}
Usage:
// 1st Param - Resolution
// 2nd Param - Count of Images
const cost = costCalculator.calculateImageModelCost('512x512', 1);
console.log(cost);
Example Output:
{
"totalCost": 0.36,
"formattedTotalCost": "$0.36"
}
Usage:
// 1st Param - Model
// 2nd Param - Duration in seconds
const cost = costCalculator.calculateAudioModelCost('whisper-1', 60);
console.log(cost);
Example Output:
{
"totalCost": 0.36,
"formattedTotalCost": "$0.36"
}
This library supports a wide range of models:
Contributions to improve openai-cost-calculator
are more than welcome. Feel free to submit a pull request or report an issue on our GitHub repository.
FAQs
Easily calculate the cost of your OpenAI Usage
We found that openai-cost-calculator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.