Repository navigation
fix(gateway): answer count_tokens for a Bedrock-only model with 501 not_supported - #229
Merged
Merged
Conversation
…ot_supported Anthropic's count_tokens can only go to an Anthropic upstream. When the model is served only by a Bedrock upstream, the gateway answered 400 "no upstream serves model …", which reads as a malformed request and points at a model that works. It now answers 501 with the Anthropic error type `not_supported`, both at model admission and when a Bedrock upstream comes up while choosing upstreams, and nothing is sent to AWS. Claude Code's gateway guidance names exactly this answer: on it the client counts with a `max_tokens: 1` request instead. Bedrock's own CountTokens is not used: on bedrock-runtime it only counts some older Claude models. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Merged
fylorn
added a commit
that referenced
this pull request
Sep 29, 2026
Minor release for #223, #224, #226, #227, #228, #229, #230 and #231, and #222 before them. - Bump the workspace version to 0.55.0 - release-notes/0.55.0.md Control-plane protocol 28 (was 27): the protocol `bedrock`, ProviderInput.aws / ProviderView.aws (AwsKeys), ProviderView.region, ProviderPreview.region and AttemptView.model. The request store schema is unchanged (22), so the request history is kept. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
/v1/messages/count_tokenscan only be served by an Anthropic-protocol upstream: it has no counterpart in the other formats, and Converse has none. When the model is served only by a Bedrock upstream (the usual case for Claude Code routed to Bedrock), the gateway answered 400No upstream serves model …. Clients read that as a malformed request, and the message points at a model that works fine for/v1/messages.Change
The gateway now answers 501 with the Anthropic error type
not_supported, and sends nothing to AWS:gw.count_tokens.bedrock_model)gw.count_tokens.bedrock_upstream); a later Anthropic upstream in the chain can still serve itClaude Code's own guidance for gateways in front of Bedrock says exactly this: return
501 not_supported; the client falls back to a Haikumax_tokens:1probe. Its native Bedrock path does the same fallback when counting fails.Details:
Source::NotSupported→ 501; it maps torequestinx-thinkwatch-errorandRequestFailed.source, so the event vocabulary does not change.not_supported(tw-dialect).Why not Bedrock's CountTokens: on
bedrock-runtimeit only counts some Claude models. AWS documents that models launched as cross-Region-inference-only are not counted there and must use thebedrock-mantleendpoint instead, which has its own IAM action (bedrock-mantle:CountTokens) scoped to a Bedrock project. Newer models would fail either way, so the documented fallback is the dependable answer.Verification
cargo fmt --all -- --check,cargo clippy --workspace --all-targets -- -D warnings, andcargo test --workspace(98 test binaries) pass locally, rebased on feat(pricing): price what was sent, borrow Bedrock prices, count cache toward long context #227.tests/bedrock.rs): count_tokens gets 501not_supportedbefore the model list is fetched (per-hop path) and after (admission path), and only the catalog's GETs reach the fake.ClientApi::counts_tokensrecognises only Anthropic's path.msg-codes.txtregenerated. The two new codes need Chinese sentences in Lite'score.zh.json.🤖 Generated with Claude Code