Repository navigation
fix(core): correct context-usage Footer for prompt size and Anthropic caches #4109
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
423e0f8
fix(core): include cache_creation_input_tokens in Anthropic prompt ac…
tanzhenxin 67aaa26
fix(core): prefer promptTokenCount over totalTokenCount for context d…
tanzhenxin 4752b07
fix(core): use cache_creation as the discriminator in Anthropic usage…
tanzhenxin 34ac7bc
Merge remote-tracking branch 'origin/main' into fix/context-usage-dis…
tanzhenxin eb4a33e
chore(core): address PR review — restore isFinite guard and cover cac…
tanzhenxin 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
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
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
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
109 changes: 109 additions & 0 deletions
109
packages/core/src/core/anthropicContentGenerator/usage.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { buildAnthropicUsageMetadata } from './usage.js'; | ||
|
|
||
| describe('buildAnthropicUsageMetadata', () => { | ||
| it('sums all three prompt fields under standard Anthropic semantics', () => { | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 5_000, | ||
| cacheReadTokens: 25_000, | ||
| cacheCreationTokens: 0, | ||
| outputTokens: 1_000, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 30_000, | ||
| candidatesTokenCount: 1_000, | ||
| totalTokenCount: 31_000, | ||
| cachedContentTokenCount: 25_000, | ||
| }); | ||
| }); | ||
|
|
||
| it('sums when only cache_creation is set (first cache write)', () => { | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 10_000, | ||
| cacheReadTokens: 0, | ||
| cacheCreationTokens: 20_000, | ||
| outputTokens: 500, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 30_000, | ||
| candidatesTokenCount: 500, | ||
| totalTokenCount: 30_500, | ||
| cachedContentTokenCount: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it('uses inputTokens alone when it already covers cache fields (OpenAI semantics on Anthropic protocol)', () => { | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 30_000, | ||
| cacheReadTokens: 25_000, | ||
| cacheCreationTokens: 0, | ||
| outputTokens: 800, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 30_000, | ||
| candidatesTokenCount: 800, | ||
| totalTokenCount: 30_800, | ||
| cachedContentTokenCount: 25_000, | ||
| }); | ||
| }); | ||
|
|
||
| it('reports inputTokens directly when no cache fields are present', () => { | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 12_345, | ||
| cacheReadTokens: 0, | ||
| cacheCreationTokens: 0, | ||
| outputTokens: 678, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 12_345, | ||
| candidatesTokenCount: 678, | ||
| totalTokenCount: 13_023, | ||
| cachedContentTokenCount: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it('keeps summing when inputTokens grows past cache_creation in a long Anthropic conversation', () => { | ||
| // Regression: an earlier guard mis-classified this as OpenAI-style | ||
| // (because input >= cache_creation) and dropped the cache_creation | ||
| // portion, producing a one-shot Footer "drop" at the crossover point. | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 50_000, | ||
| cacheReadTokens: 0, | ||
| cacheCreationTokens: 32_088, | ||
| outputTokens: 200, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 82_088, | ||
| candidatesTokenCount: 200, | ||
| totalTokenCount: 82_288, | ||
| cachedContentTokenCount: 0, | ||
| }); | ||
| }); | ||
|
|
||
| it('handles all-zero usage cleanly', () => { | ||
| expect( | ||
| buildAnthropicUsageMetadata({ | ||
| inputTokens: 0, | ||
| cacheReadTokens: 0, | ||
| cacheCreationTokens: 0, | ||
| outputTokens: 0, | ||
| }), | ||
| ).toEqual({ | ||
| promptTokenCount: 0, | ||
| candidatesTokenCount: 0, | ||
| totalTokenCount: 0, | ||
| cachedContentTokenCount: 0, | ||
| }); | ||
| }); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Qwen | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import type { GenerateContentResponseUsageMetadata } from '@google/genai'; | ||
|
|
||
| export interface AnthropicTokenParts { | ||
| inputTokens: number; | ||
| cacheReadTokens: number; | ||
| cacheCreationTokens: number; | ||
| outputTokens: number; | ||
| } | ||
|
|
||
| /** | ||
| * Normalize Anthropic-side token counts into Gemini's `usageMetadata` shape. | ||
| * | ||
| * Anthropic reports the prompt across three mutually-exclusive fields: | ||
| * `input_tokens`, `cache_read_input_tokens`, `cache_creation_input_tokens`. | ||
| * The full prompt is the sum. | ||
| * | ||
| * `cache_creation_input_tokens` is unique to Anthropic's protocol — OpenAI | ||
| * has no equivalent — so its presence is a strong signal the response | ||
| * follows real Anthropic semantics. Use that as the primary discriminator: | ||
| * | ||
| * - cache_creation > 0 → Anthropic semantics, sum all three | ||
| * - else if cache_read > 0 and input ≥ cache_read → OpenAI-style on the | ||
| * Anthropic protocol (input already covers the cached portion), trust | ||
| * input alone | ||
| * - else → sum (when no cache activity, sum equals input) | ||
| * | ||
| * An earlier version of this guard compared `inputTokens` to *both* cache | ||
| * fields and fell back to "input alone" whenever input was the larger | ||
| * value. That mis-fired on long real Anthropic conversations: once enough | ||
| * history accumulates, `inputTokens` naturally grows past | ||
| * `cache_creation_input_tokens`, which would silently drop the cache | ||
| * portion from the displayed prompt size and produce a one-shot Footer | ||
| * "drop" at the crossover point. | ||
| */ | ||
| export function buildAnthropicUsageMetadata( | ||
| parts: AnthropicTokenParts, | ||
| ): GenerateContentResponseUsageMetadata { | ||
| const { inputTokens, cacheReadTokens, cacheCreationTokens, outputTokens } = | ||
| parts; | ||
| const looksLikeOpenAi = | ||
|
tanzhenxin marked this conversation as resolved.
Resolved
|
||
| cacheCreationTokens === 0 && | ||
| cacheReadTokens > 0 && | ||
| inputTokens >= cacheReadTokens; | ||
| const promptTotal = looksLikeOpenAi | ||
| ? inputTokens | ||
| : inputTokens + cacheReadTokens + cacheCreationTokens; | ||
| return { | ||
| promptTokenCount: promptTotal, | ||
| candidatesTokenCount: outputTokens, | ||
| totalTokenCount: promptTotal + outputTokens, | ||
| cachedContentTokenCount: cacheReadTokens, | ||
| }; | ||
| } | ||
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.