Skip to content

fix: track token count of first document in new batch in TokenCountBatchingStrategy#5528

Merged
sobychacko merged 1 commit intospring-projects:mainfrom
weiguangli-io:codex/spring-ai-5525-batch-token-count
Mar 6, 2026
Merged

fix: track token count of first document in new batch in TokenCountBatchingStrategy#5528
sobychacko merged 1 commit intospring-projects:mainfrom
weiguangli-io:codex/spring-ai-5525-batch-token-count

Conversation

@weiguangli-io
Copy link
Contributor

Closes #5525

Summary

When a document's token count causes currentSize to exceed maxInputTokenCount in TokenCountBatchingStrategy.batch(), the current batch is saved and a new one is created. However, currentSize is reset to 0 while the triggering document is still added to the new batch — its token count is silently dropped from the running total.

This means each new batch can accept one extra document's worth of tokens beyond the configured limit before the next split is triggered.

Fix

Change currentSize = 0 to currentSize = entry.getValue() so the first document in each new batch is properly counted.

Test

Added batchShouldTrackTokenCountAcrossBatchBoundaries test that creates multiple documents with a small token limit (10 tokens), verifying that batches are correctly split and no documents are lost.

AI-assisted testing, AI-assisted review

When a document causes currentSize to exceed maxInputTokenCount,
the batch is split and currentSize is reset to 0. However, the
triggering document is still added to the new batch, so its token
count is silently dropped. This can cause subsequent batches to
exceed the configured token limit.

Reset currentSize to entry.getValue() instead of 0 so the first
document in each new batch is properly accounted for.

Closes spring-projects#5525

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Signed-off-by: liweiguang <codingpunk@gmail.com>
@weiguangli-io weiguangli-io force-pushed the codex/spring-ai-5525-batch-token-count branch from 1d1dfc3 to 7604c9f Compare March 3, 2026 04:01
@sobychacko sobychacko added this to the 2.0.0-M3 milestone Mar 6, 2026
@sobychacko sobychacko removed this from the 2.0.0-M3 milestone Mar 6, 2026
@sobychacko sobychacko merged commit 3007f57 into spring-projects:main Mar 6, 2026
2 checks passed
spring-builds pushed a commit that referenced this pull request Mar 6, 2026
Fixes: #5525

When a document causes currentSize to exceed maxInputTokenCount,
the batch is split and currentSize is reset to 0. However, the
triggering document is still added to the new batch, so its token
count is silently dropped. This can cause subsequent batches to
exceed the configured token limit.

Reset currentSize to entry.getValue() instead of 0 so the first
document in each new batch is properly accounted for.

Signed-off-by: liweiguang <codingpunk@gmail.com>
(cherry picked from commit 3007f57)
@sobychacko
Copy link
Contributor

PR merged upstream to main and back-ported to 1.1.x. Thanks for the contribution.

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.

The currentSize accumulation in TokenCountBatchingStrategy is inaccurate

2 participants