|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - http://moodle.org/ |
| 3 | +// |
| 4 | +// Moodle is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// Moodle is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace local_ai_course_assistant; |
| 18 | + |
| 19 | +/** |
| 20 | + * Token cost manager — rate cards and cost estimation. |
| 21 | + * |
| 22 | + * Rates are USD per 1,000,000 tokens (industry standard as of early 2026). |
| 23 | + * Model strings are matched by prefix (longest match wins) so new dated |
| 24 | + * model variants (e.g. gpt-4o-2024-11-20) are covered automatically. |
| 25 | + * |
| 26 | + * Update the rate card when providers change pricing. |
| 27 | + * |
| 28 | + * @package local_ai_course_assistant |
| 29 | + * @copyright 2025 AI Course Assistant |
| 30 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 31 | + */ |
| 32 | +class token_cost_manager { |
| 33 | + |
| 34 | + /** |
| 35 | + * Rate card: USD per 1,000,000 tokens. |
| 36 | + * Keys are model prefix strings matched with str_starts_with. |
| 37 | + * Values: ['input' => float, 'output' => float]. |
| 38 | + * |
| 39 | + * Prices sourced from provider pricing pages (March 2026). |
| 40 | + */ |
| 41 | + private static array $rate_cards = [ |
| 42 | + // ── OpenAI ──────────────────────────────────────────────────────────── |
| 43 | + 'gpt-4o-mini' => ['input' => 0.15, 'output' => 0.60], |
| 44 | + 'gpt-4o' => ['input' => 2.50, 'output' => 10.00], |
| 45 | + 'gpt-4-turbo' => ['input' => 10.00, 'output' => 30.00], |
| 46 | + 'gpt-4' => ['input' => 30.00, 'output' => 60.00], |
| 47 | + 'gpt-3.5-turbo' => ['input' => 0.50, 'output' => 1.50], |
| 48 | + 'o1-mini' => ['input' => 3.00, 'output' => 12.00], |
| 49 | + 'o1-preview' => ['input' => 15.00, 'output' => 60.00], |
| 50 | + 'o1' => ['input' => 15.00, 'output' => 60.00], |
| 51 | + 'o3-mini' => ['input' => 1.10, 'output' => 4.40], |
| 52 | + 'o3' => ['input' => 10.00, 'output' => 40.00], |
| 53 | + |
| 54 | + // ── Anthropic Claude ────────────────────────────────────────────────── |
| 55 | + 'claude-haiku' => ['input' => 0.80, 'output' => 4.00], |
| 56 | + 'claude-sonnet' => ['input' => 3.00, 'output' => 15.00], |
| 57 | + 'claude-opus' => ['input' => 15.00, 'output' => 75.00], |
| 58 | + |
| 59 | + // ── DeepSeek ────────────────────────────────────────────────────────── |
| 60 | + 'deepseek-chat' => ['input' => 0.14, 'output' => 0.28], |
| 61 | + 'deepseek-reasoner' => ['input' => 0.55, 'output' => 2.19], |
| 62 | + ]; |
| 63 | + |
| 64 | + /** |
| 65 | + * Estimate the cost of a single API call in USD. |
| 66 | + * |
| 67 | + * Returns null if the model is not in the rate card (e.g. Ollama local models). |
| 68 | + * |
| 69 | + * @param string $modelname Exact model string from the API response. |
| 70 | + * @param int $prompttokens |
| 71 | + * @param int $completiontokens |
| 72 | + * @return float|null Cost in USD, or null if model is not known. |
| 73 | + */ |
| 74 | + public static function estimate_cost(string $modelname, int $prompttokens, int $completiontokens): ?float { |
| 75 | + $rates = self::get_rates($modelname); |
| 76 | + if ($rates === null) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + $inputcost = ($prompttokens / 1_000_000) * $rates['input']; |
| 80 | + $outputcost = ($completiontokens / 1_000_000) * $rates['output']; |
| 81 | + return $inputcost + $outputcost; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Look up rate card by model name (prefix match, longest prefix wins). |
| 86 | + * |
| 87 | + * @param string $modelname |
| 88 | + * @return array|null ['input' => float, 'output' => float] or null. |
| 89 | + */ |
| 90 | + public static function get_rates(string $modelname): ?array { |
| 91 | + $modelname = strtolower(trim($modelname)); |
| 92 | + $best = null; |
| 93 | + $bestlen = 0; |
| 94 | + foreach (self::$rate_cards as $prefix => $rates) { |
| 95 | + if (str_starts_with($modelname, $prefix) && strlen($prefix) > $bestlen) { |
| 96 | + $best = $rates; |
| 97 | + $bestlen = strlen($prefix); |
| 98 | + } |
| 99 | + } |
| 100 | + return $best; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Format a dollar amount for display. |
| 105 | + * |
| 106 | + * Uses more decimal places for sub-cent amounts so the value is meaningful. |
| 107 | + * |
| 108 | + * @param float|null $cost |
| 109 | + * @return string e.g. "$0.000142", "$0.0183", "$1.24", or "—" if null. |
| 110 | + */ |
| 111 | + public static function format_cost(?float $cost): string { |
| 112 | + if ($cost === null) { |
| 113 | + return '—'; |
| 114 | + } |
| 115 | + if ($cost < 0.0001) { |
| 116 | + return '$' . number_format($cost, 6); |
| 117 | + } |
| 118 | + if ($cost < 0.01) { |
| 119 | + return '$' . number_format($cost, 4); |
| 120 | + } |
| 121 | + if ($cost < 1.0) { |
| 122 | + return '$' . number_format($cost, 3); |
| 123 | + } |
| 124 | + return '$' . number_format($cost, 2); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Return all rate card entries for display in the admin UI. |
| 129 | + * |
| 130 | + * @return array [['model', 'input_per_1m', 'output_per_1m'], ...] |
| 131 | + */ |
| 132 | + public static function get_all_rates(): array { |
| 133 | + $result = []; |
| 134 | + foreach (self::$rate_cards as $prefix => $rates) { |
| 135 | + $result[] = [ |
| 136 | + 'model' => $prefix . '…', |
| 137 | + 'input_per_1m' => '$' . number_format($rates['input'], 2), |
| 138 | + 'output_per_1m' => '$' . number_format($rates['output'], 2), |
| 139 | + ]; |
| 140 | + } |
| 141 | + return $result; |
| 142 | + } |
| 143 | +} |
0 commit comments