costTracking static method

SessionLifecycle costTracking({
  1. required void onCostUpdate(
    1. int totalInputTokens,
    2. int totalOutputTokens,
    3. double estimatedCost
    ),
  2. double inputTokenCost = 0.000003,
  3. double outputTokenCost = 0.000015,
})

Cost tracking lifecycle hooks.

Tracks cumulative token usage and estimated cost across the session, emitting summaries at turn end and session end.

Implementation

static SessionLifecycle costTracking({
  required void Function(
    int totalInputTokens,
    int totalOutputTokens,
    double estimatedCost,
  )
  onCostUpdate,
  double inputTokenCost = 0.000003,
  double outputTokenCost = 0.000015,
}) {
  var totalInput = 0;
  var totalOutput = 0;
  var totalCost = 0.0;
  DateTime? sessionStart;

  return SessionLifecycle(
    onSessionStart: (event) async {
      sessionStart = event.timestamp;
      totalInput = 0;
      totalOutput = 0;
      totalCost = 0.0;
    },
    onSessionEnd: (event) async {
      onCostUpdate(totalInput, totalOutput, totalCost);
    },
  );
}