Skip to content

[Feat] Supports Anthropic Messages count_tokens API - #35588

Merged
DarkLight1337 merged 11 commits into
vllm-project:mainfrom
chaunceyjiang:claude_count_tokens
Mar 2, 2026
Merged

[Feat] Supports Anthropic Messages count_tokens API#35588
DarkLight1337 merged 11 commits into
vllm-project:mainfrom
chaunceyjiang:claude_count_tokens

Conversation

@chaunceyjiang

@chaunceyjiang chaunceyjiang commented Feb 28, 2026

Copy link
Copy Markdown
Collaborator

Purpose

FIX #29915

Test Plan

curl http://localhost:8000/v1/messages/count_tokens\?beta\=true \
    -H 'Content-Type: application/json' \
    -H 'anthropic-version: 2023-06-01' \
    -H "X-Api-Key: $ANTHROPIC_API_KEY" \
    -d '{
          "messages": [
            {
              "content": "string",
              "role": "user"
            }
          ],
          "model": "my-model"
        }'

Test Result

{"input_tokens":11,"context_management":{"original_input_tokens":11}}


Essential Elements of an Effective PR Description Checklist
  • The purpose of the PR, such as "Fix some issue (link existing issues this PR will resolve)".
  • The test plan, such as providing test command.
  • The test results, such as pasting the results comparison before and after, or e2e results
  • (Optional) The necessary documentation update, such as updating supported_models.md and examples for a new model.
  • (Optional) Release notes update. If your change is user facing, please update the release notes draft in the Google Doc.

@mergify mergify Bot added the frontend label Feb 28, 2026

@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 introduces a count_tokens endpoint for the Anthropic Messages API. The implementation is a good start, but I've identified a critical issue that would cause a runtime error due to a type mismatch when converting the request. Additionally, there are opportunities to improve code quality in the new API router by addressing code duplication and an inconsistent method call, which I've flagged as high-severity concerns.

Comment thread vllm/entrypoints/anthropic/serving.py
Comment thread vllm/entrypoints/anthropic/api_router.py Outdated
Comment thread vllm/entrypoints/anthropic/api_router.py Outdated
@chaunceyjiang
chaunceyjiang marked this pull request as ready for review February 28, 2026 05:46
@chaunceyjiang chaunceyjiang changed the title [Feat] add count_tokens endpoint for Anthropic Messages API [Feat] Supports Anthropic Messages count_tokens API Feb 28, 2026
Comment thread vllm/entrypoints/anthropic/protocol.py Outdated
@mergify

mergify Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Hi @chaunceyjiang, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

1 similar comment
@mergify

mergify Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Hi @chaunceyjiang, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

@chaunceyjiang
chaunceyjiang force-pushed the claude_count_tokens branch 3 times, most recently from b7294d1 to b72e9c1 Compare February 28, 2026 09:22
@mergify

mergify Bot commented Feb 28, 2026

Copy link
Copy Markdown
Contributor

Hi @chaunceyjiang, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

@chaunceyjiang

Copy link
Copy Markdown
Collaborator Author

Test:

image image

@chaunceyjiang

Copy link
Copy Markdown
Collaborator Author

/cc @DarkLight1337 PTAL.

@DarkLight1337
DarkLight1337 enabled auto-merge (squash) February 28, 2026 12:35
@github-actions github-actions Bot added the ready ONLY add when PR is ready to merge/full CI is needed label Feb 28, 2026
@mergify

mergify Bot commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be
merged. Please rebase the PR, @chaunceyjiang.

https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/syncing-a-fork

@mergify mergify Bot added the needs-rebase label Mar 1, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
@mergify mergify Bot removed the needs-rebase label Mar 2, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
@mergify

mergify Bot commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Hi @chaunceyjiang, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
@mergify

mergify Bot commented Mar 2, 2026

Copy link
Copy Markdown
Contributor

Hi @chaunceyjiang, the pre-commit checks have failed. Please run:

uv pip install pre-commit
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy or markdownlint failing?
mypy and markdownlint are run differently in CI. If the failure is related to either of these checks, please use the following commands to run them locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10
# For markdownlint
pre-commit run --hook-stage manual markdownlint

Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
@DarkLight1337
DarkLight1337 merged commit 9a87b05 into vllm-project:main Mar 2, 2026
50 checks passed
@chaunceyjiang
chaunceyjiang deleted the claude_count_tokens branch March 2, 2026 09:51
Copilot AI pushed a commit to machov/vllm that referenced this pull request Mar 10, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
wendyliu235 pushed a commit to wendyliu235/vllm-public that referenced this pull request Mar 18, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
mystous pushed a commit to mystous/vllm_hybrid that referenced this pull request May 10, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
my-other-github-account pushed a commit to my-other-github-account/vllm that referenced this pull request May 15, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
0826joyce pushed a commit to 0826joyce/vllm-serving-optimization that referenced this pull request May 19, 2026
Signed-off-by: chaunceyjiang <chaunceyjiang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

frontend ready ONLY add when PR is ready to merge/full CI is needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: include reasoning tokens in /v1/messages Anthropic endpoint if model supports it

2 participants