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:
@@ -659,6 +659,28 @@ func completedSteps(steps []protocol.RunStep) int {
|
||||
return n
|
||||
}
|
||||
|
||||
// stageUsage carries the usage a model stage already measured for budget
|
||||
// accounting onto the run record. It reports a cost only when the provider
|
||||
// reported one and an in/out split only when the harness reported it, so an
|
||||
// unknown cost or split is never presented as zero. It returns nil when no usage
|
||||
// was observed, so the step stays absent rather than zero.
|
||||
func stageUsage(snapshot stage.BudgetSnapshot) *protocol.StageUsage {
|
||||
if !snapshot.TokensKnown && !snapshot.USDKnown {
|
||||
return nil
|
||||
}
|
||||
usage := &protocol.StageUsage{TokensKnown: snapshot.TokensKnown, CostKnown: snapshot.USDKnown}
|
||||
if snapshot.TokensKnown {
|
||||
usage.TokensTotal = snapshot.UsedTokens
|
||||
if snapshot.SplitKnown {
|
||||
usage.TokensIn, usage.TokensOut, usage.SplitKnown = snapshot.TokensInput, snapshot.TokensOutput, true
|
||||
}
|
||||
}
|
||||
if snapshot.USDKnown {
|
||||
usage.CostUSD = snapshot.UsedUSD
|
||||
}
|
||||
return usage
|
||||
}
|
||||
|
||||
// finishStage persists one step, refreshes the shared memory for a completed
|
||||
// step, notifies and classifies the step's target. Both the executed path and
|
||||
// the skipped path use it, so persistence, notification and terminal routing
|
||||
@@ -883,6 +905,7 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
var err error
|
||||
stageCtx := ctx
|
||||
var modelConfig stage.ModelConfig
|
||||
var budgetBefore stage.BudgetSnapshot
|
||||
if stage.EffectiveType(def.Type) == stage.ModelType {
|
||||
spec, specErr := (stage.ModelStage{}).Spec(def)
|
||||
if specErr != nil {
|
||||
@@ -893,6 +916,10 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
if budgets[key] == nil {
|
||||
budgets[key] = stage.NewBudget(modelConfig.BudgetUSD, modelConfig.BudgetTokens)
|
||||
}
|
||||
// The budget is shared across every visit to this stage key, so capture
|
||||
// its cumulative usage before the visit and record only this visit's
|
||||
// increment on the step, never the cumulative total again.
|
||||
budgetBefore = budgets[key].Snapshot()
|
||||
stageCtx = stage.WithBudget(ctx, budgets[key])
|
||||
}
|
||||
snapshotCtx, snapshotCancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
@@ -975,10 +1002,15 @@ func (r *runner) runLoop(ctx context.Context, id int64, graph protocol.Graph, na
|
||||
step := protocol.RunStep{Sequence: sequence, Stage: key, Status: "completed", Outcome: result.Outcome, Output: result.Output, Transcript: operations.Transcript(client.Transcript), SessionID: stageSession, FileChanges: changes, SessionExport: sessionExport, Artifacts: artifacts, ArtifactsSkipped: artifactsSkipped, ArtifactsTruncated: artifactsTruncated}
|
||||
// Stamp the effective provider/model this visit actually ran with for every
|
||||
// model stage, including failures, so the app/TUI evidence reflects any
|
||||
// override. Non-model stages keep empty values.
|
||||
// override. Non-model stages keep empty values. The usage the stage already
|
||||
// measured for budget accounting is carried onto the same step, never
|
||||
// re-read from the harness or parsed from the transcript.
|
||||
if stage.EffectiveType(def.Type) == stage.ModelType {
|
||||
step.Provider = modelConfig.Provider
|
||||
step.Model = modelConfig.Model
|
||||
if budget := budgets[key]; budget != nil {
|
||||
step.Usage = stageUsage(budget.Snapshot().Since(budgetBefore))
|
||||
}
|
||||
}
|
||||
if budget := budgets[key]; budget != nil {
|
||||
step.Transcript = stage.BudgetTranscript(step.Transcript, budget, modelConfig.Timeout, modelConfig.Retries)
|
||||
|
||||
Reference in New Issue
Block a user