Skip to content

fix(langchain): recognize ChatAnthropicVertex in _get_approximate_token_counter#36320

Merged
ccurme (ccurme) merged 1 commit intolangchain-ai:masterfrom
Jordanh1996:fix/anthropic-vertex-token-counter
Mar 27, 2026
Merged

fix(langchain): recognize ChatAnthropicVertex in _get_approximate_token_counter#36320
ccurme (ccurme) merged 1 commit intolangchain-ai:masterfrom
Jordanh1996:fix/anthropic-vertex-token-counter

Conversation

@Jordanh1996
Copy link
Copy Markdown
Contributor

Summary

_get_approximate_token_counter uses model._llm_type == "anthropic-chat" to detect Anthropic models and apply chars_per_token=3.3. But ChatAnthropicVertex (Claude via Vertex AI) returns _llm_type = "anthropic-chat-vertexai", so the check fails and the default 4.0 chars/token is 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 with prompt is too long.

Change

== "anthropic-chat".startswith("anthropic-chat")

This covers both ChatAnthropic ("anthropic-chat") and ChatAnthropicVertex ("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).

…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
@github-actions github-actions bot added fix For PRs that implement a fix langchain `langchain` package issues & PRs size: XS < 50 LOC labels Mar 27, 2026
@github-actions
Copy link
Copy Markdown

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:

  1. Comment on the linked issue to request assignment from a maintainer
  2. Once assigned, edit your PR description and the PR will be reopened automatically

Maintainers: reopen this PR or remove the missing-issue-link label to bypass this check.

@ccurme ccurme (ccurme) marked this pull request as ready for review March 27, 2026 22:18
@ccurme ccurme (ccurme) merged commit 475408f into langchain-ai:master Mar 27, 2026
66 of 98 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-issue-check external fix For PRs that implement a fix langchain `langchain` package issues & PRs new-contributor size: XS < 50 LOC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

_get_approximate_token_counter doesn't recognize ChatAnthropicVertex, causing SummarizationMiddleware to never trigger

2 participants