feat(bedrock): support token counting#1650
Open
aidand-ant wants to merge 2 commits into
Open
Conversation
Amazon Bedrock now supports token counting via the CountTokens API,
so the Bedrock client no longer needs to reject count_tokens() calls.
- Rewrite POST /v1/messages/count_tokens (and the beta variant) to
Bedrock's POST /model/{modelId}/count-tokens endpoint, wrapping the
request body in the CountTokensInput shape as a base64-encoded
invokeModel body
- Map Bedrock's inputTokens response field to input_tokens so callers
get the same MessageTokensCount type as the first-party API
- Expose count_tokens on the Bedrock beta messages resource
mypy rejects wrapping a method assigned from another class with an incompatible self type. Match the vertex pattern, which exposes count_tokens on the resource but not on the raw/streaming response wrappers.
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.
Amazon Bedrock now supports token counting via the CountTokens API, so the SDK no longer needs to reject
count_tokens()calls on the Bedrock client.What changed
_prepare_optionsnow rewritesPOST /v1/messages/count_tokens(and the?beta=truevariant) to Bedrock'sPOST /model/{modelId}/count-tokensendpoint. The Anthropic-format request body is wrapped in Bedrock'sCountTokensInputshape as a base64-encodedinvokeModelbody. The model ID is URL-quoted the same way as forcreate, so inference profile ARNs work.BaseBedrockClientgains a_process_response_dataoverride that maps Bedrock's{"inputTokens": N}response to{"input_tokens": N}, so callers get the sameMessageTokensCounttype as the first-party API.create; it now also exposescount_tokens(including raw/streaming response wrappers).Usage
Tests
Added sync, async, and beta tests covering the URL rewrite, the base64-wrapped request body, and response parsing. All AWS-related tests pass (139 passed).