{"id":25082361,"url":"https://github.com/codergautam/openai-cost-calculator","last_synced_at":"2025-04-15T08:16:29.590Z","repository":{"id":183586255,"uuid":"670202706","full_name":"codergautam/openai-cost-calculator","owner":"codergautam","description":"Easily calculate the cost of your OpenAI Usage","archived":false,"fork":false,"pushed_at":"2023-07-25T03:27:10.000Z","size":6,"stargazers_count":6,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T08:16:20.847Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codergautam.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-07-24T14:13:49.000Z","updated_at":"2024-11-07T17:28:43.000Z","dependencies_parsed_at":"2023-07-25T03:15:11.719Z","dependency_job_id":"dd02199e-9bde-4103-a5f9-5ae7f4136b46","html_url":"https://github.com/codergautam/openai-cost-calculator","commit_stats":null,"previous_names":["codergautam/openai-cost-calculator"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codergautam%2Fopenai-cost-calculator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codergautam%2Fopenai-cost-calculator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codergautam%2Fopenai-cost-calculator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codergautam%2Fopenai-cost-calculator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codergautam","download_url":"https://codeload.github.com/codergautam/openai-cost-calculator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249031814,"owners_count":21201357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-02-07T05:29:55.055Z","updated_at":"2025-04-15T08:16:29.560Z","avatar_url":"https://github.com/codergautam.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# openai-cost-calculator\n\n`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.\n\n## Installation\n\n**Using npm:**\n```\nnpm install openai-cost-calculator\n```\n\n**Using yarn:**\n```\nyarn add openai-cost-calculator\n```\n\n## Importing\n\n**CommonJS:**\n```javascript\nconst costCalculator = require('openai-cost-calculator');\n```\n\n**ES6:**\n```javascript\nimport * as costCalculator from 'openai-cost-calculator';\n```\n\n## Functions\n\n### 1. calculateLanguageModelCost\n\n**Usage:**\n```javascript\nconst cost = costCalculator.calculateLanguageModelCost('gpt-4', { promptTokens: 5000, completionTokens: 10000 });\nconsole.log(cost);\n```\n\n**Example Output:**\n```json\n{\n  \"promptCost\": 0.15,\n  \"completionCost\": 0.3,\n  \"totalCost\": 0.45,\n  \"formattedTotalCost\": \"$0.45\"\n}\n```\n\n# Combining openai-gpt-token-counter with openai-cost-calculator\n\nYou can combine the `openai-gpt-token-counter` module with this one to estimate the cost of processing text using a specific OpenAI model.\n\nBefore we begin, make sure that you have `openai-gpt-token-counter` installed. If not, you can install it using npm:\n```shell\nnpm install openai-gpt-token-counter\n```\n\n## Usage\n```javascript\n// Importing the required modules\nconst tokenCounter = require('openai-gpt-token-counter');\nconst costCalculator = require('openai-cost-calculator');\n\nconst messages = [\n  { role: \"user\", content: \"This is a test prompt to test out the cost calculation functionality of openai-cost-calculator\" },\n  // Add more messages if needed\n];\n\nconst model = \"gpt-4\"; // Replace with your desired OpenAI chat model\n\nconst tokenCount = tokenCounter.chat(messages, model);\nconsole.log(`Token count: ${tokenCount}`);\n\n// Calculate the cost of processing the text\nconst cost = costCalculator.calculateLanguageModelCost(model, { promptTokens: tokenCount });\nconsole.log(`Processing cost: ${cost.formattedTotalCost}`);\n```\n\nThis 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.\n\nRemember 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`.\n\nWith this approach, you can estimate the cost of using OpenAI models in your projects and ensure that you stay within your budget.\n\n---\n\n### 2. calculateFineTuningModelCost\n\n**Usage:**\n```javascript\n// 1st Param - Model\n// 2nd Param - Number of tokens\n// 3rd Param - Operation, can be either 'Training' or 'Usage'\nconst cost = costCalculator.calculateFineTuningModelCost('ada', 10000, 'Training');\nconsole.log(cost);\n```\n\n**Example Output:**\n```json\n{\n  \"totalCost\": 0.004,\n  \"formattedTotalCost\": \"$0.004\"\n}\n```\n\n### 3. calculateEmbeddingModelCost\n\n**Usage:**\n```javascript\n// 1st Param - Model\n// 2nd Param - Number of tokens\nconst cost = costCalculator.calculateEmbeddingModelCost('ada', 10000);\nconsole.log(cost);\n```\n\n**Example Output:**\n```json\n{\n  \"totalCost\": 0.001,\n  \"formattedTotalCost\": \"$0.001\"\n}\n```\n\n### 4. calculateImageModelCost\n\n**Usage:**\n```javascript\n// 1st Param - Resolution\n// 2nd Param - Count of Images\nconst cost = costCalculator.calculateImageModelCost('512x512', 1);\nconsole.log(cost);\n```\n\n**Example Output:**\n```json\n{\n  \"totalCost\": 0.36,\n  \"formattedTotalCost\": \"$0.36\"\n}\n```\n\n### 5. calculateAudioModelCost\n\n**Usage:**\n```javascript\n// 1st Param - Model\n// 2nd Param - Duration in seconds\nconst cost = costCalculator.calculateAudioModelCost('whisper-1', 60);\nconsole.log(cost);\n```\n\n**Example Output:**\n```json\n{\n  \"totalCost\": 0.36,\n  \"formattedTotalCost\": \"$0.36\"\n}\n```\n\n## Available Models\n\nThis library supports a wide range of models:\n\n- **Language Models**: gpt-4, gpt-4-0314, gpt-4-0613, gpt-3.5-turbo, etc. (all models)\n- **Fine Tuning Models**: ada, babbage, curie, davinci\n- **Embedding Models**: ada\n- **Image Models**: Resolution(1024x1024, 512x512, 256x256)\n- **Audio Models**: whisper-1\n\n## Contributions\n\nContributions to improve `openai-cost-calculator` are more than welcome. Feel free to submit a pull request or report an issue on our [GitHub repository](https://github.com/codergautam/openai-cost-calculator).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodergautam%2Fopenai-cost-calculator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodergautam%2Fopenai-cost-calculator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodergautam%2Fopenai-cost-calculator/lists"}