calculateCost function

Usage calculateCost(
  1. Usage usage,
  2. Model model
)

Fills in Usage.cost from the model's ModelCost rates.

Ported from pi's calculateCost (flat-rate case; tiered pricing is not ported yet). Returns usage unchanged apart from the cost breakdown.

Implementation

Usage calculateCost(Usage usage, Model model) {
  const perMillion = 1000000.0;
  final cost = UsageCost(
    input: usage.input * model.cost.input / perMillion,
    output: usage.output * model.cost.output / perMillion,
    cacheRead: usage.cacheRead * model.cost.cacheRead / perMillion,
    cacheWrite: usage.cacheWrite * model.cost.cacheWrite / perMillion,
  );
  return usage.copyWith(
    cost: cost.copyWith(
      total: cost.input + cost.output + cost.cacheRead + cost.cacheWrite,
    ),
  );
}