Files
goclaw/internal/tracing/cost.go
T
Viet Tran 0926d053b0 feat: add token usage tracking, cost analytics, budget enforcement, wake API, and activity audit trail (#142)
- A1+C2: Include token usage in run.completed event payload for WS clients
- A2: Cost tracking with model pricing config, cost calculation, and cost summary API
- A3: Budget enforcement per agent with monthly budget limits (migration 000015)
- C1: External wake/trigger API (POST /v1/agents/{id}/wake) for orchestrators
- C3: Activity audit trail with structured logging and queryable API
- UI: Activity page, cost stat card on overview, budget section in agent detail
- i18n: Complete en/vi/zh translations for all new features
2026-03-11 12:52:12 +07:00

39 lines
1.2 KiB
Go

package tracing
import (
"github.com/nextlevelbuilder/goclaw/internal/config"
"github.com/nextlevelbuilder/goclaw/internal/providers"
)
// CalculateCost computes the USD cost for a single LLM call based on token usage and pricing.
// Returns 0 if pricing is nil.
func CalculateCost(pricing *config.ModelPricing, usage *providers.Usage) float64 {
if pricing == nil || usage == nil {
return 0
}
cost := float64(usage.PromptTokens) * pricing.InputPerMillion / 1_000_000
cost += float64(usage.CompletionTokens) * pricing.OutputPerMillion / 1_000_000
if pricing.CacheReadPerMillion > 0 && usage.CacheReadTokens > 0 {
cost += float64(usage.CacheReadTokens) * pricing.CacheReadPerMillion / 1_000_000
}
if pricing.CacheCreatePerMillion > 0 && usage.CacheCreationTokens > 0 {
cost += float64(usage.CacheCreationTokens) * pricing.CacheCreatePerMillion / 1_000_000
}
return cost
}
// LookupPricing finds the model pricing from config.
// Tries "provider/model" first, then just "model".
func LookupPricing(pricingMap map[string]*config.ModelPricing, provider, model string) *config.ModelPricing {
if pricingMap == nil {
return nil
}
if p, ok := pricingMap[provider+"/"+model]; ok {
return p
}
if p, ok := pricingMap[model]; ok {
return p
}
return nil
}