@@ -19,11 +19,11 @@ type TokenMetadata struct {
1919 BaselineTokens int `json:"baseline_tokens"`
2020 ResponseTokens int `json:"response_tokens"`
2121 CostAvoided float64 `json:"cost_avoided"`
22- CompressionRatio float64 `json:"compression_ratio "`
22+ ReductionRatio float64 `json:"reduction_ratio "`
2323}
2424
2525// EstimateTokens approximates token count from byte length using
26- // the standard English heuristic of 1 token ≈ 4 characters .
26+ // the heuristic of 1 token ≈ 4 bytes (accurate for ASCII-heavy source code) .
2727func EstimateTokens (s string ) int {
2828 return len (s ) / 4
2929}
@@ -48,7 +48,7 @@ func CalculateSavings(baselineBytes, responseBytes int, pricePerToken float64) T
4848 BaselineTokens : baseline ,
4949 ResponseTokens : response ,
5050 CostAvoided : math .Round (float64 (saved )* pricePerToken * 1e6 ) / 1e6 ,
51- CompressionRatio : ratio ,
51+ ReductionRatio : ratio ,
5252 }
5353}
5454
@@ -61,12 +61,13 @@ type savingsRecord struct {
6161}
6262
6363// Tracker accumulates token savings across calls and persists totals.
64+ // Use Snapshot() to read totals safely — do not access fields directly.
6465type Tracker struct {
6566 mu sync.Mutex
6667 path string
67- InstallID string
68- TotalTokensSaved int64
69- TotalCostAvoided float64
68+ installID string
69+ totalTokensSaved int64
70+ totalCostAvoided float64
7071}
7172
7273// NewTracker loads or creates savings.json at path. Generates a random
@@ -79,20 +80,20 @@ func NewTracker(path string) *Tracker {
7980 if ! os .IsNotExist (err ) {
8081 slog .Warn ("metrics: failed to read savings file" , "path" , path , "err" , err )
8182 }
82- t .InstallID = newInstallID ()
83+ t .installID = newInstallID ()
8384 return t
8485 }
8586
8687 var rec savingsRecord
8788 if err := json .Unmarshal (data , & rec ); err != nil {
8889 slog .Warn ("metrics: malformed savings file, starting fresh" , "path" , path , "err" , err )
89- t .InstallID = newInstallID ()
90+ t .installID = newInstallID ()
9091 return t
9192 }
9293
93- t .InstallID = rec .InstallID
94- t .TotalTokensSaved = rec .TotalTokensSaved
95- t .TotalCostAvoided = rec .TotalCostAvoided
94+ t .installID = rec .InstallID
95+ t .totalTokensSaved = rec .TotalTokensSaved
96+ t .totalCostAvoided = rec .TotalCostAvoided
9697 return t
9798}
9899
@@ -102,13 +103,13 @@ func (t *Tracker) Record(meta TokenMetadata) {
102103 t .mu .Lock ()
103104 defer t .mu .Unlock ()
104105
105- t .TotalTokensSaved += int64 (meta .TokensSaved )
106- t .TotalCostAvoided += meta .CostAvoided
106+ t .totalTokensSaved += int64 (meta .TokensSaved )
107+ t .totalCostAvoided += meta .CostAvoided
107108
108109 rec := savingsRecord {
109- InstallID : t .InstallID ,
110- TotalTokensSaved : t .TotalTokensSaved ,
111- TotalCostAvoided : t .TotalCostAvoided ,
110+ InstallID : t .installID ,
111+ TotalTokensSaved : t .totalTokensSaved ,
112+ TotalCostAvoided : t .totalCostAvoided ,
112113 LastUpdated : time .Now ().UTC ().Format (time .RFC3339 ),
113114 }
114115
@@ -144,14 +145,14 @@ func (t *Tracker) Record(meta TokenMetadata) {
144145 return
145146 }
146147
147- slog .Debug ("metrics: savings persisted" , "path" , t .path , "total_tokens_saved" , t .TotalTokensSaved )
148+ slog .Debug ("metrics: savings persisted" , "path" , t .path , "total_tokens_saved" , t .totalTokensSaved )
148149}
149150
150151// Snapshot returns current cumulative totals under lock.
151152func (t * Tracker ) Snapshot () (totalTokensSaved int64 , totalCostAvoided float64 ) {
152153 t .mu .Lock ()
153154 defer t .mu .Unlock ()
154- return t .TotalTokensSaved , t .TotalCostAvoided
155+ return t .totalTokensSaved , t .totalCostAvoided
155156}
156157
157158func newInstallID () string {
0 commit comments