feat: add LLM cost tracking with budget enforcement and admin dashboard

- New Cost_Tracker class: logs every API call (model, tokens, cost), enforces
  monthly budgets with hard-stop mode, provides aggregated reporting
- New llm_costs database table with indexes on created_at, model, operation
- Modified LLM class: call_openrouter_with_usage() extracts token usage from
  OpenRouter responses; budget gate checks before each API call
- Admin settings: monthly budget (USD), enforcement mode (disabled/warn/hard-stop),
  cost log retention period
- Admin cost dashboard: budget progress bar, daily cost chart (Chart.js),
  per-model and per-operation breakdowns, recent API call log
- PHPUnit tests: 16 tests covering cost calculation, pricing lookups,
  budget enforcement logic, and model validation
- Updated ARCHITECTURE.md with new table schema, data flow, and key decisions
- Updated CONVENTIONS.md with "how to add cost tracking for a new operation"
- Version bump to 2.4.0

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Terence
2026-04-13 04:18:30 +07:00
parent 6796121003
commit b7da4f42b9
47 changed files with 2192 additions and 164 deletions

9
includes/class-peptide-news-loader.php Normal file → Executable file
View File

@@ -1,4 +1,5 @@
<?php
declare( strict_types=1 );
/**
* Register all actions and filters for the plugin.
*
@@ -50,7 +51,7 @@ class Peptide_News_Loader {
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
*/
public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
public function add_action( string $hook, object $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void {
$this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args );
}
@@ -64,7 +65,7 @@ class Peptide_News_Loader {
* @param int $priority Optional. The priority at which the function should be fired. Default is 10.
* @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1.
*/
public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) {
public function add_filter( string $hook, object $component, string $callback, int $priority = 10, int $accepted_args = 1 ): void {
$this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args );
}
@@ -81,7 +82,7 @@ class Peptide_News_Loader {
* @param int $accepted_args The number of arguments that should be passed to the $callback.
* @return array The collection of actions and filters registered with WordPress.
*/
private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) {
private function add( array $hooks, string $hook, object $component, string $callback, int $priority, int $accepted_args ): array {
$hooks[] = array(
'hook' => $hook,
'component' => $component,
@@ -98,7 +99,7 @@ class Peptide_News_Loader {
*
* @since 1.0.0
*/
public function run() {
public function run(): void {
foreach ( $this->filters as $hook ) {
add_filter(
$hook['hook'],