diff --git a/src/backend/src/modules/puterai/AIChatService.js b/src/backend/src/modules/puterai/AIChatService.js index 3b12f4bf..0e278b8b 100644 --- a/src/backend/src/modules/puterai/AIChatService.js +++ b/src/backend/src/modules/puterai/AIChatService.js @@ -112,6 +112,16 @@ class AIChatService extends BaseService { } await this.db.insert('ai_usage', values); + + // USD cost from microcents + const cost_usd = values.cost / 1000000; + + // Add to TrackSpendingService + const svc_spending = this.services.get('spending'); + svc_spending.record_cost(`${details.service_used}:chat-completion`, { + timestamp: Date.now(), + cost: cost_usd, + }); }); const svc_apiErrpr = this.services.get('api-error'); diff --git a/src/backend/src/modules/puterai/OpenAICompletionService.js b/src/backend/src/modules/puterai/OpenAICompletionService.js index b71f2ea4..2e8ec184 100644 --- a/src/backend/src/modules/puterai/OpenAICompletionService.js +++ b/src/backend/src/modules/puterai/OpenAICompletionService.js @@ -432,9 +432,6 @@ class OpenAICompletionService extends BaseService { return tokens.length; })(); - const svc_spending = Context.get('services').get('spending'); - svc_spending.record_spending('openai', 'chat-completion', spending_meta); - const svc_counting = Context.get('services').get('counting'); svc_counting.increment({ service_name: 'openai:chat-completion', diff --git a/src/backend/src/services/TrackSpendingService.js b/src/backend/src/services/TrackSpendingService.js index 3722319f..4bbb3e82 100644 --- a/src/backend/src/services/TrackSpendingService.js +++ b/src/backend/src/services/TrackSpendingService.js @@ -278,6 +278,8 @@ class TrackSpendingService extends BaseService { /** * Records spending for a given vendor using the specified strategy * + * @deprecated Use `record_cost` instead + * * @param {string} vendor - The vendor name/identifier * @param {string} strategy_key - Key identifying the pricing strategy to use * @param {Object} data - Data needed to calculate cost based on the strategy @@ -300,6 +302,22 @@ class TrackSpendingService extends BaseService { const window = this.add_or_get_window_(id); window.add(cost); } + + /** + * Records known cost into a specified window id. + * + * This is simliar to `record_spending` but puts the responsibility + * of determining cost outside of this services. + */ + record_cost (window_id, { timestamp, cost }) { + const window = this.add_or_get_window_(window_id); + this.log.info(`Spent ${format_as_usd(cost)}`, { + window_id, + timestamp, + cost, + }) + window.add(cost); + } } module.exports = {