Skip to content

fix(gateway): answer count_tokens for a Bedrock-only model with 501 not_supported - #229

Merged
fylorn merged 1 commit into
mainfrom
fix/count-tokens-on-bedrock
Sep 28, 2026
Merged

fylorn merged 1 commit into
mainfrom
fix/count-tokens-on-bedrock

Conversation

@fylorn

@fylorn fylorn commented Sep 28, 2026

Copy link
Copy Markdown
Contributor

Problem

/v1/messages/count_tokens can 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 400 No 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:

  • at model admission, when the model is admitted for Bedrock but not for Anthropic (gw.count_tokens.bedrock_model)
  • while choosing upstreams, when a Bedrock upstream comes up for count_tokens, e.g. without a model list (gw.count_tokens.bedrock_upstream); a later Anthropic upstream in the chain can still serve it

Claude Code's own guidance for gateways in front of Bedrock says exactly this: return 501 not_supported; the client falls back to a Haiku max_tokens:1 probe. Its native Bedrock path does the same fallback when counting fails.

Details:

  • Source::NotSupported → 501; it maps to request in x-thinkwatch-error and RequestFailed.source, so the event vocabulary does not change.
  • The Anthropic error body maps 501 to not_supported (tw-dialect).

Why not Bedrock's CountTokens: on bedrock-runtime it only counts some Claude models. AWS documents that models launched as cross-Region-inference-only are not counted there and must use the bedrock-mantle endpoint 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, and cargo test --workspace (98 test binaries) pass locally, rebased on feat(pricing): price what was sent, borrow Bedrock prices, count cache toward long context #227.
  • New tests:
    • End to end against a fake Bedrock (tests/bedrock.rs): count_tokens gets 501 not_supported before the model list is fetched (per-hop path) and after (admission path), and only the catalog's GETs reach the fake.
    • ClientApi::counts_tokens recognises only Anthropic's path.
    • The Anthropic error body for 501.
  • msg-codes.txt regenerated. The two new codes need Chinese sentences in Lite's core.zh.json.

🤖 Generated with Claude Code

…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>
@fylorn
fylorn merged commit c1843e2 into main Sep 28, 2026
4 checks passed
@fylorn
fylorn deleted the fix/count-tokens-on-bedrock branch September 28, 2026 17:44
@fylorn fylorn mentioned this pull request Sep 28, 2026
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>
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.

1 participant