feat: add /tokenize enpoint for GCP and AWS Anthropic backends for chat completion schema#2390
Conversation
Adds /tokenize support for Claude on Vertex AI (GCPAnthropic, via the Anthropic MessageCountTokens rawPredict API) and Anthropic on AWS Bedrock (AWSAnthropic, via the Bedrock CountTokens InvokeModel API), sharing the openAIToAnthropicCountTokensParams helper. Registers both schemas in the tokenize endpoint spec. Signed-off-by: yxia216 <yxia216@bloomberg.net>
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #2390 +/- ##
==========================================
- Coverage 84.84% 84.83% -0.02%
==========================================
Files 151 153 +2
Lines 22090 22330 +240
==========================================
+ Hits 18743 18944 +201
- Misses 2215 2234 +19
- Partials 1132 1152 +20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Signed-off-by: yxia216 <yxia216@bloomberg.net>
aabchoo
left a comment
There was a problem hiding this comment.
In the future, could you please split your PRs for each backend? It would be a lot easier to review individually.
Signed-off-by: yxia216 <yxia216@bloomberg.net>
|
@gemini review |
|
/retest |
There was a problem hiding this comment.
Code Review
This pull request adds support for translating OpenAI-compatible tokenize requests to GCP Anthropic (Claude on Vertex AI) and AWS Bedrock (Anthropic) token counting APIs. It introduces the ToGCPAnthropicV1Tokenize and ToAWSAnthropicV1Tokenize translators, updates the endpoint specification and tests, and documents the new capabilities. The feedback suggests a minor improvement in tokenize_gcpanthropic.go to use strings.TrimSuffix instead of manual slicing for stripping model version suffixes, which would make the code cleaner and less error-prone.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
aabchoo
left a comment
There was a problem hiding this comment.
overall looks good to me!
|
"In the future, could you please split your PRs for each backend? It would be a lot easier to review individually." sure, thanks a lot for bring it up! These two are shared because both providers (gcp, bedrock) are using the same underlying claude backend, api schemas (gcpanthropic and awsanthropic) should be identical mostly. |
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds support for translating OpenAI-compatible tokenize requests to GCP Anthropic (Claude on Vertex AI) and AWS Bedrock (Anthropic) token counting APIs. It introduces the respective translators, refactors tool parameter translation helpers, updates the endpoint specification logic, adds comprehensive unit tests, and updates the documentation. The review feedback suggests avoiding in-place mutation of the shared request structure in both translators to prevent side effects, explicitly accessing the prompt field instead of relying on unsafe field promotion, and adding a defensive nil check in the helper function to prevent potential nil pointer dereferences.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
I'd still advocate for splitting it into two. They may share a lot of similarities but they're still two separate features with differences, and, as a reviewer, it's much easier to review and have a good understanding of two large PRs than one XL PR. |
Signed-off-by: yxia216 <yxia216@bloomberg.net>
Description
Adds
/tokenizesupport for gcp and aws anthropic schema for chat completion schema, which is also split from #1650 as planned.Provider-specific model name handling:
The
modelNameOverrideconfigured on a backend is shared across all API types (messages,chat,count_tokens). However, thecount_tokensendpoints on GCP and AWS have stricter model name requirements than their inference counterparts:GCP Vertex AI: The
count-tokensendpoint does not accept the@defaultor@latestversion aliases (e.g.,claude-sonnet-4-6@defaultreturns "not supported for token counting"). Explicit version tags like@20250514are accepted, as are bare model names. Since@defaultis required for inference endpoints that share the same backend config, the count_tokens translator strips@defaultand@latestsuffixes only.AWS Bedrock: The CountTokens API (
POST /model/{modelId}/count-tokens) does not support cross-region inference (CRIS) model IDs (e.g.,us.anthropic.claude-sonnet-4-6returns "The provided model doesn't support counting tokens"). CRIS prefixes are required for inference endpoints (InvokeModel, Converse) that share the same backend config, so the count_tokens translator strips short region prefixes (e.g.,us.,eu.) only.