UNPKG

@yogesh0333/yogiway-prompt

Version:

Free & Open Source Prompt Optimization Library - Save 30-50% on AI API costs. Multi-language, multi-platform support.

99 lines (98 loc) 3.85 kB
"use strict"; /** * Cost Calculator - Estimate API costs for different providers */ Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateCost = calculateCost; exports.compareCosts = compareCosts; exports.calculateSavings = calculateSavings; const tokenizer_1 = require("./tokenizer"); // Model pricing (per 1M tokens) // Note: Pricing may change - verify with provider documentation // Last updated: 2025 const MODEL_PRICING = { // OpenAI "gpt-5": { input: 50.0, output: 150.0, note: "Latest model - verify current pricing", }, "gpt-4": { input: 30.0, output: 60.0, note: "Verify current pricing" }, "gpt-4-turbo": { input: 10.0, output: 30.0, note: "Verify current pricing" }, "gpt-3.5-turbo": { input: 0.5, output: 1.5, note: "Verify current pricing" }, "gpt-4o": { input: 5.0, output: 15.0, note: "Verify current pricing" }, // Anthropic "claude-3-opus": { input: 15.0, output: 75.0, note: "Verify current pricing", }, "claude-3-sonnet": { input: 3.0, output: 15.0, note: "Verify current pricing", }, "claude-3-haiku": { input: 0.25, output: 1.25, note: "Verify current pricing", }, // Google "gemini-pro": { input: 0.5, output: 1.5, note: "Verify current pricing" }, "gemini-ultra": { input: 1.25, output: 5.0, note: "Verify current pricing" }, // Mistral "mistral-large": { input: 2.7, output: 8.1, note: "Verify current pricing" }, "mistral-medium": { input: 2.7, output: 8.1, note: "Verify current pricing" }, // Cohere command: { input: 1.0, output: 2.0, note: "Verify current pricing" }, }; /** * Calculate cost estimate for a prompt */ function calculateCost(prompt, provider = "openai", model = "gpt-5", estimatedOutputTokens) { const inputTokens = (0, tokenizer_1.countTokens)(prompt, provider); const outputTokens = estimatedOutputTokens || (0, tokenizer_1.estimateOutputTokens)(inputTokens, provider); // Get pricing for model const modelPricing = MODEL_PRICING[model] || MODEL_PRICING["gpt-5"]; const pricing = { input: modelPricing.input, output: modelPricing.output }; const inputCost = (inputTokens / 1000000) * pricing.input; const outputCost = (outputTokens / 1000000) * pricing.output; const totalCost = inputCost + outputCost; return { provider, model, inputTokens, estimatedOutputTokens: outputTokens, inputCost: Math.round(inputCost * 10000) / 10000, // Round to 4 decimals outputCost: Math.round(outputCost * 10000) / 10000, totalCost: Math.round(totalCost * 10000) / 10000, currency: "USD", }; } /** * Compare costs across multiple providers */ function compareCosts(prompt, models) { return models .map(({ provider, model }) => calculateCost(prompt, provider, model)) .sort((a, b) => a.totalCost - b.totalCost); } /** * Calculate savings from optimization */ function calculateSavings(originalPrompt, optimizedPrompt, provider = "openai", model = "gpt-5", requestsPerDay = 1000) { const originalCost = calculateCost(originalPrompt, provider, model); const optimizedCost = calculateCost(optimizedPrompt, provider, model); const perRequest = originalCost.totalCost - optimizedCost.totalCost; const perDay = perRequest * requestsPerDay; const perMonth = perDay * 30; const perYear = perMonth * 12; const percentage = (perRequest / originalCost.totalCost) * 100; return { perRequest: Math.round(perRequest * 10000) / 10000, perDay: Math.round(perDay * 100) / 100, perMonth: Math.round(perMonth * 100) / 100, perYear: Math.round(perYear * 100) / 100, percentage: Math.round(percentage * 100) / 100, }; }