feat(langchain): support custom token_counter in ContextEditingMiddleware - #39754
Merged
ccurme (ccurme) merged 1 commit intoAug 19, 2026
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
Merging this PR will not alter performance
Comparing Footnotes
|
ccurme (ccurme)
approved these changes
Aug 19, 2026
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.
Description
ContextEditingMiddlewarehardcodes its token counter insidewrap_model_call/awrap_model_call:token_count_methodonly allows"approximate"(which callscount_tokens_approximatelywith its defaults) or"model". There is no way to inject a custom counter, even though:TokenCountertype alias, andContextEdit.apply()already receivescount_tokensas a parameter — the extension point exists everywhere except the middleware's public API;SummarizationMiddlewarealready acceptstoken_counter: TokenCounter = count_tokens_approximately, so this change simply brings the two middlewares to API parity.Why this matters:
count_tokens_approximatelyassumes ~4 chars/token, which fits English prose but systematically underestimates CJK-heavy workloads by 2–3x (we measured 1.4–2.1 chars/token against provider-reportedusage_metadataon Chinese financial filings and HTML tables). With the estimate reading 2–3x low,ClearToolUsesEdit.triggerfires far later than configured, silently defeating the purpose of threshold-based context management. The"model"counting mode is not a general fallback: many OpenAI-compatible providers do not implementget_num_tokens_from_messages.Changes
token_counter: TokenCounter | None = NonetoContextEditingMiddleware.__init__; when provided it takes precedence overtoken_count_method(mirroringSummarizationMiddleware)._resolve_token_counter()helper.token_count_method.Example:
Backwards compatible: no behavior change unless the new argument is passed. No new dependencies.
Issue
Fixes #39757