-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Implement AWS Bedrock CountTokens API support #14557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
krrishdholakia
merged 4 commits into
BerriAI:main
from
timelfrink:fix/issue-14478-bedrock-count-tokens-endpoint
Sep 18, 2025
Merged
Implement AWS Bedrock CountTokens API support #14557
krrishdholakia
merged 4 commits into
BerriAI:main
from
timelfrink:fix/issue-14478-bedrock-count-tokens-endpoint
Sep 18, 2025
+668
−154
Conversation
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
The latest updates on your projects. Learn more about Vercel for GitHub.
|
@timelfrink have you manually qa'ed this pr? if so, can you please attach a video/screenshot of it working |
- Add support for both Converse and InvokeModel input formats - Implement endpoint handling in pass_through_endpoints - Add transformation logic for AWS Bedrock CountTokens API - Simplify model resolution using existing router patterns - Support token counting for messages and raw text inputs
2470385
to
7eecba6
Compare
- Add endpoint integration test in test_proxy_token_counter.py - Add unit tests for transformation logic in bedrock/count_tokens/ - Test model extraction from request body vs endpoint path - Test input format detection (converse vs invokeModel) - Test request transformation from Anthropic to Bedrock format - All tests follow existing codebase patterns and pass successfully
- Apply Black formatting to all Bedrock CountTokens files - Clean up imports and remove unused variables in tests - Fix indentation and simplify test structure - Fix pyright type error with type ignore annotation - All tests continue to pass after cleanup
- Add explicit type annotation for transformed_message dict - Resolves mypy errors while maintaining functionality - All tests continue to pass
@krrishdholakia updated the PR Just checking, Vercel deployment failed, but I assume this is unrelated? |
Thank you for manually qa'ing this @timelfrink |
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.
Title
Implement AWS Bedrock CountTokens API support
Relevant issues
Fixes #14478
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/
directory, Adding at least 1 test is a hard requirement - see detailsmake test-unit
Type
✨ New Feature
Changes
Problem Description
Issue #14478 reported that the AWS Bedrock
/v1/messages/count_tokens
endpoint was not working properly. The original issue was that the endpoint path parsing was incorrectly extracting "messages" as the model name instead of properly handling the Anthropic-style API format.Complete Solution Implementation
This PR provides a comprehensive implementation of AWS Bedrock CountTokens API support with proper endpoint handling, request transformation, and response formatting.
Key Components
1. Bedrock CountTokens Handler (
litellm/llms/bedrock/count_tokens/handler.py
)2. Request Transformation (
litellm/llms/bedrock/count_tokens/transformation.py
)3. Endpoint Integration (
litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
)/bedrock/v1/messages/count_tokens
endpoint supportFeatures
API Usage
Implementation Details
Request Flow
/bedrock/v1/messages/count_tokens
Model Resolution
Error Handling
Testing
The implementation includes comprehensive testing to ensure reliability and compatibility.
Backward Compatibility
Impact