import { monitoringService, withToolMonitoring, type MonitoringLogger } from './MonitoringService.js'; /** * LLM Tool Utilities * Helper functions for instrumenting LLM tool calls with monitoring and token tracking * @packageDocumentation */ /** * Helper function for LLM tool calls with automatic token usage tracking * Wraps withToolMonitoring and adds token usage metric recording * Uses the singleton monitoringService instance to avoid memory leaks * * @param toolName - Name of the LLM tool (e.g., 'anthropic', 'gemini', 'grok') * @param modelName - Name of the model being used * @param internalCall - Function that performs the actual LLM call * @param logger - Optional logger for debugging * @returns Promise resolving to the LLM response with tokens field * * @example * ```typescript * async call(prompt: string): Promise { * return withLLMToolCall('grok', this.model, async () => { * return await this.internalCall(prompt); * }); * } * ``` */ export async function withLLMToolCall( toolName: string, modelName: string, internalCall: () => Promise, logger?: MonitoringLogger ): Promise { return withToolMonitoring(toolName, 'call', async () => { const result = await internalCall(); // Record token usage if available if (result.tokens) { void monitoringService.recordMetric('token_usage', result.tokens, { tool_name: toolName, model_name: modelName, operation: 'call' }).catch(error => logger?.debug('Monitoring error (non-critical):', error)); } return result; }, { labels: { model_name: modelName }, logger }); } // Re-export MonitoringLogger for convenience export type { MonitoringLogger };