Token usage: keep cache/reasoning split, count only recorded steps, cost only when positive; captures fixture, docs and notes
This commit is contained in:
@@ -82,14 +82,17 @@ void main() {
|
||||
),
|
||||
),
|
||||
)!;
|
||||
expect(tokenUsageDetail(usage), '22.4M (20M in, 2.4M out)');
|
||||
expect(tokenUsageDetail(usage), '22.4M · 20M in, 2.4M out');
|
||||
expect(tokenUsageCost(usage), '\$1.75');
|
||||
|
||||
final partialCost = runTokenUsage(
|
||||
_run(1, usage: _usage(9, stages: 2, costStages: 1, cost: 0.1)),
|
||||
)!;
|
||||
expect(tokenUsageCost(partialCost), '≥\$0.10');
|
||||
expect(tokenUsageCost(runTokenUsage(_run(1, usage: _usage(9)))!), isNull);
|
||||
expect(
|
||||
tokenUsageCost(runTokenUsage(_run(1, usage: _usage(9)))!),
|
||||
'Not reported',
|
||||
);
|
||||
expect(
|
||||
tokenUsageCost(
|
||||
runTokenUsage(_run(1, usage: _usage(9, costStages: 1, cost: 0.0042)))!,
|
||||
@@ -98,6 +101,52 @@ void main() {
|
||||
);
|
||||
});
|
||||
|
||||
test('the detail splits fresh from cached when every stage reported it', () {
|
||||
final usage = runTokenUsage(
|
||||
_run(
|
||||
1,
|
||||
usage: {
|
||||
..._usage(22400000, split: true, input: 900000, output: 150000),
|
||||
'cache_known': true,
|
||||
'cache_read': 21300000,
|
||||
'cache_write': 50000,
|
||||
},
|
||||
),
|
||||
)!;
|
||||
expect(tokenUsageLabel(usage), '22.4M tokens');
|
||||
expect(usage.fresh, 1100000);
|
||||
expect(
|
||||
tokenUsageDetail(usage),
|
||||
'22.4M · 1.1M fresh, 21.3M cached · 900K in, 150K out',
|
||||
);
|
||||
// An older row (or a partial cache split) has no cache figure: just the
|
||||
// total and what else it reported.
|
||||
final old = runTokenUsage(
|
||||
_run(
|
||||
1,
|
||||
usage: _usage(22400000, split: true, input: 900000, output: 150000),
|
||||
),
|
||||
)!;
|
||||
expect(old.fresh, isNull);
|
||||
expect(tokenUsageDetail(old), '22.4M · 900K in, 150K out');
|
||||
});
|
||||
|
||||
test('a zero or missing "known" cost is not reported, never \$0', () {
|
||||
// Subscription models report cost_known with no positive cost_usd.
|
||||
for (final usage in [
|
||||
_usage(9, costStages: 1),
|
||||
_usage(9, costStages: 1, cost: 0),
|
||||
]) {
|
||||
final tokens = runTokenUsage(_run(1, usage: usage))!;
|
||||
expect(tokens.cost, isNull);
|
||||
expect(tokenUsageCost(tokens), 'Not reported');
|
||||
}
|
||||
final stage = stageTokenUsage({
|
||||
'usage': {'tokens_total': 9, 'tokens_known': true, 'cost_known': true},
|
||||
})!;
|
||||
expect(tokenUsageCost(stage), 'Not reported');
|
||||
});
|
||||
|
||||
test('a stage reports its own tokens only when it counted them', () {
|
||||
final stage = stageTokenUsage({
|
||||
'usage': {
|
||||
@@ -110,7 +159,7 @@ void main() {
|
||||
'cost_usd': 0.02,
|
||||
},
|
||||
})!;
|
||||
expect(tokenUsageDetail(stage), '1.5K (1.2K in, 300 out)');
|
||||
expect(tokenUsageDetail(stage), '1.5K · 1.2K in, 300 out');
|
||||
expect(tokenUsageCost(stage), '\$0.02');
|
||||
expect(
|
||||
stageTokenUsage({
|
||||
|
||||
@@ -98,7 +98,7 @@ void main() {
|
||||
expect(rows.firstWhere((row) => row.label == 'Size on disk').value, '5 MB');
|
||||
expect(
|
||||
rows.firstWhere((row) => row.label == 'Tokens').value,
|
||||
'123 (100 in, 23 out)',
|
||||
'123 · 100 in, 23 out',
|
||||
);
|
||||
expect(rows.firstWhere((row) => row.label == 'Cost').value, '\$0.50');
|
||||
expect(rows.firstWhere((row) => row.label == 'Retry of').value, 'run 12');
|
||||
|
||||
@@ -30,7 +30,7 @@ void main() {
|
||||
final facts = detailVisitFacts(_reported);
|
||||
expect(
|
||||
facts,
|
||||
contains((label: 'Tokens', value: '1.5M (1.2M in, 300K out)')),
|
||||
contains((label: 'Tokens', value: '1.5M · 1.2M in, 300K out')),
|
||||
);
|
||||
expect(facts, contains((label: 'Cost', value: '\$0.42')));
|
||||
final none = detailVisitFacts(_unreported);
|
||||
|
||||
Reference in New Issue
Block a user