Skip to content

fix: add missing count_tokens to Bedrock beta.messages#1233

Open
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
MaxwellCalkin:fix/bedrock-beta-count-tokens
Open

fix: add missing count_tokens to Bedrock beta.messages#1233
MaxwellCalkin wants to merge 1 commit intoanthropics:mainfrom
MaxwellCalkin:fix/bedrock-beta-count-tokens

Conversation

@MaxwellCalkin
Copy link

Summary

Calling client.beta.messages.count_tokens() on an AnthropicBedrock client raises AttributeError because the count_tokens method alias is missing from the Bedrock beta Messages / AsyncMessages classes.

This PR:

  1. Adds count_tokens aliases to the Bedrock beta Messages and AsyncMessages classes in _beta_messages.py, matching the pattern already used in the Vertex beta.messages.

  2. Fixes the _prepare_options guard in _client.py to also catch the beta count_tokens URL (/v1/messages/count_tokens?beta=true). Previously, only the non-beta URL (/v1/messages/count_tokens) was blocked, so a beta count_tokens call would bypass the guard and send an unsupported request to the Bedrock API.

After this fix, client.beta.messages.count_tokens() raises the existing clear error message — AnthropicError("Token counting is not supported in Bedrock yet") — instead of an AttributeError.

Before

client = AnthropicBedrock(...)
client.beta.messages.count_tokens(model="...", messages=[...])
# => AttributeError: 'Messages' object has no attribute 'count_tokens'

After

client = AnthropicBedrock(...)
client.beta.messages.count_tokens(model="...", messages=[...])
# => anthropic.AnthropicError: Token counting is not supported in Bedrock yet

Related

Files changed

  • src/anthropic/lib/bedrock/_beta_messages.py — added count_tokens aliases
  • src/anthropic/lib/bedrock/_client.py — extended URL guard to cover beta variant

Add the count_tokens method alias to the Bedrock beta Messages and
AsyncMessages classes, matching the pattern used in Vertex beta.messages.

Also update the _prepare_options guard to catch the beta count_tokens
URL (/v1/messages/count_tokens?beta=true) in addition to the non-beta
URL, so users get a clear "Token counting is not supported in Bedrock
yet" error instead of an unhandled request.

Fixes anthropics#1103
@MaxwellCalkin MaxwellCalkin requested a review from a team as a code owner March 8, 2026 20:57
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.

Token counting is not supported in Bedrock yet

1 participant