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) {