calculateCost function

double calculateCost(
  1. String modelId,
  2. TokenUsage usage
)

Calculate cost from token usage.

Implementation

double calculateCost(String modelId, TokenUsage usage) {
  final pricing = modelPricing[modelId];
  if (pricing == null) return 0.0;

  var cost = 0.0;
  cost += (usage.inputTokens / 1000000) * pricing.inputPerMillion;
  cost += (usage.outputTokens / 1000000) * pricing.outputPerMillion;

  if (pricing.cacheCreationPerMillion != null) {
    cost +=
        (usage.cacheCreationTokens / 1000000) *
        pricing.cacheCreationPerMillion!;
  }
  if (pricing.cacheReadPerMillion != null) {
    cost += (usage.cacheReadTokens / 1000000) * pricing.cacheReadPerMillion!;
  }

  return cost;
}