UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

25 lines (24 loc) 865 B
import { Cost } from "./Cost.js"; export async function calculateCost({ calls, costCalculators, }) { let costInMillicents = 0; const callsWithUnknownCost = []; for (const call of calls) { const model = call.model; const providerCostCalculator = costCalculators.find((providerCostCalculator) => providerCostCalculator.provider === model.provider); if (!providerCostCalculator) { callsWithUnknownCost.push(call); continue; } const cost = await providerCostCalculator.calculateCostInMillicents(call); if (cost === null) { callsWithUnknownCost.push(call); continue; } costInMillicents += cost; } return new Cost({ costInMillicents, hasUnknownCost: callsWithUnknownCost.length > 0, callsWithUnknownCost, }); }