accumulateUsage function

Usage? accumulateUsage(
  1. Usage? cumulative,
  2. Usage? next
)

Sums token/cost accounting across requests for the cumulative fold.

Null Usage on either side passes the other through; a missing reasoning breakdown stays missing unless both sides report one.

Implementation

Usage? accumulateUsage(Usage? cumulative, Usage? next) {
  if (next == null) return cumulative;
  if (cumulative == null) return next;
  final reasoning = (cumulative.reasoning ?? 0) + (next.reasoning ?? 0);
  return cumulative.copyWith(
    input: cumulative.input + next.input,
    output: cumulative.output + next.output,
    cacheRead: cumulative.cacheRead + next.cacheRead,
    cacheWrite: cumulative.cacheWrite + next.cacheWrite,
    cacheWrite1h: cumulative.cacheWrite1h == null && next.cacheWrite1h == null
        ? null
        : (cumulative.cacheWrite1h ?? 0) + (next.cacheWrite1h ?? 0),
    reasoning: reasoning == 0 ? null : reasoning,
    totalTokens: cumulative.totalTokens + next.totalTokens,
    cost: UsageCost(
      input: cumulative.cost.input + next.cost.input,
      output: cumulative.cost.output + next.cost.output,
      cacheRead: cumulative.cost.cacheRead + next.cost.cacheRead,
      cacheWrite: cumulative.cost.cacheWrite + next.cost.cacheWrite,
      total: cumulative.cost.total + next.cost.total,
    ),
  );
}