fix(langchain): recognize ChatAnthropicVertex in _get_approximate_token_counter#36320
Merged
ccurme (ccurme) merged 1 commit intolangchain-ai:masterfrom Mar 27, 2026
Conversation
…en_counter
Use startswith("anthropic-chat") instead of exact match to cover both
ChatAnthropic ("anthropic-chat") and ChatAnthropicVertex
("anthropic-chat-vertexai").
Fixes langchain-ai#36318
|
This PR has been automatically closed because you are not assigned to the linked issue. External contributors must be assigned to an issue before opening a PR for it. Please:
Maintainers: reopen this PR or remove the |
7 tasks
ccurme (ccurme)
approved these changes
Mar 27, 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.
Summary
_get_approximate_token_counterusesmodel._llm_type == "anthropic-chat"to detect Anthropic models and applychars_per_token=3.3. ButChatAnthropicVertex(Claude via Vertex AI) returns_llm_type = "anthropic-chat-vertexai", so the check fails and the default4.0 chars/tokenis used.This underestimates token count by ~16%. When used with
SummarizationMiddleware, the trigger threshold (e.g. 85% of 200K = 170K) is never reached according to the estimate, but the actual prompt is already past 200K. The API rejects it withprompt is too long.Change
== "anthropic-chat"→.startswith("anthropic-chat")This covers both
ChatAnthropic("anthropic-chat") andChatAnthropicVertex("anthropic-chat-vertexai").Reproduction
Full standalone script with API calls: https://gist.github.com/Jordanh1996/af56156da6cfab82917215dd340f76ac
Fixes #36318
This contribution was made with AI assistance (Claude).