USD per 1M tokens). */ final class ModelPricing { /** * Longest key wins ('gpt-4o-mini' must match before 'gpt-4o'). * Blended = rough average of input/output price for typical SEO tasks. */ private const DEFAULT_PRICES = array( 'gpt-4o-mini' => 0.4, 'gpt-4o' => 7.0, 'claude-opus' => 30.0, 'claude-sonnet' => 6.0, 'claude-haiku' => 1.6, 'gemini-1.5-flash' => 0.15, 'gemini-1.5-pro' => 2.5, ); /** Unknown model — a deliberately middle-of-the-road guess. */ private const FALLBACK_PRICE = 3.0; /** An empty stored model means "the provider's default" (M12 defaults). */ private const PROVIDER_DEFAULT_MODELS = array( 'openai' => 'gpt-4o-mini', 'anthropic' => 'claude-opus-4-8', 'gemini' => 'gemini-1.5-flash', // The auto-router picks a model per request, so there is no fixed // price — the FALLBACK_PRICE middle-of-the-road guess applies. 'openrouter' => 'openrouter/auto', ); /** @return array model substring => USD per 1M tokens */ public static function prices(): array { $prices = apply_filters( 'acez_seo_ai_model_prices', self::DEFAULT_PRICES ); return is_array( $prices ) ? array_map( 'floatval', $prices ) : self::DEFAULT_PRICES; } public static function price_per_million( string $provider, string $model ): float { if ( '' === $model ) { $model = self::PROVIDER_DEFAULT_MODELS[ $provider ] ?? ''; } $prices = self::prices(); uksort( $prices, static fn( string $a, string $b ): int => strlen( $b ) <=> strlen( $a ) ); foreach ( $prices as $needle => $price ) { if ( '' !== $model && str_contains( $model, (string) $needle ) ) { return (float) $price; } } return self::FALLBACK_PRICE; } /** Estimated cost in USD for the given (approximate) token count. */ public static function estimate_cost( string $provider, string $model, int $tokens ): float { return max( 0, $tokens ) / 1000000 * self::price_per_million( $provider, $model ); } }