/**
 * Model Pricing
 *
 * Pricing data for supported models and cost calculation utilities.
 * Prices are in USD per 1 million tokens.
 *
 * @module cost/pricing
 */
/**
 * Model pricing data (USD per 1M tokens)
 * Updated as of 2024
 */
export const MODEL_PRICING = {
    // Anthropic Claude 3.x
    "claude-3-opus": {
        provider: "anthropic",
        inputPer1M: 15.0,
        outputPer1M: 75.0,
        cachedInputPer1M: 1.5,
    },
    "claude-3-sonnet": {
        provider: "anthropic",
        inputPer1M: 3.0,
        outputPer1M: 15.0,
        cachedInputPer1M: 0.3,
    },
    "claude-3-haiku": {
        provider: "anthropic",
        inputPer1M: 0.25,
        outputPer1M: 1.25,
        cachedInputPer1M: 0.03,
    },
    // Anthropic Claude 3.5
    "claude-3.5-sonnet": {
        provider: "anthropic",
        inputPer1M: 3.0,
        outputPer1M: 15.0,
        cachedInputPer1M: 0.3,
    },
    "claude-3.5-haiku": {
        provider: "anthropic",
        inputPer1M: 1.0,
        outputPer1M: 5.0,
        cachedInputPer1M: 0.1,
    },
    // OpenAI GPT-4
    "gpt-4": {
        provider: "openai",
        inputPer1M: 30.0,
        outputPer1M: 60.0,
    },
    "gpt-4-turbo": {
        provider: "openai",
        inputPer1M: 10.0,
        outputPer1M: 30.0,
    },
    "gpt-4o": {
        provider: "openai",
        inputPer1M: 2.5,
        outputPer1M: 10.0,
    },
    "gpt-4o-mini": {
        provider: "openai",
        inputPer1M: 0.15,
        outputPer1M: 0.6,
    },
    "gpt-3.5-turbo": {
        provider: "openai",
        inputPer1M: 0.5,
        outputPer1M: 1.5,
    },
    // Google Gemini
    "gemini-1.5-pro": {
        provider: "google",
        inputPer1M: 1.25,
        outputPer1M: 5.0,
    },
    "gemini-1.5-flash": {
        provider: "google",
        inputPer1M: 0.075,
        outputPer1M: 0.3,
    },
    "gemini-1.0-pro": {
        provider: "google",
        inputPer1M: 0.5,
        outputPer1M: 1.5,
    },
};
/**
 * Get model provider from model ID
 */
export function getModelProvider(model) {
    if (model.startsWith("claude")) {
        return "anthropic";
    }
    if (model.startsWith("gpt")) {
        return "openai";
    }
    if (model.startsWith("gemini")) {
        return "google";
    }
    return "anthropic"; // Default fallback
}
/**
 * Get pricing for a model
 */
export function getModelPricing(model) {
    return MODEL_PRICING[model] || MODEL_PRICING["claude-3.5-sonnet"];
}
/**
 * Calculate cost for token usage
 */
export function calculateCost(model, usage) {
    const pricing = getModelPricing(model);
    const inputCost = (usage.inputTokens / 1_000_000) * pricing.inputPer1M;
    const outputCost = (usage.outputTokens / 1_000_000) * pricing.outputPer1M;
    const cachedInputCost = usage.cachedInputTokens
        ? (usage.cachedInputTokens / 1_000_000) * (pricing.cachedInputPer1M || pricing.inputPer1M)
        : 0;
    return {
        inputCost,
        outputCost,
        cachedInputCost,
        totalCost: inputCost + outputCost + cachedInputCost,
    };
}
/**
 * Estimate cost for a given number of tokens
 * If outputTokens is not provided, assumes 80% input, 20% output ratio
 */
export function estimateCost(model, inputTokens, outputTokens) {
    const actualOutputTokens = outputTokens ?? Math.floor(inputTokens * 0.25);
    const totalTokens = inputTokens + actualOutputTokens;
    const cost = calculateCost(model, {
        inputTokens,
        outputTokens: actualOutputTokens,
        totalTokens,
    });
    return cost.totalCost;
}
/**
 * Format cost as USD string
 */
export function formatCost(cost) {
    if (cost < 0.01) {
        return `$${(cost * 100).toFixed(3)}¢`;
    }
    return `$${cost.toFixed(4)}`;
}
/**
 * Format tokens with K/M suffix
 */
export function formatTokens(tokens) {
    if (tokens >= 1_000_000) {
        return `${(tokens / 1_000_000).toFixed(2)}M`;
    }
    if (tokens >= 1_000) {
        return `${(tokens / 1_000).toFixed(1)}K`;
    }
    return tokens.toString();
}
/**
 * Estimate tokens for text content
 * Uses rough approximation of ~4 characters per token
 */
export function estimateTokens(text) {
    // Rough approximation: 1 token ≈ 4 characters for English text
    // This is a simplification - actual tokenization varies by model
    return Math.ceil(text.length / 4);
}
/**
 * Get all supported models
 */
export function getSupportedModels() {
    return Object.keys(MODEL_PRICING);
}
/**
 * Get models by provider
 */
export function getModelsByProvider(provider) {
    return getSupportedModels().filter((model) => getModelProvider(model) === provider);
}
/**
 * Get cheapest model for a provider
 */
export function getCheapestModel(provider) {
    const models = provider ? getModelsByProvider(provider) : getSupportedModels();
    return models.reduce((cheapest, model) => {
        const cheapestPricing = getModelPricing(cheapest);
        const modelPricing = getModelPricing(model);
        const cheapestAvg = (cheapestPricing.inputPer1M + cheapestPricing.outputPer1M) / 2;
        const modelAvg = (modelPricing.inputPer1M + modelPricing.outputPer1M) / 2;
        return modelAvg < cheapestAvg ? model : cheapest;
    }, models[0]);
}
//# sourceMappingURL=pricing.js.map