Skip to content

Conversation

@AnasAbdelR
Copy link
Contributor

Relevant issues

Fixes token counting inaccuracies for deployments using os.environ/ prefix syntax in API key configuration.

Pre-Submission checklist

  • I have added testing in the tests/ directory
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

Bug Fix

Changes

This PR fixes three bugs in count_tokens_with_anthropic_api that cause inaccurate token counting for Anthropic models:

  1. os.environ/ prefix not resolved: When deployment config uses api_key: os.environ/ANTHROPIC_API_KEY, the literal string was passed to the Anthropic SDK, causing authentication failure and silent fallback to tiktoken.

  2. system prompt ignored: The system prompt was not passed to the Anthropic count_tokens API.

  3. tools ignored: Tool definitions were not passed to the Anthropic count_tokens API.

Files changed

  • litellm/proxy/utils.py: Added os.environ/ resolution via get_secret_str, added system and tools parameters
  • litellm/proxy/_types.py: Added system and tools fields to TokenCountRequest
  • litellm/proxy/anthropic_endpoints/endpoints.py: Extract system/tools from request
  • litellm/proxy/proxy_server.py: Pass system/tools through to provider counter
  • litellm/llms/anthropic/common_utils.py: Accept system/tools in AnthropicTokenCounter
  • litellm/llms/base_llm/base_utils.py: Updated BaseTokenCounter interface

Tests

Added 3 new tests in tests/proxy_unit_tests/test_proxy_token_counter.py:

  • test_count_tokens_with_anthropic_api_resolves_os_environ_prefix
  • test_count_tokens_with_anthropic_api_passes_system_and_tools
  • test_anthropic_endpoint_extracts_system_and_tools
tests/proxy_unit_tests/test_proxy_token_counter.py::test_anthropic_endpoint_extracts_system_and_tools PASSED
tests/proxy_unit_tests/test_proxy_token_counter.py::test_count_tokens_with_anthropic_api_passes_system_and_tools PASSED
tests/proxy_unit_tests/test_proxy_token_counter.py::test_count_tokens_with_anthropic_api_resolves_os_environ_prefix PASSED
======================== 3 passed in 4.17s =========================

Verified with real conversation (195 messages):

Test Ground Truth Broken Fixed
Messages only 185,049 180,674 (tiktoken) 185,049
Messages + System 185,065 180,674 (tiktoken) 185,065
Messages + System + Tools 185,719 180,674 (tiktoken) 185,719

Fixed version matches direct Anthropic API exactly. Broken version undercounts by 5,045 tokens (2.7%).

…ols in Anthropic API

This PR fixes three bugs in count_tokens_with_anthropic_api that cause
inaccurate token counting for Anthropic models:

1. **os.environ/ prefix not resolved**: When deployment config uses
   `api_key: os.environ/ANTHROPIC_API_KEY`, the literal string was passed
   to the Anthropic SDK, causing auth failure and silent fallback to
   tiktoken (which undercounts by ~2-15%).

2. **system prompt ignored**: The system prompt was not passed to the
   Anthropic count_tokens API, resulting in undercount.

3. **tools ignored**: Tool definitions were not passed to the Anthropic
   count_tokens API, resulting in undercount.

## Changes

- `litellm/proxy/utils.py`: Added os.environ/ resolution via get_secret_str,
  added system and tools parameters to count_tokens_with_anthropic_api
- `litellm/proxy/_types.py`: Added system and tools fields to TokenCountRequest
- `litellm/proxy/anthropic_endpoints/endpoints.py`: Extract system/tools from request
- `litellm/proxy/proxy_server.py`: Pass system/tools through to provider counter
- `litellm/llms/anthropic/common_utils.py`: Accept system/tools in AnthropicTokenCounter
- `litellm/llms/base_llm/base_utils.py`: Updated BaseTokenCounter interface

## Test Results

With a real 195-message conversation:
- Messages only: FIXED matches Direct API EXACTLY (185,049 tokens)
- Messages + System: FIXED matches Direct API EXACTLY (185,065 tokens)
- Messages + System + Tools: FIXED matches Direct API EXACTLY (185,719 tokens)
- BROKEN undercounts by 5,045 tokens (2.7%) due to tiktoken fallback
@vercel
Copy link

vercel bot commented Dec 4, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
litellm Ready Ready Preview Comment Dec 4, 2025 5:14pm

@CLAassistant
Copy link

CLAassistant commented Dec 4, 2025

CLA assistant check
All committers have signed the CLA.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants