dev: improve accuracy of TrackSpendingService

This commit is contained in:
KernelDeimos 2025-01-27 11:20:42 -05:00
parent 52c1adb7eb
commit d00061ae2f
3 changed files with 28 additions and 3 deletions

View File

@ -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');

View File

@ -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',

View File

@ -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 = {