Collect costs for dispatched model attempts

This commit is contained in:
2026-11-22 18:27:01 +00:00
parent cd0e1f5d39
commit 81a2a7591d
9 changed files with 129 additions and 6 deletions

View File

@@ -1,11 +1,25 @@
package observation
import (
"context"
"math"
"go.maximumdirect.net/promptkit/internal/domain"
)
type costContextKey struct{}
// WithCostAccumulator carries one invocation's cost state.
func WithCostAccumulator(ctx context.Context, accumulator *CostAccumulator) context.Context {
return context.WithValue(ctx, costContextKey{}, accumulator)
}
// CostFromContext returns the current invocation's cost state, if installed.
func CostFromContext(ctx context.Context) *CostAccumulator {
value, _ := ctx.Value(costContextKey{}).(*CostAccumulator)
return value
}
// ResolveCost applies reported-cost precedence over a configured-zero fallback.
func ResolveCost(reported *float64, zeroCost bool) domain.ObservedCost {
if reported != nil && !math.IsNaN(*reported) && !math.IsInf(*reported, 0) {

View File

@@ -14,6 +14,7 @@ type Group struct {
terminals int
attempts, retries, repairs uint64
usage domain.ObservedTokenUsage
cost CostAccumulator
}
// Observe enrolls a run and incorporates its first terminal event only.
@@ -39,6 +40,7 @@ func (g *Group) Observe(event domain.ExecutionEvent) {
g.usage = domain.ObservedTokenUsage{PromptTokens: complete, CompletionTokens: complete, ReasoningTokens: complete, TotalTokens: complete, CachedTokens: complete, CacheWriteTokens: complete}
}
mergeGroupUsage(&g.usage, event.Usage)
g.cost.Add(event.CostUSD)
g.terminals++
}
@@ -47,6 +49,9 @@ func (g *Group) Snapshot() domain.ExecutionGroupSummary {
g.mu.Lock()
defer g.mu.Unlock()
result := domain.ExecutionGroupSummary{Members: make([]domain.ExecutionGroupMember, 0, len(g.members)), TerminalCoverageComplete: len(g.members) == g.terminals, Attempts: g.attempts, TransientRetries: g.retries, RepairCalls: g.repairs, Usage: g.usage}
if g.terminals > 0 || len(g.members) == 0 {
result.CostUSD = g.cost.Observed()
}
for id, terminal := range g.members {
result.Members = append(result.Members, domain.ExecutionGroupMember{RunID: id, TerminalObserved: terminal})
}

View File

@@ -41,6 +41,25 @@ func TestGroupTerminalAccounting(t *testing.T) {
}
}
func TestGroupCostCoverageIsIndependentOfTerminalCoverage(t *testing.T) {
var group Group
if got := group.Snapshot().CostUSD; got != (domain.ObservedCost{Known: true, Complete: true}) {
t.Fatalf("empty=%+v", got)
}
group.Observe(domain.ExecutionEvent{Kind: domain.ExecutionStarted, RunID: "pending"})
if got := group.Snapshot().CostUSD; got != (domain.ObservedCost{}) {
t.Fatalf("pending=%+v", got)
}
charge := domain.ObservedCost{Value: .25, Known: true, Complete: true, Source: domain.CostSourceProvider}
terminal := domain.ExecutionEvent{Kind: domain.ExecutionFinished, RunID: "done", CostUSD: charge}
group.Observe(terminal)
group.Observe(terminal)
got := group.Snapshot()
if got.TerminalCoverageComplete || got.CostUSD != charge {
t.Fatalf("snapshot=%+v", got)
}
}
func TestGroupConcurrentSnapshots(t *testing.T) {
var group Group
var wg sync.WaitGroup

View File

@@ -30,6 +30,7 @@ type Recorder struct {
generationDuration time.Duration
retryDelay time.Duration
usage UsageAccumulator
cost *CostAccumulator
}
// Selection contains resolved identities only. Unresolved fields stay empty.
@@ -53,6 +54,7 @@ type AttemptData struct {
StatusCode int
Usage domain.TokenUsage
UsagePresence *domain.TokenUsagePresence
CostUSD domain.ObservedCost
}
// DelayData describes one completed or interrupted retry wait.
@@ -99,6 +101,9 @@ func (r *Recorder) RunID() string { return r.runID }
// SetParentRunID records the immediate source identity before start delivery.
func (r *Recorder) SetParentRunID(id string) { r.parentRunID = id }
// SetCostAccumulator selects the invocation-owned cost state for terminal reporting.
func (r *Recorder) SetCostAccumulator(cost *CostAccumulator) { r.cost = cost }
// Start delivers the opening event with only identities already resolved.
func (r *Recorder) Start() {
event := r.event(domain.ExecutionStarted)
@@ -138,6 +143,7 @@ func (r *Recorder) Attempt(data AttemptData) {
event.StatusCode = data.StatusCode
if data.Dispatched {
event.Usage = observedUsage(data.Usage, data.UsagePresence)
event.CostUSD = data.CostUSD
}
r.deliver(event)
}
@@ -176,6 +182,9 @@ func (r *Recorder) Finish(data FinishData) {
event.Duration = 0
}
event.Usage = r.usage.Observed()
if r.cost != nil {
event.CostUSD = r.cost.Observed()
}
r.deliver(event)
}
@@ -222,7 +231,7 @@ type executionBoundary struct{ context.Context }
func (c executionBoundary) Value(key any) any {
switch key.(type) {
case contextKey, claimSinkKey:
case contextKey, claimSinkKey, costContextKey:
return nil
default:
return c.Context.Value(key)

View File

@@ -19,10 +19,15 @@ func TestExecutionBoundaryPreservesCallerContext(t *testing.T) {
recorder := newRecorder("outer", time.Now, nil)
sink := recorder
parent = WithClaimSink(WithRecorder(parent, recorder), &sink)
var cost CostAccumulator
parent = WithCostAccumulator(parent, &cost)
child := WithoutExecutionState(parent)
if FromContext(child) != nil || FromContext(parent) != recorder {
t.Fatal("execution boundary did not isolate recorder state")
}
if CostFromContext(child) != nil || CostFromContext(parent) != &cost {
t.Fatal("execution boundary did not isolate cost state")
}
RegisterClaim(child, nil)
if sink != recorder {
t.Fatal("execution boundary retained the parent claim sink")