Token usage: keep cache/reasoning split, count only recorded steps, cost only when positive; captures fixture, docs and notes
This commit is contained in:
@@ -11,6 +11,7 @@ class TokenUsage {
|
||||
required this.partial,
|
||||
this.input,
|
||||
this.output,
|
||||
this.cacheRead,
|
||||
this.cost,
|
||||
this.costPartial = false,
|
||||
this.stages,
|
||||
@@ -27,7 +28,16 @@ class TokenUsage {
|
||||
final int? input;
|
||||
final int? output;
|
||||
|
||||
/// The reported cost in USD, only when some stage reported one.
|
||||
/// The cache-read share of [total], only when every counted stage reported
|
||||
/// its cache components. Older rows never recorded them, so they have none.
|
||||
final int? cacheRead;
|
||||
|
||||
/// The tokens that were not cache reads, when [cacheRead] is known.
|
||||
int? get fresh => cacheRead == null ? null : total - cacheRead!;
|
||||
|
||||
/// The reported cost in USD, only when a positive cost was reported. Some
|
||||
/// providers (subscription models) report a cost of zero or none at all;
|
||||
/// that is "not reported", never a known zero.
|
||||
final double? cost;
|
||||
|
||||
/// Some stages reported no cost, so [cost] is a lower bound.
|
||||
@@ -51,14 +61,16 @@ TokenUsage? runTokenUsage(JsonMap run) {
|
||||
if (tokenStages <= 0 || total == null) return null;
|
||||
final costStages = _int(raw['cost_known_stages']) ?? 0;
|
||||
final split = raw['split_known'] == true;
|
||||
final cost = costStages > 0 ? _positive(raw['cost_usd']) : null;
|
||||
return TokenUsage(
|
||||
total: total,
|
||||
partial: stages != null && tokenStages < stages,
|
||||
// The server omits a zero in/out, so a known split makes absence zero.
|
||||
input: split ? _int(raw['tokens_in']) ?? 0 : null,
|
||||
output: split ? _int(raw['tokens_out']) ?? 0 : null,
|
||||
cost: costStages > 0 ? _double(raw['cost_usd']) ?? 0 : null,
|
||||
costPartial: costStages > 0 && stages != null && costStages < stages,
|
||||
cacheRead: _cacheRead(raw, total),
|
||||
cost: cost,
|
||||
costPartial: cost != null && stages != null && costStages < stages,
|
||||
stages: stages,
|
||||
tokenStages: tokenStages,
|
||||
);
|
||||
@@ -77,7 +89,8 @@ TokenUsage? stageTokenUsage(JsonMap step) {
|
||||
partial: false,
|
||||
input: split ? _int(raw['tokens_in']) ?? 0 : null,
|
||||
output: split ? _int(raw['tokens_out']) ?? 0 : null,
|
||||
cost: raw['cost_known'] == true ? _double(raw['cost_usd']) ?? 0 : null,
|
||||
cacheRead: _cacheRead(raw, total),
|
||||
cost: raw['cost_known'] == true ? _positive(raw['cost_usd']) : null,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -109,37 +122,46 @@ String formatTokenCount(int tokens) {
|
||||
String tokenUsageLabel(TokenUsage usage) =>
|
||||
'${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)} tokens';
|
||||
|
||||
/// The detail's token value: the compact total, the in/out split when
|
||||
/// The detail's token value: the compact total, then fresh versus cached
|
||||
/// when every stage reported its cache components, the in/out split when
|
||||
/// reported, and how many stages reported when the total is partial.
|
||||
/// `22.4M (20M in, 2.4M out)`, `≥9 · 1 of 2 stages reported`.
|
||||
String tokenUsageDetail(TokenUsage usage) {
|
||||
final parts = <String>[
|
||||
[
|
||||
'${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)}',
|
||||
if (usage.input != null && usage.output != null)
|
||||
'(${formatTokenCount(usage.input!)} in, '
|
||||
'${formatTokenCount(usage.output!)} out)',
|
||||
].join(' '),
|
||||
if (usage.partial && usage.stages != null)
|
||||
'${usage.tokenStages} of ${usage.stages} stages reported',
|
||||
];
|
||||
return parts.join(' · ');
|
||||
}
|
||||
/// `22.4M · 1.1M fresh, 21.3M cached · 900K in, 150K out`,
|
||||
/// `≥9 · 1 of 2 stages reported`.
|
||||
String tokenUsageDetail(TokenUsage usage) => [
|
||||
'${usage.partial ? '≥' : ''}${formatTokenCount(usage.total)}',
|
||||
if (usage.fresh != null)
|
||||
'${formatTokenCount(usage.fresh!)} fresh, '
|
||||
'${formatTokenCount(usage.cacheRead!)} cached',
|
||||
if (usage.input != null && usage.output != null)
|
||||
'${formatTokenCount(usage.input!)} in, '
|
||||
'${formatTokenCount(usage.output!)} out',
|
||||
if (usage.partial && usage.stages != null)
|
||||
'${usage.tokenStages} of ${usage.stages} stages reported',
|
||||
].join(' · ');
|
||||
|
||||
/// The reported cost, or null when no stage reported one: `$1.75`, `$0.10`,
|
||||
/// `$0.0042` (four places below a cent),
|
||||
/// `≥$0.10` when some stages reported no cost.
|
||||
String? tokenUsageCost(TokenUsage usage) {
|
||||
/// The wording for a cost no stage reported as a positive amount.
|
||||
const costNotReported = 'Not reported';
|
||||
|
||||
/// The reported cost: `$1.75`, `$0.10`, `$0.0042` (four places below a cent),
|
||||
/// `≥$0.10` when some stages reported no cost, and [costNotReported] when no
|
||||
/// positive cost was reported.
|
||||
String tokenUsageCost(TokenUsage usage) {
|
||||
final cost = usage.cost;
|
||||
if (cost == null) return null;
|
||||
final text = cost >= 0.01 || cost == 0
|
||||
? cost.toStringAsFixed(2)
|
||||
: cost.toStringAsFixed(4);
|
||||
if (cost == null) return costNotReported;
|
||||
final text = cost >= 0.01 ? cost.toStringAsFixed(2) : cost.toStringAsFixed(4);
|
||||
return '${usage.costPartial ? '≥' : ''}\$$text';
|
||||
}
|
||||
|
||||
int? _int(dynamic value) =>
|
||||
value is num && value.isFinite && value >= 0 ? value.round() : null;
|
||||
|
||||
double? _double(dynamic value) =>
|
||||
value is num && value.isFinite && value >= 0 ? value.toDouble() : null;
|
||||
double? _positive(dynamic value) =>
|
||||
value is num && value.isFinite && value > 0 ? value.toDouble() : null;
|
||||
|
||||
/// The cache reads, only when the server marked the cache split complete and
|
||||
/// the count fits inside the total.
|
||||
int? _cacheRead(Map raw, int total) {
|
||||
if (raw['cache_known'] != true) return null;
|
||||
final read = _int(raw['cache_read']) ?? 0;
|
||||
return read <= total ? read : null;
|
||||
}
|
||||
|
||||
@@ -76,6 +76,7 @@ List<DetailMetric> detailUsageMetrics(dynamic raw) {
|
||||
final tokenStages = _int(usage['tokens_known_stages']) ?? 0;
|
||||
final costStages = _int(usage['cost_known_stages']) ?? 0;
|
||||
final totalTokens = _int(usage['tokens_total']);
|
||||
final cost = usage['cost_usd'] is num ? usage['cost_usd'] as num : null;
|
||||
final splitKnown = usage['split_known'] == true && tokenStages > 0;
|
||||
final rows = <DetailMetric>[
|
||||
if (stages != null) (label: 'Stages', value: '$stages'),
|
||||
@@ -95,11 +96,11 @@ List<DetailMetric> detailUsageMetrics(dynamic raw) {
|
||||
),
|
||||
(
|
||||
label: 'Reported cost',
|
||||
// cost_usd is omitted by the server when zero, so a known stage count
|
||||
// makes an absent value a real $0.000000 rather than an unknown cost.
|
||||
value: costStages == 0
|
||||
? 'Unavailable'
|
||||
: '\$${((usage['cost_usd'] as num?) ?? 0).toStringAsFixed(6)}',
|
||||
// A cost of zero (or none) is what subscription models report even
|
||||
// with a "known" cost: it is not reported, never a measured zero.
|
||||
value: costStages == 0 || cost == null || cost <= 0
|
||||
? 'Not reported'
|
||||
: '\$${cost.toStringAsFixed(6)}',
|
||||
),
|
||||
];
|
||||
if (stages != null && tokenStages < stages) {
|
||||
|
||||
@@ -84,8 +84,7 @@ List<VisitFact> detailVisitFacts(JsonMap step) {
|
||||
final usage = stageTokenUsage(step);
|
||||
if (usage != null) {
|
||||
facts.add((label: 'Tokens', value: tokenUsageDetail(usage)));
|
||||
final cost = tokenUsageCost(usage);
|
||||
if (cost != null) facts.add((label: 'Cost', value: cost));
|
||||
facts.add((label: 'Cost', value: tokenUsageCost(usage)));
|
||||
}
|
||||
final target = _text(step['target']);
|
||||
if (target != null) facts.add((label: 'Target', value: target));
|
||||
|
||||
Reference in New Issue
Block a user