Skip to content

fix(count_tokens): backport Bedrock Mantle count_tokens fallback to stable/1.103.x - #47743

Merged
yuneng-berri merged 3 commits into
stable/1.103.xfrom
litellm_backport_45317_46430_stable_1_103_x
Oct 21, 2026
Merged

yuneng-berri merged 3 commits into
stable/1.103.xfrom
litellm_backport_45317_46430_stable_1_103_x

Conversation

@mateo-berri

Copy link
Copy Markdown
Contributor

Summary

Backport of #45317 and the Bedrock part of #46430 onto stable/1.103.x. Bedrock Runtime CountTokens rejects newer Claude models with a 400 or 404, so /v1/messages/count_tokens fell back to a local estimate that undercounts. Runtime is still tried first, then Claude models fall back to the Bedrock Mantle Anthropic count_tokens endpoint, and non-Claude models never call Mantle. Same change stable/1.104.x got in #47634

Picks

This is a mechanical pick. Tests, Black, the grype sweep, /gauntlet-mini, the live leg and the version bump were skipped, so the 1.103 patch bump is still owed before a release

Linear ticket

Resolves LIT-9563

p0-regression: LIT-9563

Link to Devin session: https://app.devin.ai/sessions/7663d25bd05a4d06b8871043e834b3a0
Open in Devin Desktop: https://app.devin.ai/desktop/session/7663d25bd05a4d06b8871043e834b3a0?variant=devin
Requested by: @mateo-berri

devin-ai-integration Bot and others added 3 commits October 21, 2026 01:50
…not count a Claude model (#45317)

(cherry picked from commit 577d74c)
…#46430)

(cherry picked from commit 08a24d3)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Minimal prerequisite from #43646 for stable/1.104.x.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 6a80137)
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
2 out of 3 committers have signed the CLA.

✅ mrinal-berri
✅ mateo-berri
❌ devin-ai-integration[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@mateo-berri

Copy link
Copy Markdown
Contributor Author

@litereview full

@litereview

litereview Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

⚠️ full

Scope: full · Full PR review.

Completed · Checks failed.

Verdict

The product backport works on this line. The tests it brings along do not: every new test file targets APIs or helper modules that only exist on newer branches, so all 11 new unit tests fail, and the e2e and integration files fail at collection. As a result, none of the new Mantle fallback behavior is tested on stable/1.103.x.

What I ran

  • Mantle fallback behavior. I ran an ad-hoc script that drove BedrockTokenCounter.count_tokens through an httpx.MockTransport behind a real AsyncHTTPHandler, with SigV4 signing.

    • global.anthropic.claude-opus-4-8: bedrock-runtime returned 400, the counter retried on bedrock-mantle.us-east-1.api.aws with base model anthropic.claude-opus-4-8, and it returned total_tokens=42 with tokenizer_type=bedrock_mantle_api.
    • amazon.nova-pro-v1:0: the same 400 from runtime never reached Mantle. The counter returned error=True with status 400.

    This matches what the PR describes.

  • Imports. Everything the product code imports exists on this line: run_aws_signing, build_mantle_messages_url, and AnthropicCountTokensConfig.transform_request_to_count_tokens, which takes the tools/system arguments the new code passes.

  • Google countTokens change. When the original response has no totalTokens, the endpoint now falls back to the normalized total. When the original response does have totalTokens, nothing changes.

  • Log level. The handler.py HTTP-error log drops from error to debug. That is the only change there.

Not covered

  • The live base-vs-head A/B against real Bedrock was not run. There were no AWS credentials in this sandbox.
  • No merge-ref check was possible, because the checkout has no git history.

The PR body itself says tests and the live leg were skipped.

Checks and execution details
  • Failed · Unit tests for changed Bedrock count_tokens and Google endpoint (optional): pytest tests/test_litellm/llms/bedrock/count_tokens tests/test_litellm/proxy/google_endpoints tests/proxy_unit_tests/test_proxy_token_counter.py: 54 passed, 2 skipped, 11 failed. All 11 failures are the transport= TypeError in the new tests.
  • Passed · Ad-hoc behavioral check of Mantle fallback (optional): Used httpx.MockTransport behind a real AsyncHTTPHandler with SigV4 signing. For the Claude model, a runtime 400 led to a Mantle retry that returned 42 tokens, and the request body used the base model id. For Nova, a runtime 400 was returned as an error and Mantle was never called.
  • Failed · Collection of new e2e/integration tests (optional): Both files fail at collection with ModuleNotFoundError (e2e_metadata, integration._support.bedrock_runtime_peer).
  • Not run · Live base-vs-head proxy A/B with real Bedrock (optional): There were no AWS credentials in this sandbox, and the checkout has no git history to build a base worktree from.

View review · Reviewed 696e1c3c87ca



def _counter(bedrock: _Bedrock) -> BedrockTokenCounter:
return BedrockTokenCounter(client=AsyncHTTPHandler(transport=httpx.MockTransport(bedrock)))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium · New backported tests fail on stable/1.103.x: AsyncHTTPHandler(transport=...) and missing e2e/integration helper modules

All three groups of new tests were written against helpers that do not exist on this line. Two of the groups fail before any test runs.

  • Unit tests. Both new unit-test files build the client with AsyncHTTPHandler(transport=httpx.MockTransport(...)): test_bedrock_token_counter.py:41 and test_bedrock_mantle_count_tokens_handler.py:42. On this line, AsyncHTTPHandler.__init__ (litellm/llms/custom_httpx/http_handler.py:604) has no transport parameter, so each test fails with TypeError: AsyncHTTPHandler.__init__() got an unexpected keyword argument 'transport'.
  • Integration test. tests/integration/providers/test_bedrock_mantle_count_tokens_wire.py:22 imports integration._support.bedrock_runtime_peer. That module is not in tests/integration/_support/, so collection fails with ModuleNotFoundError.
  • E2E test. tests/e2e/llm_translation/test_messages_count_tokens_bedrock_e2e.py:19 imports e2e_metadata. No such module exists under tests/e2e/, so collection fails with ModuleNotFoundError: No module named 'e2e_metadata'.

What happens if this merges: the tests/test_litellm job goes red with 11 failures. Every test that exercises the new Mantle fallback (the runtime-400/404 retry, the non-Claude no-retry rule, the system/tools pass-through on Mantle, and Mantle error status reporting) never actually runs. A later regression in BedrockTokenCounter or BedrockMantleCountTokensHandler on this stable line would go uncaught.

These files were taken as-is from the stable/1.104.x and main picks. To fix them, either adapt them to this line's test harness (for example, build an httpx.AsyncClient(transport=...) and inject it into the handler), or pull in the missing helpers.

Evidence

.venv/bin/python -m pytest tests/test_litellm/llms/bedrock/count_tokens → 11 failed, all with TypeError: AsyncHTTPHandler.__init__() got an unexpected keyword argument 'transport'

pytest --collect-only tests/e2e/llm_translation/test_messages_count_tokens_bedrock_e2e.py → ModuleNotFoundError: No module named 'e2e_metadata'

cd tests && pytest --collect-only integration/providers/test_bedrock_mantle_count_tokens_wire.py → collection error. tests/integration/_support/ contains no bedrock_runtime_peer.py

Latest summary by full · View this review · Reviewed 696e1c3c87ca

@yuneng-berri
yuneng-berri merged commit 3902cba into stable/1.103.x Oct 21, 2026
5 of 6 checks passed
@yuneng-berri
yuneng-berri deleted the litellm_backport_45317_46430_stable_1_103_x branch October 21, 2026 07:54
yuneng-berri added a commit that referenced this pull request Oct 21, 2026
…translation_cost_stable_1_103_x

# Conflicts:
#	litellm/model_prices_and_context_window_backup.json
#	litellm/proxy/hooks/proxy_track_cost_callback.py
#	litellm/responses/litellm_completion_transformation/streaming_iterator.py
#	model_prices_and_context_window.json
#	tests/test_litellm/proxy/hooks/test_proxy_track_cost_callback.py
#	tests/test_litellm/test_utils.py
#	uv.lock

This branch was successfully deployed

No deployments
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.

4 participants