Skip to content

feat: implement Effective Tokens specification in Go#24122

Merged
pelikhan merged 1 commit intomainfrom
copilot/implement-token-specification-in-go
Apr 2, 2026
Merged

feat: implement Effective Tokens specification in Go#24122
pelikhan merged 1 commit intomainfrom
copilot/implement-token-specification-in-go

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 2, 2026

Summary

Implements the Effective Tokens (ET) specification defined in docs/src/content/docs/reference/effective-tokens-specification.md in Go and integrates the computation in the audit and logs commands.

Changes

New files

  • pkg/cli/data/model_multipliers.json – Embedded JSON resource with:
    • Token class weights per the ET spec (w_in=1.0, w_cache=0.1, w_out=4.0, w_reason=4.0, w_cache_write=1.0)
    • Per-model multipliers for 30+ known models (Claude, GPT, Gemini families)
  • pkg/cli/effective_tokens.go – Core ET implementation:
    • Embeds data/model_multipliers.json at compile time via //go:embed
    • Model multiplier lookup with exact match then longest-prefix fallback (handles model variants like claude-sonnet-4.6-preview-*)
    • Implements the full ET formula from the spec:
      base_weighted_tokens = (w_in × I) + (w_cache × C) + (w_out × O) + (w_reason × R)
      effective_tokens     = m × base_weighted_tokens
      
    • populateEffectiveTokens(summary) populates all per-model and total effective token fields after parsing
  • pkg/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 function

Modified files

  • pkg/cli/token_usage.go – Added EffectiveTokens to ModelTokenUsage and ModelTokenUsageRow; added TotalEffectiveTokens to TokenUsageSummary; calls populateEffectiveTokens after parsing token-usage.jsonl
  • pkg/cli/logs_models.go – Added EffectiveTokens int to WorkflowRun
  • pkg/cli/logs_report.go – Added EffectiveTokens to RunData and TotalEffectiveTokens to LogsSummary; propagates effective tokens in buildLogsData
  • pkg/cli/audit_report.go – Added EffectiveTokens to MetricsData; populated from processedRun.TokenUsage.TotalEffectiveTokens
  • pkg/cli/logs_orchestrator.go – Propagates TotalEffectiveTokens from TokenUsageSummary to WorkflowRun.EffectiveTokens in both the fresh-download and cached-run code paths

How it works

  1. When the firewall proxy's token-usage.jsonl is parsed, populateEffectiveTokens applies token class weights + model multiplier to each model's usage → fills EffectiveTokens per model and TotalEffectiveTokens on the summary
  2. The orchestrator propagates TotalEffectiveTokens to WorkflowRun.EffectiveTokens
  3. buildLogsData sums up EffectiveTokens across runs into LogsSummary.TotalEffectiveTokens and includes per-run EffectiveTokens in RunData
  4. The audit report populates MetricsData.EffectiveTokens from the TokenUsageSummary
  5. All fields are surfaced in both JSON (--json flag) and console table output via existing console struct tag rendering

… 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>
@pelikhan pelikhan marked this pull request as ready for review April 2, 2026 13:49
Copilot AI review requested due to automatic review settings April 2, 2026 13:49
Copilot AI requested a review from pelikhan April 2, 2026 13:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +62 to +76
// 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
}

Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +21 to +22
// data/model_multipliers.json file and can be updated without recompilation.
//
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
// 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.

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +16
func TestEffectiveTokenMultiplierExactMatch(t *testing.T) {
// Reset cached multipliers so tests start fresh
loadedMultipliers = nil

tests := []struct {
Copy link

Copilot AI Apr 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 uses AI. Check for mistakes.
@pelikhan
Copy link
Copy Markdown
Collaborator

pelikhan commented Apr 2, 2026

@copilot review comments

@pelikhan pelikhan merged commit 89eaead into main Apr 2, 2026
67 of 68 checks passed
@pelikhan pelikhan deleted the copilot/implement-token-specification-in-go branch April 2, 2026 14:08
Copilot stopped work on behalf of pelikhan due to an error April 2, 2026 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants