@light-merlin-dark/tok
Version:
Fast token estimation and cost calculation for enterprise LLMs with CLI and MCP support
79 lines • 2.68 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CostTracker = void 0;
const CostCalculator_1 = require("./CostCalculator");
class CostTracker {
totals = new Map();
startTime;
constructor() {
this.startTime = new Date();
}
add(model, promptTokens, completionTokens, price) {
const existing = this.totals.get(model) ?? {
promptTokens: 0,
completionTokens: 0,
promptCost: 0,
completionCost: 0
};
existing.promptTokens += promptTokens;
existing.completionTokens += completionTokens;
existing.promptCost += CostCalculator_1.CostCalculator.cost(promptTokens, price.prompt);
existing.completionCost += CostCalculator_1.CostCalculator.cost(completionTokens, price.completion);
this.totals.set(model, existing);
}
/** Raw map for callers to format as they wish */
snapshot() {
return new Map(this.totals);
}
/** Grand total across all models */
grandTotal() {
let sum = 0;
for (const totals of this.totals.values()) {
sum += totals.promptCost + totals.completionCost;
}
return sum;
}
/** Total token count across all models */
totalTokens() {
let promptTotal = 0;
let completionTotal = 0;
for (const totals of this.totals.values()) {
promptTotal += totals.promptTokens;
completionTotal += totals.completionTokens;
}
return { prompt: promptTotal, completion: completionTotal };
}
/** Get tracking duration in seconds */
getDuration() {
return (new Date().getTime() - this.startTime.getTime()) / 1000;
}
/** Reset all tracking data */
reset() {
this.totals.clear();
this.startTime = new Date();
}
/** Get summary statistics */
getSummary() {
const modelBreakdown = Array.from(this.totals.entries()).map(([model, totals]) => ({
model,
tokens: {
prompt: totals.promptTokens,
completion: totals.completionTokens
},
cost: {
prompt: totals.promptCost,
completion: totals.completionCost,
total: totals.promptCost + totals.completionCost
}
}));
return {
models: Array.from(this.totals.keys()),
totalCost: this.grandTotal(),
totalTokens: this.totalTokens(),
duration: this.getDuration(),
modelBreakdown
};
}
}
exports.CostTracker = CostTracker;
//# sourceMappingURL=CostTracker.js.map