The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(pricing): report only cache fallbacks used
Rudy Celeklid6614cc435b8
diff --git a/cli/beacon/internal/pricing/cache_fallback_test.go b/cli/beacon/internal/pricing/cache_fallback_test.gonew file mode 100644index 00000000..0cc3d4be--- /dev/null+++ b/cli/beacon/internal/pricing/cache_fallback_test.go@@ -0,0 +1,41 @@+package pricing++import "testing"++func TestCacheWriteFallbackDescribesOnlyRatesUsed(t *testing.T) {+ rates := Rates{RateSet: RateSet{Input: 2_000_000, Output: 3_000_000, CacheWrite1h: 5_000_000}}+ for _, tc := range []struct {+ name string+ total, oneHour int64+ wantCost Microdollars+ wantFallback bool+ }{+ {"zero_counts", 0, 0, 0, false},+ {"only_one_hour", 1000, 1000, 5000, false},+ {"mixed_ttls", 1000, 600, 3800, true},+ {"only_five_minutes", 1000, 0, 2000, true},+ {"clamped_one_hour", 1000, 2000, 5000, false},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := Cost(rates, Tokens{CacheCreation: tc.total, CacheCreation1h: tc.oneHour}, false)+ if got.Cost != tc.wantCost || got.CacheWriteAtInputRate != tc.wantFallback || got.CacheWrite1hAtWriteRate {+ t.Fatalf("estimate = %+v; want cost %d input fallback %v and no 1h fallback", got, tc.wantCost, tc.wantFallback)+ }+ })+ }+}++func TestOneHourOnlyOverrideDoesNotReportInputFallback(t *testing.T) {+ overrides, err := LoadOverrides([]byte(`{"schema":"beacon.pricing.overrides/v1","models":{"internal-model":{"input_usd_per_mtok":2,"output_usd_per_mtok":3,"cache_write_1h_usd_per_mtok":5}}}`), overridesCatalog(t))+ if err != nil {+ t.Fatal(err)+ }+ rates, ok := overrides.Rates("internal-model")+ if !ok {+ t.Fatal("override model missing")+ }+ got := Cost(rates, Tokens{CacheCreation: 1000, CacheCreation1h: 1000}, false)+ if got.Cost != 5000 || got.CacheWriteAtInputRate || got.CacheWrite1hAtWriteRate {+ t.Fatalf("override estimate = %+v; want explicit one-hour rate without fallback", got)+ }+}diff --git a/cli/beacon/internal/pricing/cost.go b/cli/beacon/internal/pricing/cost.goindex c6ce4645..02642f15 100644--- a/cli/beacon/internal/pricing/cost.go+++ b/cli/beacon/internal/pricing/cost.go@@ -100,7 +100,8 @@ func Cost(r Rates, t Tokens, requestScoped bool) Estimate {cacheWrite := set.CacheWriteif cacheWrite <= 0 {cacheWrite = set.Input- est.CacheWriteAtInputRate = t.CacheCreation > 0+ est.CacheWriteAtInputRate = t.CacheCreation > t.CacheCreation1h ||+ (t.CacheCreation1h > 0 && set.CacheWrite1h <= 0)}cacheWrite1h := set.CacheWrite1hif cacheWrite1h <= 0 {diff --git a/cli/beacon/internal/tokens/cache_fallback_test.go b/cli/beacon/internal/tokens/cache_fallback_test.gonew file mode 100644index 00000000..5d696473--- /dev/null+++ b/cli/beacon/internal/tokens/cache_fallback_test.go@@ -0,0 +1,55 @@+package tokens++import (+ "encoding/json"+ "reflect"+ "testing"++ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/pricing"+)++func TestOneHourOverrideFallbacksInReportJSON(t *testing.T) {+ for _, tc := range []struct {+ name, extraRate string+ oneHour int64+ wantCost float64+ wantFallbacks []string+ }{+ {"explicit_one_hour", `,"cache_write_1h_usd_per_mtok":5`, 1000, 0.005, nil},+ {"mixed_ttls", `,"cache_write_1h_usd_per_mtok":5`, 600, 0.0038, []string{"cache_write_at_input_rate"}},+ {"both_rates_missing", "", 1000, 0.002, []string{"cache_write_1h_at_write_rate", "cache_write_at_input_rate"}},+ } {+ t.Run(tc.name, func(t *testing.T) {+ catalog := pricing.Default()+ overrides, err := pricing.LoadOverrides([]byte(`{"schema":"beacon.pricing.overrides/v1","models":{"internal-model":{"input_usd_per_mtok":2,"output_usd_per_mtok":3`+tc.extraRate+`}}}`), catalog)+ if err != nil {+ t.Fatal(err)+ }+ line := `{"timestamp":"2026-10-12T10:00:00Z","event":{"kind":"agent_runtime","action":"token.usage","category":"metric"},"harness":{"name":"claude_code","collection_method":"poll"},"session":{"id":"s1"},"model":"internal-model","gen_ai":{"usage":{"cache_creation":{"input_tokens":1000,"ephemeral_1h_input_tokens":`+ subset, _ := json.Marshal(tc.oneHour)+ report := Aggregate(decodeEvents(t, line+string(subset)+`}}}}`), Options{Pricer: pricing.NewPricer(catalog, overrides)})+ encoded, err := json.Marshal(report)+ if err != nil {+ t.Fatal(err)+ }+ var wire struct {+ Pricing struct {+ Models []struct {+ Fallbacks []string `json:"fallbacks"`+ EstimatedCostUSD float64 `json:"estimated_cost_usd"`+ } `json:"models"`+ } `json:"pricing"`+ }+ if err := json.Unmarshal(encoded, &wire); err != nil {+ t.Fatal(err)+ }+ if len(wire.Pricing.Models) != 1 {+ t.Fatalf("report JSON = %s", encoded)+ }+ got := wire.Pricing.Models[0]+ if got.EstimatedCostUSD != tc.wantCost || !reflect.DeepEqual(got.Fallbacks, tc.wantFallbacks) {+ t.Fatalf("report pricing = %+v; want cost %v fallbacks %v", got, tc.wantCost, tc.wantFallbacks)+ }+ })+ }+}diff --git a/docs/cli/pricing.mdx b/docs/cli/pricing.mdxindex c280adfe..4d96e4b2 100644--- a/docs/cli/pricing.mdx+++ b/docs/cli/pricing.mdx@@ -112,8 +112,10 @@ confidential: Beacon does not put rates in any event, but the file itself is pla- **A row replaces the catalog row whole.** Fields you leave out are unpublished, notinherited from the catalog, and are priced the way the catalog prices a missing rate: cachereads and cache writes at the row's input rate, one-hour writes at its five-minute write- rate. Those fallbacks are named in the report's `pricing.models[].fallbacks` and by- `beacon pricing show`.+ rate. Reports name only fallbacks actually used for non-zero token counts in+ `pricing.models[].fallbacks`; one-hour writes at an explicit one-hour rate do not use the+ five-minute write fallback. `beacon pricing show` also lists unpublished rates and their+ available fallbacks.- **Bands** apply to every token of a single request whose prompt (input plus cache read pluscache write) is above `above_tokens`, exactly as the catalog's bands do, and only to usageBeacon knows is one request (see [How the estimate is computed](/cli/token-usage#how-the-estimate-is-computed)).