feat: implement Effective Tokens specification in Go#24122
Conversation
… JSON - Add pkg/cli/data/model_multipliers.json with token class weights and per-model multipliers embedded at compile time - Add pkg/cli/effective_tokens.go implementing the ET formula: base_weighted_tokens = (w_in×I) + (w_cache×C) + (w_out×O) + (w_reason×R) effective_tokens = m × base_weighted_tokens - Add EffectiveTokens field to ModelTokenUsage, TotalEffectiveTokens to TokenUsageSummary, EffectiveTokens to WorkflowRun, RunData, MetricsData and TotalEffectiveTokens to LogsSummary - Populate effective tokens after token-usage.jsonl parsing; propagate to audit MetricsData and logs RunData/LogsSummary display - Add tests for multiplier lookup, base weighted tokens, ET computation, and populateEffectiveTokens Agent-Logs-Url: https://github.com/github/gh-aw/sessions/1c8b15c4-3885-4c12-90d3-668d9a472adf Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Implements the Effective Tokens (ET) specification in the Go CLI, computing ET from firewall proxy token usage (token class weights × per-model multipliers) and surfacing the resulting totals in logs and audit outputs.
Changes:
- Added embedded ET data (
model_multipliers.json) and core ET computation utilities (effective_tokens.go) with unit tests. - Extended token usage aggregation models to store per-model and total effective tokens, computed after parsing
token-usage.jsonl. - Propagated effective token totals through logs orchestration and reporting, and into the audit report metrics.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/cli/data/model_multipliers.json | Adds token class weights + per-model multiplier table used for ET computation. |
| pkg/cli/effective_tokens.go | Implements ET initialization, model multiplier lookup (incl. prefix matching), and summary population. |
| pkg/cli/effective_tokens_test.go | Adds unit tests for multiplier lookup, base/ET calculations, and population behavior. |
| pkg/cli/token_usage.go | Adds effective token fields to models and computes totals post-parse. |
| pkg/cli/logs_models.go | Adds EffectiveTokens to WorkflowRun for downstream reporting. |
| pkg/cli/logs_orchestrator.go | Propagates ET totals from parsed token usage into each run (cached + fresh paths). |
| pkg/cli/logs_report.go | Includes per-run and total effective tokens in structured logs output and summaries. |
| pkg/cli/audit_report.go | Surfaces effective tokens in audit metrics derived from token usage summary / run data. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // loadedMultipliers is the parsed multiplier table, keyed by lowercase model name. | ||
| // Initialized once on first call to effectiveTokenMultiplier. | ||
| var loadedMultipliers map[string]float64 | ||
|
|
||
| // loadedTokenWeights holds the token class weights from the JSON file. | ||
| // Initialized once on first call to initMultipliers. | ||
| var loadedTokenWeights tokenClassWeights | ||
|
|
||
| // initMultipliers parses the embedded JSON and populates loadedMultipliers and | ||
| // loadedTokenWeights. Safe to call multiple times; only initializes once. | ||
| func initMultipliers() { | ||
| if loadedMultipliers != nil { | ||
| return | ||
| } | ||
|
|
There was a problem hiding this comment.
loadedMultipliers / loadedTokenWeights are lazily initialized via a nil check without synchronization. Since token usage parsing runs concurrently (e.g., in downloadRunArtifactsConcurrent), this can cause data races and undefined behavior under -race (concurrent reads/writes to the globals and the map). Use sync.Once (or a mutex) to guard initialization and make reads safe, and provide a test-only reset hook if tests need to force re-init.
| // data/model_multipliers.json file and can be updated without recompilation. | ||
| // |
There was a problem hiding this comment.
The comment says the embedded data/model_multipliers.json "can be updated without recompilation", but //go:embed bakes the file into the binary, so updates require rebuilding. Either adjust the comment to reflect that, or implement an override mechanism (e.g., optional external file/env var) if runtime updates are intended.
| // data/model_multipliers.json file and can be updated without recompilation. | |
| // | |
| // data/model_multipliers.json file, which is compiled into the binary. Updates | |
| // to this file require recompilation. |
| func TestEffectiveTokenMultiplierExactMatch(t *testing.T) { | ||
| // Reset cached multipliers so tests start fresh | ||
| loadedMultipliers = nil | ||
|
|
||
| tests := []struct { |
There was a problem hiding this comment.
These tests reset loadedMultipliers to force re-initialization, but they don't reset loadedTokenWeights (and any future init guard like sync.Once). If tests are run in parallel or a sync.Once is introduced to fix the init race, you'll need a package-private test helper to fully reset all cached ET state to keep tests isolated.
|
@copilot review comments |
Summary
Implements the Effective Tokens (ET) specification defined in
docs/src/content/docs/reference/effective-tokens-specification.mdin Go and integrates the computation in the audit and logs commands.Changes
New files
pkg/cli/data/model_multipliers.json– Embedded JSON resource with:pkg/cli/effective_tokens.go– Core ET implementation:data/model_multipliers.jsonat compile time via//go:embedclaude-sonnet-4.6-preview-*)populateEffectiveTokens(summary)populates all per-model and total effective token fields after parsingpkg/cli/effective_tokens_test.go– Tests covering exact match, case-insensitive lookup, prefix matching, base weighted token computation, per-model ET computation, and the populate functionModified files
pkg/cli/token_usage.go– AddedEffectiveTokenstoModelTokenUsageandModelTokenUsageRow; addedTotalEffectiveTokenstoTokenUsageSummary; callspopulateEffectiveTokensafter parsingtoken-usage.jsonlpkg/cli/logs_models.go– AddedEffectiveTokens inttoWorkflowRunpkg/cli/logs_report.go– AddedEffectiveTokenstoRunDataandTotalEffectiveTokenstoLogsSummary; propagates effective tokens inbuildLogsDatapkg/cli/audit_report.go– AddedEffectiveTokenstoMetricsData; populated fromprocessedRun.TokenUsage.TotalEffectiveTokenspkg/cli/logs_orchestrator.go– PropagatesTotalEffectiveTokensfromTokenUsageSummarytoWorkflowRun.EffectiveTokensin both the fresh-download and cached-run code pathsHow it works
token-usage.jsonlis parsed,populateEffectiveTokensapplies token class weights + model multiplier to each model's usage → fillsEffectiveTokensper model andTotalEffectiveTokenson the summaryTotalEffectiveTokenstoWorkflowRun.EffectiveTokensbuildLogsDatasums upEffectiveTokensacross runs intoLogsSummary.TotalEffectiveTokensand includes per-runEffectiveTokensinRunDataMetricsData.EffectiveTokensfrom theTokenUsageSummary--jsonflag) and console table output via existingconsolestruct tag rendering