Token usage: keep cache/reasoning split, count only recorded steps, cost only when positive; captures fixture, docs and notes

This commit is contained in:
2026-11-03 22:16:49 +00:00
parent aa948bdfe4
commit 3227d61caf
19 changed files with 447 additions and 61 deletions
+24
View File
@@ -325,6 +325,14 @@ type StageUsage struct {
SplitKnown bool `json:"split_known,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
CostKnown bool `json:"cost_known"`
// TokensReasoning, CacheRead and CacheWrite are the remaining components of
// a split report (TokensTotal = in + out + reasoning + cache read + cache
// write). They are recorded only when CacheKnown; a row stored before they
// were kept has CacheKnown=false, so its absent counts are unknown, not zero.
TokensReasoning int64 `json:"tokens_reasoning,omitempty"`
CacheRead int64 `json:"cache_read,omitempty"`
CacheWrite int64 `json:"cache_write,omitempty"`
CacheKnown bool `json:"cache_known,omitempty"`
}
// RunUsage is the totalled reported usage of a run's model stages. It is summed
@@ -344,6 +352,15 @@ type RunUsage struct {
SplitUnknownStages int `json:"split_unknown_stages,omitempty"`
CostKnownStages int `json:"cost_known_stages"`
CostUnknownStages int `json:"cost_unknown_stages,omitempty"`
// TokensReasoning, CacheRead and CacheWrite sum the stages that recorded the
// component split (CacheKnownStages). CacheKnown is true only when every
// token-reporting stage did, so fresh (total minus cache read) versus cached
// is never presented from a partial split.
TokensReasoning int64 `json:"tokens_reasoning,omitempty"`
CacheRead int64 `json:"cache_read,omitempty"`
CacheWrite int64 `json:"cache_write,omitempty"`
CacheKnown bool `json:"cache_known,omitempty"`
CacheKnownStages int `json:"cache_known_stages,omitempty"`
}
// TotalUsage sums the per-stage usage of a run's steps into one run total. It is
@@ -368,6 +385,12 @@ func TotalUsage(steps []RunStep) *RunUsage {
splitComplete = false
total.SplitUnknownStages++
}
if usage.CacheKnown {
total.CacheKnownStages++
total.TokensReasoning += usage.TokensReasoning
total.CacheRead += usage.CacheRead
total.CacheWrite += usage.CacheWrite
}
}
if usage.CostKnown {
total.CostKnownStages++
@@ -380,6 +403,7 @@ func TotalUsage(steps []RunStep) *RunUsage {
return nil
}
total.SplitKnown = total.TokensKnownStages > 0 && splitComplete
total.CacheKnown = total.TokensKnownStages > 0 && total.CacheKnownStages == total.TokensKnownStages
return total
}
+6
View File
@@ -37,6 +37,12 @@ type UsageTotals struct {
SplitUnknownStages int `json:"split_unknown_stages,omitempty"`
CostKnownStages int `json:"cost_known_stages"`
CostUnknownStages int `json:"cost_unknown_stages,omitempty"`
// The cache/reasoning components, as in RunUsage.
TokensReasoning int64 `json:"tokens_reasoning,omitempty"`
CacheRead int64 `json:"cache_read,omitempty"`
CacheWrite int64 `json:"cache_write,omitempty"`
CacheKnown bool `json:"cache_known,omitempty"`
CacheKnownStages int `json:"cache_known_stages,omitempty"`
}
type UsageStats struct {