fix: correct token double-counting for Anthropic and Bedrock providers#2861
Merged
tusharmath merged 3 commits intomainfrom Apr 6, 2026
Merged
fix: correct token double-counting for Anthropic and Bedrock providers#2861tusharmath merged 3 commits intomainfrom
tusharmath merged 3 commits intomainfrom
Conversation
Bug 1 - Anthropic double-counting: Anthropic streams usage as CUMULATIVE values across message_start and message_delta events. The code was using accumulate (sum) to combine them, causing output_tokens to be over-counted (1 + N instead of N) when message_start includes output_tokens=1. Fix: Introduced Usage::merge() which uses max() instead of sum for token fields. This correctly handles cumulative values - the larger value wins. ref: https://platform.claude.com/docs/en/build-with-claude/streaming#event-types Affected providers: anthropic, claude_code, anthropic_compatible, vertex_ai_anthropic, minimax, alibaba_coding, opencode_zen (claude-* models) Bug 2 - Bedrock prompt_tokens: Bedrock was setting prompt_tokens to total_tokens instead of input_tokens, inflating the reported input token count by including output tokens. Fix: Changed u.total_tokens to u.input_tokens. Affected providers: All bedrock models. Also fixed: cost-only events now properly accumulate costs (sum) instead of replacing them. Co-Authored-By: ForgeCode <noreply@forgecode.dev>
The previous implementation used Deref comparison which returned the original variant unchanged. When Actual(200) was compared with Approx(100), it returned Actual(200) - violating the documented contract that the result should be Approx if either input is Approx. Now uses explicit match to ensure Approx propagation matches documentation. Co-Authored-By: ForgeCode <noreply@forgecode.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix token double-counting for Anthropic providers and incorrect
prompt_tokensmapping for Bedrock, bringing token reporting.Context
Anthropic double-counting: Anthropic streams usage as cumulative values across
message_startandmessage_deltaevents (ref). Our code was usingaccumulate()(sum) to combine them, producing1 + Ninstead of the correctNfor output tokens.Bedrock prompt_tokens:
prompt_tokenswas set tototal_tokensinstead ofinput_tokens, inflating the reported input count by including output tokens.Changes
Bug 1 – Anthropic cumulative usage (6 providers affected)
crates/forge_domain/src/message.rs: AddedUsage::merge()which usesmax()per token field instead of+. Cost is still summed since cost events are additive.crates/forge_domain/src/context.rs: AddedTokenCount::max()for comparing two token counts by inner value while preservingActual/Approxsemantics.crates/forge_domain/src/result_stream_ext.rs: Changed partial-usage branch fromaccumulate()tomerge(). Also fixed cost-only events to sum costs instead of replacing.Affected providers:
anthropic,claude_code,anthropic_compatible,vertex_ai_anthropic,minimax,alibaba_coding.Bug 2 – Bedrock prompt_tokens
crates/forge_repo/src/provider/bedrock.rs: Changedu.total_tokens→u.input_tokens.Affected providers: all Bedrock models.
Key Implementation Details
The distinction between
accumulateandmerge:accumulate()merge()Documentation references
Testing
All existing tests updated + new tests added:
test_into_full_anthropic_streaming_usage_merge– covers real Anthropic pattern wheremessage_starthasoutput_tokens=1test_into_full_anthropic_streaming_usage_merge_zero_output– covers Vertex AI pattern wheremessage_starthasoutput_tokens=0test_usage_merge_anthropic_cumulative– unit test for merge logictest_usage_merge_preserves_costs– verifies cost summation in mergeAll 1,381 lib tests pass.