costTracking static method
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);
},
);
}