'object', 'properties' => [ 'period' => [ 'type' => 'string', 'description' => 'Time period for stats', 'enum' => ['day', 'week', 'month', 'year'], ], ], ]; } public function handle(array $args, array $context = []): array { try { $period = $this->optionalEnum($args, 'period', ['day', 'week', 'month', 'year'], 'month'); } catch (\InvalidArgumentException $e) { return $this->error($e->getMessage()); } // Use workspace_id from context if available (null returns system-wide stats) $workspaceId = $context['workspace_id'] ?? null; $stats = AIUsage::statsForWorkspace($workspaceId, $period); return $this->success([ 'period' => $period, 'total_requests' => $stats['total_requests'], 'total_input_tokens' => (int) $stats['total_input_tokens'], 'total_output_tokens' => (int) $stats['total_output_tokens'], 'total_cost' => number_format((float) $stats['total_cost'], 4), 'by_provider' => $stats['by_provider'], 'by_purpose' => $stats['by_purpose'], ]); } }