Cost Tracking Library

@contractspec/lib.cost-tracking transforms raw telemetry into dollars: DB/API/compute costs per operation, budget alerts per tenant, and actionable optimization tips.

Installation

bun add @contractspec/lib.cost-tracking

Record Samples

const tracker = new CostTracker();
tracker.recordSample({
  operation: 'orders.list',
  tenantId: 'acme',
  dbReads: 1200,
  dbWrites: 4,
  computeMs: 180,
  externalCalls: [{ provider: 'stripe', cost: 0.02 }],
});

Budget Alerts

const budgets = new BudgetAlertManager({
  budgets: [{ tenantId: 'acme', monthlyLimit: 150 }],
  onAlert: ({ tenantId, total }) => notifyFinance(tenantId, total),
});

tracker.getTotals({ tenantId: 'acme' }).forEach((summary) => budgets.track(summary));

Optimization Suggestions

Feed summaries into OptimizationRecommender to surface N+1 queries, compute-heavy steps, or expensive external calls. Store the generated suggestions in the new Prisma model to power Ops playbooks.