Record and expose per-run model usage (tokens, split, cost)
Carry the usage a model stage already measures for its budget (stage.Budget.Snapshot()) onto the step as typed evidence and total it in one pass, rather than a second harness read, a transcript parse or an output-length estimate. A shared budget across stage revisits records only the visit's increment, and a revisit that observes no new report records no usage instead of a fabricated known zero. - protocol: StageUsage/RunUsage/TotalUsage and UsageTotals. - pipeline: step_usage table + migration, COALESCEd into the one step projection, FinishStep persistence, one json_extract history aggregate, retention removes the rows with their run. - server: stageUsage maps the budget snapshot, stamping per-stage model and usage for every model stage (including failures). - cli: inspect/stats human and JSON output. - budget/pi: retain the reported in/out split, mark a total-only (Pi) report split-unknown, and keep an unreported cost unknown (never zero). Evidence and docs updated in the packaged skill references and docs/cli.md; the live database has no step_usage rows yet (all history predates this), so per-run figures are reported as unavailable, not zero.
This commit is contained in:
@@ -160,6 +160,7 @@ type RunStep struct {
|
||||
FileChanges *StageFileChanges `json:"file_changes,omitempty"`
|
||||
Verdict *StageVerdict `json:"verdict,omitempty"`
|
||||
SessionExport *SessionExport `json:"session_export,omitempty"`
|
||||
Usage *StageUsage `json:"usage,omitempty"`
|
||||
Notifications []NotificationReceipt `json:"notifications,omitempty"`
|
||||
Artifacts []StageArtifact `json:"artifacts,omitempty"`
|
||||
Sequence int `json:"sequence"`
|
||||
@@ -196,6 +197,80 @@ type StageArtifact struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// StageUsage is the usage one model stage actually reported, carried from the
|
||||
// stage's already-measured budget accounting rather than a second measurement or
|
||||
// a transcript parse. TokensTotal/TokensKnown are the reported total; TokensIn
|
||||
// and TokensOut are set only when the harness reported the input/output split
|
||||
// (SplitKnown), so a harness that reports only a total (Pi) never has its total
|
||||
// presented as input tokens. CostUSD is present only when CostKnown: a stage
|
||||
// whose provider reported no cost carries CostKnown=false and no fabricated or
|
||||
// zero cost.
|
||||
type StageUsage struct {
|
||||
TokensIn int64 `json:"tokens_in,omitempty"`
|
||||
TokensOut int64 `json:"tokens_out,omitempty"`
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
TokensKnown bool `json:"tokens_known"`
|
||||
SplitKnown bool `json:"split_known,omitempty"`
|
||||
CostUSD float64 `json:"cost_usd,omitempty"`
|
||||
CostKnown bool `json:"cost_known"`
|
||||
}
|
||||
|
||||
// RunUsage is the totalled reported usage of a run's model stages. It is summed
|
||||
// in one pass over the run's already-loaded steps, so reading it adds no query.
|
||||
// A stage that reported no cost is counted in CostUnknownStages and contributes
|
||||
// no cost; a stage that reported only a total contributes to TokensTotal but not
|
||||
// to the in/out split, and SplitKnown is true only when every stage that
|
||||
// reported tokens also reported the split.
|
||||
type RunUsage struct {
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
TokensIn int64 `json:"tokens_in,omitempty"`
|
||||
TokensOut int64 `json:"tokens_out,omitempty"`
|
||||
SplitKnown bool `json:"split_known"`
|
||||
CostUSD float64 `json:"cost_usd,omitempty"`
|
||||
Stages int `json:"stages"`
|
||||
TokensKnownStages int `json:"tokens_known_stages"`
|
||||
SplitUnknownStages int `json:"split_unknown_stages,omitempty"`
|
||||
CostKnownStages int `json:"cost_known_stages"`
|
||||
CostUnknownStages int `json:"cost_unknown_stages,omitempty"`
|
||||
}
|
||||
|
||||
// TotalUsage sums the per-stage usage of a run's steps into one run total. It is
|
||||
// the one definition of the run total, so per-run inspection cannot drift from
|
||||
// the history-wide aggregate over the same rows.
|
||||
func TotalUsage(steps []RunStep) *RunUsage {
|
||||
total := &RunUsage{}
|
||||
splitComplete := true
|
||||
for i := range steps {
|
||||
usage := steps[i].Usage
|
||||
if usage == nil {
|
||||
continue
|
||||
}
|
||||
total.Stages++
|
||||
if usage.TokensKnown {
|
||||
total.TokensKnownStages++
|
||||
total.TokensTotal += usage.TokensTotal
|
||||
if usage.SplitKnown {
|
||||
total.TokensIn += usage.TokensIn
|
||||
total.TokensOut += usage.TokensOut
|
||||
} else {
|
||||
splitComplete = false
|
||||
total.SplitUnknownStages++
|
||||
}
|
||||
}
|
||||
if usage.CostKnown {
|
||||
total.CostKnownStages++
|
||||
total.CostUSD += usage.CostUSD
|
||||
} else {
|
||||
total.CostUnknownStages++
|
||||
}
|
||||
}
|
||||
if total.Stages == 0 {
|
||||
return nil
|
||||
}
|
||||
total.SplitKnown = total.TokensKnownStages > 0 && splitComplete
|
||||
return total
|
||||
}
|
||||
|
||||
type MemoryRead struct {
|
||||
Source string `json:"source"`
|
||||
Sequence int `json:"sequence"` // zero denotes original run input
|
||||
@@ -249,6 +324,10 @@ type RunDetail struct {
|
||||
Input map[string]any `json:"input"`
|
||||
Output map[string]any `json:"output"`
|
||||
Steps []RunStep `json:"steps"`
|
||||
// Usage is the totalled reported usage of the run's model stages, summed in
|
||||
// one pass over Steps. It is absent for a run with no model usage recorded
|
||||
// (including historical runs).
|
||||
Usage *RunUsage `json:"usage,omitempty"`
|
||||
// TitleSource is "model" when Title came from the shared summariser or
|
||||
// "fallback" when it was derived from the prompt; TitleModel names the model
|
||||
// that produced a model title. They are retained so a diagnosis can tell the
|
||||
|
||||
@@ -18,8 +18,32 @@ type DailyUsage struct {
|
||||
Date string `json:"date"`
|
||||
Runs int `json:"runs"`
|
||||
}
|
||||
|
||||
// UsageTotals is the summed reported model usage over all stored run history. It
|
||||
// is summed from the same per-step usage rows the per-run figures use, so the
|
||||
// history-wide totals equal the sum of the per-run totals. A stage that reported
|
||||
// no cost counts in CostUnknownStages and contributes no fabricated or zero cost;
|
||||
// a stage that reported only a total (Pi) contributes to TokensTotal but not to
|
||||
// the in/out split, and SplitKnown is true only when every token-reporting stage
|
||||
// reported a split.
|
||||
type UsageTotals struct {
|
||||
TokensTotal int64 `json:"tokens_total"`
|
||||
TokensIn int64 `json:"tokens_in,omitempty"`
|
||||
TokensOut int64 `json:"tokens_out,omitempty"`
|
||||
SplitKnown bool `json:"split_known"`
|
||||
CostUSD float64 `json:"cost_usd,omitempty"`
|
||||
Stages int `json:"stages"`
|
||||
TokensKnownStages int `json:"tokens_known_stages"`
|
||||
SplitUnknownStages int `json:"split_unknown_stages,omitempty"`
|
||||
CostKnownStages int `json:"cost_known_stages"`
|
||||
CostUnknownStages int `json:"cost_unknown_stages,omitempty"`
|
||||
}
|
||||
|
||||
type UsageStats struct {
|
||||
RunCounts
|
||||
Pipelines []PipelineUsage `json:"pipelines"`
|
||||
Days []DailyUsage `json:"days"`
|
||||
// Usage is the history-wide sum of the per-run reported usage. It is absent
|
||||
// for a database with no recorded model usage (including a pre-upgrade one).
|
||||
Usage *UsageTotals `json:"usage,omitempty"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user