-
Notifications
You must be signed in to change notification settings - Fork 320
fix: cache token double counting at message_start #2241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xiaolin593
wants to merge
2
commits into
envoyproxy:main
Choose a base branch
from
xiaolin593:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+32
−7
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1157,3 +1157,35 @@ func TestBuildAnthropicParamsWithReasoningEffort(t *testing.T) { | |
| require.Equal(t, anthropic.OutputConfigEffort(""), params.OutputConfig.Effort) | ||
| }) | ||
| } | ||
|
|
||
| // TestAnthropicStreamParser_CacheTokensNotSetFromMessageStart verifies that cache tokens are not | ||
| // written during message_start processing. Before the fix, message_start called SetCachedInputTokens | ||
| // and SetCacheCreationInputTokens; if message_delta then called AddCachedInputTokens with the same | ||
| // values, both would be counted, doubling the result. | ||
| func TestAnthropicStreamParser_CacheTokensNotSetFromMessageStart(t *testing.T) { | ||
| p := newAnthropicStreamParser("claude-3-5-sonnet") | ||
|
|
||
| // Simulate message_start with cache tokens. | ||
| messageStart := []byte(`{"type":"message_start","message":{"id":"msg_test","type":"message","role":"assistant","content":[],"model":"claude-3-5-sonnet","stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":10,"cache_read_input_tokens":5,"cache_creation_input_tokens":2,"output_tokens":0}}}`) | ||
| _, err := p.handleAnthropicStreamEvent([]byte("message_start"), messageStart) | ||
| require.NoError(t, err) | ||
|
|
||
| // Cache tokens must NOT be set after message_start; only input tokens are set. | ||
| _, cachedSet := p.tokenUsage.CachedInputTokens() | ||
| require.False(t, cachedSet, "cache_read tokens must not be set from message_start") | ||
| _, cacheCreationSet := p.tokenUsage.CacheCreationInputTokens() | ||
| require.False(t, cacheCreationSet, "cache_creation tokens must not be set from message_start") | ||
|
|
||
| // Simulate message_delta carrying the authoritative cache and output counts. | ||
| messageDelta := []byte(`{"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":8,"cache_read_input_tokens":5,"cache_creation_input_tokens":2}}`) | ||
| _, err = p.handleAnthropicStreamEvent([]byte("message_delta"), messageDelta) | ||
| require.NoError(t, err) | ||
|
|
||
| cachedTokens, cachedSet := p.tokenUsage.CachedInputTokens() | ||
| require.True(t, cachedSet) | ||
| require.Equal(t, uint32(5), cachedTokens, "cache_read tokens must not be double-counted") | ||
|
|
||
| cacheCreationTokens, cacheCreationSet := p.tokenUsage.CacheCreationInputTokens() | ||
| require.True(t, cacheCreationSet) | ||
| require.Equal(t, uint32(2), cacheCreationTokens, "cache_creation tokens must not be double-counted") | ||
|
Comment on lines
+1184
to
+1190
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make the test more robust and ensure that the total input tokens (prompt tokens) and output tokens are also correctly calculated and not double-counted, consider adding assertions for the total input and output tokens. cachedTokens, cachedSet := p.tokenUsage.CachedInputTokens()
require.True(t, cachedSet)
require.Equal(t, uint32(5), cachedTokens, "cache_read tokens must not be double-counted")
cacheCreationTokens, cacheCreationSet := p.tokenUsage.CacheCreationInputTokens()
require.True(t, cacheCreationSet)
require.Equal(t, uint32(2), cacheCreationTokens, "cache_creation tokens must not be double-counted")
inputTokens, inputSet := p.tokenUsage.InputTokens()
require.True(t, inputSet)
require.Equal(t, uint32(17), inputTokens, "total input tokens must be correctly calculated")
outputTokens, outputSet := p.tokenUsage.OutputTokens()
require.True(t, outputSet)
require.Equal(t, uint32(8), outputTokens, "output tokens must be correctly set") |
||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think SetInputTokens should also be moved to MessageDelta block, so all token related thing happens at one place