Skip to content

Keep token usage label maps within their bound when merged - #468

Open
teamleaderleo wants to merge 2 commits into
mainfrom
fix/token-usage-label-bound
Open

teamleaderleo wants to merge 2 commits into
mainfrom
fix/token-usage-label-bound

Conversation

@teamleaderleo

@teamleaderleo teamleaderleo commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

`addTokenUsageLabelCounts` let a label map fill all `tokenUsageMaxLabelKeys` slots with real labels and then added `other` as a 17th key. When a snapshot merged such a map into an empty one, whether the result stayed at 16 depended on map iteration order, so `TestTokenUsageStopReasonAndErrorLabelsStayBounded` failed roughly one run in seventeen (seen on #397: `stop reasons = 17 keys, want at most 16`).

The fix reserves the last slot for `other`, so a map never exceeds the bound regardless of merge order. Adds a test that merges a full map (16 labels plus `other`) repeatedly and asserts the bound and the total.

Test: `go test -count=20 -run 'TestTokenUsage|TestAddTokenUsageLabel' ./internal/proxy/` passes.

🤖 Generated with Claude Code


Summary by cubic

Fixes addTokenUsageLabelCounts so merged token usage label maps never exceed the tokenUsageMaxLabelKeys bound. Previously, a full map could add other as a 17th key, and whether that key survived merging depended on map iteration order, causing TestTokenUsageStopReasonAndErrorLabelsStayBounded to fail intermittently. The last slot is now reserved for other, and a test repeatedly merges a full map to assert the bound and total.

Written for commit cdcadad. Summary will update on new commits.

Review in cubic

addTokenUsageLabelCounts let a map reach tokenUsageMaxLabelKeys real labels and then added "other" as one more key. Merging such a map into an empty one (as a snapshot does) could land at 17 keys depending on iteration order, which made TestTokenUsageStopReasonAndErrorLabelsStayBounded fail about one run in seventeen. Reserve the last slot for "other".
@coderabbitai

coderabbitai Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 8 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: df98c286-e39a-44b3-bf26-44ba6b514aab

📥 Commits

Reviewing files that changed from the base of the PR and between befea3d and cdcadad.

📒 Files selected for processing (2)
  • internal/proxy/token_usage.go
  • internal/proxy/token_usage_metadata_test.go

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

This branch has not been deployed

No deployments
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.

2 participants