Skip to content

feat: add /tokenize enpoint for GCP and AWS Anthropic backends for chat completion schema#2390

Merged
aabchoo merged 6 commits into
envoyproxy:mainfrom
hustxiayang:tokenize-anthropic
Jul 21, 2026
Merged

feat: add /tokenize enpoint for GCP and AWS Anthropic backends for chat completion schema#2390
aabchoo merged 6 commits into
envoyproxy:mainfrom
hustxiayang:tokenize-anthropic

Conversation

@hustxiayang

@hustxiayang hustxiayang commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Description
Adds /tokenize support for gcp and aws anthropic schema for chat completion schema, which is also split from #1650 as planned.

Provider-specific model name handling:

The modelNameOverride configured on a backend is shared across all API types (messages, chat, count_tokens). However, the count_tokens endpoints on GCP and AWS have stricter model name requirements than their inference counterparts:

  • GCP Vertex AI: The count-tokens endpoint does not accept the @default or @latest version aliases (e.g., claude-sonnet-4-6@default returns "not supported for token counting"). Explicit version tags like @20250514 are accepted, as are bare model names. Since @default is required for inference endpoints that share the same backend config, the count_tokens translator strips @default and @latest suffixes 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-6 returns "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.

  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>
@hustxiayang
hustxiayang requested a review from a team as a code owner July 17, 2026 20:28
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Jul 17, 2026
@codecov-commenter

codecov-commenter commented Jul 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.46970% with 41 lines in your changes missing coverage. Please review.
✅ Project coverage is 84.83%. Comparing base (934174a) to head (1e4f0b6).

Files with missing lines Patch % Lines
internal/translator/tokenize_awsanthropic.go 81.13% 10 Missing and 10 partials ⚠️
internal/translator/tokenize_gcpanthropic.go 85.71% 7 Missing and 7 partials ⚠️
internal/translator/anthropic_helper.go 87.50% 3 Missing and 4 partials ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Signed-off-by: yxia216 <yxia216@bloomberg.net>

@aabchoo aabchoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In the future, could you please split your PRs for each backend? It would be a lot easier to review individually.

Comment thread internal/translator/tokenize_awsanthropic.go
Comment thread internal/translator/tokenize_gcpanthropic.go Outdated
Signed-off-by: yxia216 <yxia216@bloomberg.net>
@missBerg missBerg added enhancement New feature or request area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) labels Jul 21, 2026
@aabchoo

aabchoo commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

@gemini review

@aabchoo

aabchoo commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

/retest

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread internal/translator/tokenize_gcpanthropic.go Outdated

@aabchoo aabchoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

overall looks good to me!

@hustxiayang

Copy link
Copy Markdown
Contributor Author

"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.

@hustxiayang

Copy link
Copy Markdown
Contributor Author

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread internal/translator/tokenize_awsanthropic.go
Comment thread internal/translator/tokenize_gcpanthropic.go
Comment thread internal/translator/anthropic_helper.go
@aabchoo

aabchoo commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

"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.

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>
@aabchoo
aabchoo enabled auto-merge (squash) July 21, 2026 17:02
@aabchoo
aabchoo merged commit 63db0d9 into envoyproxy:main Jul 21, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/translation Provider/endpoint coverage and schema translation (incl. fidelity bugs) enhancement New feature or request size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants