Skip to content

Preserve cached token counts in MessageAggregator - #7494

Open
ChaseMoon233 wants to merge 1 commit into
spring-projects:mainfrom
ChaseMoon233:fix/7491-message-aggregator-cached-tokens
Open

ChaseMoon233 wants to merge 1 commit into
spring-projects:mainfrom
ChaseMoon233:fix/7491-message-aggregator-cached-tokens

Conversation

@ChaseMoon233

Copy link
Copy Markdown

Fixes #7491

Summary

  • Track cacheReadInputTokens and cacheWriteInputTokens in MessageAggregator (initialized and reset to null in doOnSubscribe) instead of dropping them during stream aggregation.
  • Guard against later null or 0L usage chunks overwriting an earlier positive cached token count, while preserving null when not reported and 0L when explicitly reported.
  • Extend MessageAggregator.DefaultUsage with cacheReadInputTokens and cacheWriteInputTokens while keeping the existing 3-argument constructor for backwards compatibility.
  • Add unit tests in MessageAggregatorTests covering normal propagation, non-overwrite across chunks, null preservation, explicit 0L, and 3-arg constructor compatibility.

Verification

  • Baseline commit: ea0cc8c
  • Before fix (./mvnw.cmd test -pl spring-ai-model -Dtest=MessageAggregatorTests): Tests run: 24, Failures: 3, Errors: 0, Skipped: 0 (the 3 new cached-token tests failed with Expecting actual not to be null).
  • After fix (./mvnw.cmd test -pl spring-ai-model -Dtest=MessageAggregator,UsageCalculatorTests,DefaultUsageTests): Tests run: 73, Failures: 0, Errors: 0, Skipped: 0 (DefaultUsageTests: 31, MessageAggregatorTests: 24, UsageCalculatorTests: 18).
  • Formatting & Checkstyle (./mvnw.cmd test-compile -pl spring-ai-model -P!format-apply,format-check): Passed with 0 Checkstyle violations.

Signed-off-by: 弥月纱 <2649788453@qq.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MessageAggregator drops cached tokens from usage, so streamed tool loops under-report cache hits

2 participants