Skip to content

Conversation

@orolega
Copy link
Contributor

@orolega orolega commented Nov 7, 2025

Title

Fix token counting for Anthropic tool interactions

Relevant issues

Fixes #14236

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem
image

Type

🐛 Bug Fix
✅ Test

Changes

Adds support for counting tokens in Anthropic's tool_use and tool_result
content blocks. Previously, these content types caused "Invalid content
type" errors in the router's pre-call checks, preventing proper context
window filtering for Claude Code requests with tool interactions.

  • Add tool_use content type handler in _count_content_list() to count tokens from tool name and input parameters
  • Add tool_result content type handler supporting both string and nested list content formats
  • Add comprehensive test coverage for tool_use, tool_result, and combined tool conversation scenarios
  • Fix router pre-call checks to properly filter deployments by context window when tool interactions are present

@vercel
Copy link

vercel bot commented Nov 7, 2025

@orolega is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

raise ValueError(
f"Invalid image_url type: {type(c['image_url'])}. Expected str or dict."
)
elif c["type"] == "tool_use":
Copy link
Contributor

Choose a reason for hiding this comment

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

can we have this in a separate helper function, and ideally use a typed dict, so we know what to expect here? we might already have one for this in types/llms/anthropic.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Token counting API fails with "Invalid content type" error when processing Claude Code sub-agent conversations

2 participants