Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Group token attribution on the canonical model name

Claude3beef242358d
diff --git a/cli/beacon/internal/endpoint/dashboard/summary.go b/cli/beacon/internal/endpoint/dashboard/summary.go
index 497f2525..8130f1d7 100644
--- a/cli/beacon/internal/endpoint/dashboard/summary.go
+++ b/cli/beacon/internal/endpoint/dashboard/summary.go
@@ -5,6 +5,7 @@ import (
"time"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
type Count struct {
@@ -90,8 +91,12 @@ func BuildSummary(result EventResult) Summary {
if event.Repository != "" {
summary.CountsByRepository[event.Repository]++
}
- if event.Model != "" {
- summary.CountsByModel[event.Model]++
+ // Counted under the canonical name for the reason the token rollups group on it: an event
+ // count keyed on the raw spelling splits one model across several rows, and this summary
+ // sits on the same page as a spend rollup that does not. One page disagreeing with itself
+ // about how many models ran is worse than either answer alone.
+ if model := asymptoteobserve.NormalizeModelName(event.Model); model != "" {
+ summary.CountsByModel[model]++
}
if event.MCP != nil && event.MCP.Server != "" {
summary.CountsByMCPServer[event.MCP.Server]++
diff --git a/cli/beacon/internal/endpoint/dashboard/summary_test.go b/cli/beacon/internal/endpoint/dashboard/summary_test.go
new file mode 100644
index 00000000..8d19da32
--- /dev/null
+++ b/cli/beacon/internal/endpoint/dashboard/summary_test.go
@@ -0,0 +1,27 @@
+package dashboard
+
+import (
+ "testing"
+
+ "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+)
+
+// The same model under two spellings is one model here too. This summary renders beside a spend
+// rollup that groups canonically, so counting the raw string would have one page report two models
+// where the other reports one.
+func TestSummaryCountsOneModelUnderItsCanonicalName(t *testing.T) {
+ result := EventResult{
+ TotalMatched: 2,
+ Events: []EventRecord{
+ {Event: schema.Event{Timestamp: "2026-06-11T10:00:00Z", Model: "claude-opus-5"}},
+ {Event: schema.Event{Timestamp: "2026-06-11T10:01:00Z", Model: "anthropic/Claude-Opus-5"}},
+ },
+ }
+ summary := BuildSummary(result)
+ if got := summary.CountsByModel["claude-opus-5"]; got != 2 {
+ t.Errorf("counts_by_model[claude-opus-5] = %d, want 2; full map %v", got, summary.CountsByModel)
+ }
+ if len(summary.CountsByModel) != 1 {
+ t.Errorf("counts_by_model = %v, want a single model", summary.CountsByModel)
+ }
+}
diff --git a/cli/beacon/internal/tokens/tokens.go b/cli/beacon/internal/tokens/tokens.go
index 68b07c3b..4f7b461d 100644
--- a/cli/beacon/internal/tokens/tokens.go
+++ b/cli/beacon/internal/tokens/tokens.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
)
const defaultNearLimitRatio = 0.8
@@ -417,13 +418,25 @@ func collectUsageEvents(events []schema.Event, sessionUsers sessionUserIndex) []
usage = &schema.GenAIUsageInfo{}
}
ue := &usageEvent{
- order: i,
- action: event.Event.Action,
- name: event.Message,
- endpoint: event.Endpoint.Hostname,
- harness: event.Harness.Name,
- user: userKey(event.User),
- model: event.Model,
+ order: i,
+ action: event.Event.Action,
+ name: event.Message,
+ endpoint: event.Endpoint.Hostname,
+ harness: event.Harness.Name,
+ user: userKey(event.User),
+ // Canonicalized on the way in, so every consumer downstream -- the by-model rollup,
+ // utilization, the coverage join, the Codex span preference that keys on model --
+ // agrees about what one model is called, from one place.
+ //
+ // Every write path already does this (see normalizeEventModel in the hook logger and
+ // SplitModelProvider in the collector exporter), so a log Beacon wrote recently is
+ // already canonical and this is a no-op on it. It is here for the logs where that does
+ // not hold: those written before that rule existed, which the runtime log retains and
+ // a --since window spans, and any JSONL Beacon reads without having written it.
+ //
+ // Grouping only. The stored event is untouched, and the raw spelling a runtime used
+ // stays in the log where an investigator can still see it.
+ model: asymptoteobserve.NormalizeModelName(event.Model),
repository: event.Repository,
usage: Usage{Events: 1},
}
diff --git a/cli/beacon/internal/tokens/tokens_test.go b/cli/beacon/internal/tokens/tokens_test.go
index ef930c5b..5012eaf9 100644
--- a/cli/beacon/internal/tokens/tokens_test.go
+++ b/cli/beacon/internal/tokens/tokens_test.go
@@ -644,3 +644,66 @@ func TestSessionModelCarryForwardDoesNotRelabelSpend(t *testing.T) {
}
}
}
+
+// Every path that writes an event now canonicalizes the model name, so a log Beacon wrote after
+// that landed cannot split. This is about the logs where that does not hold: everything written
+// before the rule existed, which the runtime log retains and a --since window spans, and any JSONL
+// Beacon reads without having written -- a customer's own file, or events forwarded in from a
+// producer with its own conventions.
+//
+// Reading is where a report can still be made whole for those, and grouping is the only place it
+// matters: three spellings of one model are three rows, each holding a third of the bill, with
+// nothing on the report saying so. A split row is not obviously wrong to a reader, which is what
+// makes it worth closing rather than tolerating.
+func TestAggregateGroupsOneModelReportedUnderSeveralSpellings(t *testing.T) {
+ mk := func(model string) schema.Event {
+ return usageEventFixture("2026-06-11T10:00:00Z", "claude_code", "s1", model, func(e *schema.Event) {
+ e.GenAI.Usage.InputTokens = int64Ptr(10)
+ })
+ }
+ report := Aggregate([]schema.Event{
+ mk("claude-opus-5"),
+ mk("anthropic/claude-opus-5"),
+ mk("Claude-Opus-5"),
+ mk(" openrouter/anthropic/Claude-Opus-5 "),
+ }, Options{})
+
+ if len(report.ByModel) != 1 {
+ keys := make([]string, 0, len(report.ByModel))
+ for _, group := range report.ByModel {
+ keys = append(keys, group.Key)
+ }
+ t.Fatalf("by-model rows = %v, want one row for one model", keys)
+ }
+ row := report.ByModel[0]
+ if row.Key != "claude-opus-5" {
+ t.Errorf("by-model key = %q, want the canonical spelling", row.Key)
+ }
+ if row.Usage.TotalTokens() != 40 {
+ t.Errorf("by-model tokens = %d, want all 40 under the one row", row.Usage.TotalTokens())
+ }
+ if len(report.Utilization) != 1 {
+ t.Errorf("utilization rows = %d, want one -- it keys on the model too", len(report.Utilization))
+ }
+ // The totals never split, so they are the control: they were right before this change and must
+ // be unchanged by it.
+ if report.Totals.TotalTokens() != 40 {
+ t.Errorf("totals = %d, want 40", report.Totals.TotalTokens())
+ }
+}
+
+// The normalization is the shared one, so what it declines to do it declines to do here. Folding
+// the dot in "claude-sonnet-4.6" would merge it with Anthropic's "claude-sonnet-4-6", and the same
+// rule turns "gpt-4.1" into a model that exists under no name. A visible split beats an invented
+// id, so these stay two rows.
+func TestAggregateDoesNotMergeModelsThatOnlyLookAlike(t *testing.T) {
+ mk := func(model string) schema.Event {
+ return usageEventFixture("2026-06-11T10:00:00Z", "claude_code", "s1", model, func(e *schema.Event) {
+ e.GenAI.Usage.InputTokens = int64Ptr(10)
+ })
+ }
+ report := Aggregate([]schema.Event{mk("claude-sonnet-4.6"), mk("claude-sonnet-4-6")}, Options{})
+ if len(report.ByModel) != 2 {
+ t.Fatalf("by-model rows = %d, want 2 -- these are different ids and merging them needs a catalog", len(report.ByModel))
+ }
+}