handonweb-openai-costs
v1.0.0
Published
Calculate OpenAI API costs before making calls. Supports GPT-4o, GPT-4.1, o3, o4-mini, GPT-5, and more.
Downloads
99
Maintainers
Readme
handonweb-openai-costs
Calculate OpenAI API costs before making calls. Supports all current models including GPT-4o, GPT-4.1, o3, o4-mini, GPT-5, embeddings, Whisper, TTS, and DALL-E 3. Zero dependencies.
Install
npm install handonweb-openai-costsUsage
const { calculateCost, listModels, getModelPricing } = require('handonweb-openai-costs');
// Calculate cost for 1000 input + 500 output tokens
const cost = calculateCost('gpt-4o', 1000, 500);
console.log(cost);
// { inputCost: 0.0000025, outputCost: 0.000005, totalCost: 0.0000075, currency: 'USD' }
// Check a million tokens on GPT-4o-mini
const big = calculateCost('gpt-4o-mini', 1_000_000, 1_000_000);
console.log(`Total: $${big.totalCost}`); // Total: $0.75
// List all supported models
console.log(listModels());
// ['gpt-4o', 'gpt-4o-mini', 'gpt-4.1', ...]
// Get raw pricing for a model
const pricing = getModelPricing('gpt-4.1');
console.log(pricing);
// { inputPer1M: 2.00, outputPer1M: 8.00, description: '...' }Supported Models
| Model | Input (per 1M tokens) | Output (per 1M tokens) | |-------|----------------------|------------------------| | gpt-4o | $2.50 | $10.00 | | gpt-4o-mini | $0.15 | $0.60 | | gpt-4.1 | $2.00 | $8.00 | | gpt-4.1-mini | $0.40 | $1.60 | | gpt-4.1-nano | $0.10 | $0.40 | | gpt-5 | $10.00 | $30.00 | | gpt-5-mini | $1.50 | $6.00 | | o3 | $10.00 | $40.00 | | o3-mini | $1.10 | $4.40 | | o4-mini | $1.10 | $4.40 | | gpt-4-turbo | $10.00 | $30.00 | | gpt-3.5-turbo | $0.50 | $1.50 | | text-embedding-3-small | $0.02 | — | | text-embedding-3-large | $0.13 | — |
Plus whisper, tts-1, and dall-e-3 with their own pricing structures.
API
calculateCost(model, inputTokens, outputTokens?)
Returns { inputCost, outputCost, totalCost, currency }. Throws if the model is unknown.
listModels()
Returns an array of all supported model name strings.
getModelPricing(model)
Returns the raw pricing object for a model, or null if not found.
About Hand On Web
We build AI chatbots, voice agents, and automation tools for businesses.
- 🌐 handonweb.com
- 📧 [email protected]
- 📍 Chester, UK
Licence
MIT
