array( 10.00, 50.00 ), 'openai/gpt-5.6' => array( 5.00, 30.00 ), 'openai/gpt-5.6-terra' => array( 2.00, 12.00 ), 'openai/gpt-5.6-luna' => array( 0.20, 1.20 ), 'openai/gpt-5.6-sol' => array( 5.00, 30.00 ), 'openai/gpt-5.5' => array( 5.00, 30.00 ), 'openai/gpt-5.4' => array( 2.50, 15.00 ), 'openai/gpt-5.4-mini' => array( 0.75, 4.50 ), 'openai/gpt-5.4-nano' => array( 0.20, 1.25 ), 'openai/gpt-4.1' => array( 2.00, 8.00 ), 'openai/gpt-4.1-mini' => array( 0.40, 1.60 ), 'openai/gpt-5' => array( 1.25, 10.00 ), 'openai/gpt-5-mini' => array( 0.25, 2.00 ), 'openai/gpt-5-nano' => array( 0.05, 0.40 ), 'openai/gpt-4o' => array( 2.50, 10.00 ), 'openai/gpt-4o-mini' => array( 0.15, 0.60 ), 'claude/claude-sonnet-5-5' => array( 2.00, 10.00 ), 'claude/claude-opus-5-5' => array( 5.00, 25.00 ), 'claude/claude-haiku-5-5' => array( 1.00, 5.00 ), 'claude/claude-fable-5-1' => array( 10.00, 50.00 ), 'claude/claude-haiku-4-5' => array( 1.00, 5.00 ), 'claude/claude-sonnet-5' => array( 2.00, 10.00 ), 'claude/claude-opus-5' => array( 5.00, 25.00 ), 'claude/claude-fable-5' => array( 10.00, 50.00 ), 'claude/claude-mythos-5' => array( 10.00, 50.00 ), 'claude/claude-opus-4-8' => array( 5.00, 25.00 ), 'claude/claude-opus-4-7' => array( 5.00, 25.00 ), 'claude/claude-opus-4-6' => array( 5.00, 25.00 ), 'claude/claude-sonnet-4-6' => array( 3.00, 15.00 ), 'claude/claude-opus-4-5' => array( 5.00, 25.00 ), 'gemini/gemini-3.8-flash' => array( 0.75, 4.50 ), 'gemini/gemini-3.7-flash' => array( 0.75, 4.50 ), 'gemini/gemma-4-31b-it' => array( 0.00, 0.00 ), 'gemini/gemini-3.6-flash' => array( 1.50, 7.50 ), 'gemini/gemini-3.5-flash' => array( 1.50, 9.00 ), 'gemini/gemini-3.1-pro-preview' => array( 2.00, 12.00 ), 'gemini/gemini-3.1-flash-lite' => array( 0.25, 1.50 ), 'openrouter/anthropic/claude-opus-4.8' => array( 5.00, 25.00 ), 'openrouter/anthropic/claude-opus-4.7' => array( 5.00, 25.00 ), 'openrouter/anthropic/claude-sonnet-4.6' => array( 3.00, 15.00 ), 'openrouter/openai/gpt-5.5' => array( 5.00, 30.00 ), 'openrouter/openai/gpt-5.4-mini' => array( 0.75, 4.50 ), 'openrouter/google/gemini-3.5-flash' => array( 1.50, 9.00 ), 'openrouter/google/gemini-3.1-pro-preview' => array( 2.00, 12.00 ), 'openrouter/google/gemma-4-31b-it:free' => array( 0.00, 0.00 ), 'openrouter/deepseek/deepseek-v3.2' => array( 0.2145, 0.3218 ), 'openrouter/openrouter/free' => array( 0.00, 0.00 ), ); private const INPUT_TOKEN_TIERS = array( 'gemini/gemini-3.1-pro-preview' => array( 'threshold' => 200000, 'rate' => array( 4.00, 18.00 ), ), ); /** * Conservative fallback pricing per provider when no exact model match * is found. Picks the upper-mid tier so estimates skew slightly high * rather than misleadingly low. * * @var array */ private const FALLBACK_PRICING = array( 'openai' => array( 2.50, 10.00 ), 'claude' => array( 3.00, 15.00 ), 'gemini' => array( 1.25, 10.00 ), 'openrouter' => array( 3.00, 10.00 ), ); /** * Calculate the estimated cost of an API call in USD micros (USD × 1,000,000). * * Micros provide 6-decimal precision: a 1200-token call to gpt-5-nano at * $0.05/1M input is $0.00006 = 60 micros, which integer cents (rounded up * to 1¢) would have inflated by ~170×. Modern usage dashboards (AWS Cost * Explorer, OpenAI billing) all store cost in fractional-cent units for * this exact reason. * * @param string $provider Provider id. * @param string $model Model id. * @param int $input_tokens Prompt tokens. * @param int $output_tokens Completion tokens. * @return int Estimated cost in USD micros (USD × 1,000,000), rounded to nearest. */ public static function calculate_cost_micros( string $provider, string $model, int $input_tokens, int $output_tokens ): int { $input_tokens = max( 0, $input_tokens ); $output_tokens = max( 0, $output_tokens ); if ( $input_tokens <= 0 && $output_tokens <= 0 ) { return 0; } $pricing = \AIPT\Core\Services\PricingFeedManager::instance()->resolve( $provider, $model, $input_tokens ); $input_micros = $input_tokens * $pricing[0]; $output_micros = $output_tokens * $pricing[1]; return max( 0, (int) round( $input_micros + $output_micros ) ); } /** * Last-resort hardcoded baseline used by PricingFeedManager when the * remote snapshot is unavailable. Exposed so the manager doesn't reach * into a private constant via reflection. * * @return array */ public static function get_baseline_table(): array { return self::PRICING; } public static function get_baseline_rate( string $provider, string $model, int $input_tokens ): ?array { $key = sprintf( '%s/%s', strtolower( $provider ), strtolower( $model ) ); $rate = self::PRICING[ $key ] ?? null; $tier = self::INPUT_TOKEN_TIERS[ $key ] ?? null; if ( ! is_array( $rate ) ) { return null; } if ( is_array( $tier ) && $input_tokens > (int) ( $tier['threshold'] ?? PHP_INT_MAX ) && isset( $tier['rate'] ) && is_array( $tier['rate'] ) ) { $rate = $tier['rate']; } return array( (float) $rate[0], (float) $rate[1] ); } /** * Provider-level fallback rates for cases where the model id is unknown * to both the remote feed and the baseline table. * * @param string $provider Provider id. * @return array{float, float} */ public static function get_provider_fallback( string $provider ): array { $provider = strtolower( trim( $provider ) ); if ( isset( self::FALLBACK_PRICING[ $provider ] ) ) { return self::FALLBACK_PRICING[ $provider ]; } return self::FALLBACK_PRICING['openai']; } }