/**
 * pricing.ts
 *
 * Model pricing lookup and cost calculation for OpenAI and Anthropic models.
 * Used by custom providers (agentic, instrumented) to calculate
 * per-request cost from token counts.
 *
 * Prices are per-token (not per-million) for simpler arithmetic.
 * Source: https://openai.com/api/pricing/ and https://docs.anthropic.com/en/docs/about-claude/models
 */
// ---------------------------------------------------------------------------
// Pricing table
// ---------------------------------------------------------------------------
/**
 * Per-token pricing for supported models.
 * Prices are in USD. Update when providers change pricing or new models
 * are added to models.yaml.
 */
// Source: https://openai.com/api/pricing/ and https://docs.anthropic.com/en/docs/about-claude/models
const MODEL_PRICING = {
    // Anthropic models
    "claude-3.5-sonnet-20241022": {
        input: 3 / 1_000_000,
        output: 15 / 1_000_000,
    },
    "claude-opus-4-5-20251101": { input: 15 / 1_000_000, output: 75 / 1_000_000 },
    "claude-opus-4-6": { input: 15 / 1_000_000, output: 75 / 1_000_000 },
    "claude-opus-4-8": { input: 15 / 1_000_000, output: 75 / 1_000_000 },
    "claude-sonnet-4-20250514": { input: 3 / 1_000_000, output: 15 / 1_000_000 },
    "claude-sonnet-4-6": { input: 3 / 1_000_000, output: 15 / 1_000_000 },
    // OpenAI models
    "gpt-4-turbo": { input: 10 / 1_000_000, output: 30 / 1_000_000 },
    "gpt-4.1": { input: 2 / 1_000_000, output: 8 / 1_000_000 },
    "gpt-4.1-mini": { input: 0.4 / 1_000_000, output: 1.6 / 1_000_000 },
    "gpt-4.1-nano": { input: 0.1 / 1_000_000, output: 0.4 / 1_000_000 },
    "gpt-4o": { input: 2.5 / 1_000_000, output: 10 / 1_000_000 },
    "gpt-4o-mini": { input: 0.15 / 1_000_000, output: 0.6 / 1_000_000 },
    "gpt-5-2025-08-07": { input: 10 / 1_000_000, output: 30 / 1_000_000 },
    "gpt-5.2": { input: 2 / 1_000_000, output: 8 / 1_000_000 },
    "gpt-5.4": { input: 2 / 1_000_000, output: 8 / 1_000_000 },
    "o3-mini": { input: 1.1 / 1_000_000, output: 4.4 / 1_000_000 },
};
// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------
/**
 * Calculate the cost in USD for a given model and token usage.
 * Returns 0 if the model is not in the pricing table.
 */
export function calculateCost(model, promptTokens, completionTokens) {
    const pricing = lookupPricing(model);
    if (!pricing)
        return 0;
    return pricing.input * promptTokens + pricing.output * completionTokens;
}
/**
 * Format a cost value as a human-readable USD string.
 * - Under $0.01: shows 4 decimal places (e.g., "$0.0023")
 * - Under $1: shows 2 decimal places (e.g., "$0.45")
 * - $1+: shows 2 decimal places (e.g., "$12.50")
 */
export function formatCost(cost) {
    if (cost === 0)
        return "$0.00";
    if (cost < 0.01)
        return `$${cost.toFixed(4)}`;
    return `$${cost.toFixed(2)}`;
}
/**
 * Look up pricing for a model. Supports exact matches and prefix
 * matching (e.g., "gpt-4o-2024-08-06" matches "gpt-4o").
 * Returns undefined if no pricing is found.
 */
export function lookupPricing(model) {
    // Exact match first
    if (MODEL_PRICING[model])
        return MODEL_PRICING[model];
    // Prefix match: "gpt-4o-2024-08-06" → "gpt-4o"
    for (const [key, pricing] of Object.entries(MODEL_PRICING)) {
        if (model.startsWith(key))
            return pricing;
    }
    return undefined;
}
