Skip to content

fix(anthropic): strip server-side tools from count_tokens requests (#5702)#5705

Closed
MohammadYusif wants to merge 1 commit into
pydantic:mainfrom
MohammadYusif:fix/issue-5702
Closed

fix(anthropic): strip server-side tools from count_tokens requests (#5702)#5705
MohammadYusif wants to merge 1 commit into
pydantic:mainfrom
MohammadYusif:fix/issue-5702

Conversation

@MohammadYusif
Copy link
Copy Markdown

The Anthropic count_tokens endpoint rejects server-side tools such as
CodeExecutionTool, WebSearchTool, and WebFetchTool with a 400 error
("Server tools are not supported in the count_tokens endpoint").

_messages_count_tokens was forwarding the full tool list produced by
_add_native_tools — including those server tools — unchanged to the
endpoint. The fix filters out any tool whose type field starts with a
server-tool prefix before the call, preserving all function/memory/
tool-search/MCP tools that the endpoint does accept.

Adds a unit test that verifies each server-side native tool is excluded
from the count_tokens payload.

Checklist

  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it.
  • No breaking changes in accordance with the version policy.
  • PR title is fit for the release changelog.

…ydantic#5702)

The Anthropic count_tokens endpoint rejects server-side tools such as
CodeExecutionTool, WebSearchTool, and WebFetchTool with a 400 error
("Server tools are not supported in the count_tokens endpoint").

`_messages_count_tokens` was forwarding the full tool list produced by
`_add_native_tools` — including those server tools — unchanged to the
endpoint. The fix filters out any tool whose `type` field starts with a
server-tool prefix before the call, preserving all function/memory/
tool-search/MCP tools that the endpoint does accept.

Adds a unit test that verifies each server-side native tool is excluded
from the count_tokens payload.
@github-actions github-actions Bot added the size: S Small PR (≤100 weighted lines) label May 28, 2026
@github-actions github-actions Bot closed this May 28, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Thanks for your interest in this issue! However, there are already open PRs addressing issue #5702: #5704, #5703.

To avoid duplicate efforts, this PR has been closed. If you'd like to contribute, you can review the existing PRs or share your thoughts on issue #5702.

If you believe the existing PRs are inactive, please comment on the issue and a maintainer can reassess.

@MohammadYusif MohammadYusif deleted the fix/issue-5702 branch May 28, 2026 16:14
@github-actions github-actions Bot added the bug Report that something isn't working, or PR implementing a fix label May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Report that something isn't working, or PR implementing a fix size: S Small PR (≤100 weighted lines)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Anthropic count_tokens fails when native CodeExecutionTool is enabled

1 participant