Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

refactor(utils)!: delete prompt_token_calculator (#38132)

ryan-crabbe-berri380338c0b4dd
diff --git a/litellm/utils.py b/litellm/utils.py
index 5b2ef93edb3..7fed0bab935 100644
--- a/litellm/utils.py
+++ b/litellm/utils.py
@@ -6528,13 +6528,6 @@ def acreate(*args, **kwargs): ## Thin client to handle the acreate langchain ca
return litellm.acompletion(*args, **kwargs)
-def prompt_token_calculator(model, messages):
- text: Final = " ".join(message["content"] for message in messages)
- if "claude" in model:
- return token_counter(model=model, text=text)
- return len(_get_default_encoding().encode(text))
-
-
def valid_model(model):
try:
# for a given model name, check if the user has the right permissions to access the model
diff --git a/ruff-strict-budget.json b/ruff-strict-budget.json
index 3b3c3e8ae5b..03318718fb5 100644
--- a/ruff-strict-budget.json
+++ b/ruff-strict-budget.json
@@ -1,6 +1,6 @@
{
"ANN001": {
- "limit": 3020
+ "limit": 3018
},
"ANN002": {
"limit": 71
@@ -9,7 +9,7 @@
"limit": 827
},
"ANN201": {
- "limit": 2017
+ "limit": 2016
},
"ANN202": {
"limit": 852
diff --git a/tests/test_litellm/test_utils.py b/tests/test_litellm/test_utils.py
index 27cf9067914..77dca9322c6 100644
--- a/tests/test_litellm/test_utils.py
+++ b/tests/test_litellm/test_utils.py
@@ -1,7 +1,6 @@
import json
import logging
import os
-import sys
from typing import Final
from unittest.mock import AsyncMock, MagicMock, patch
@@ -42,7 +41,6 @@ from litellm.utils import (
get_prompt_cache_min_tokens,
is_cached_message,
is_prompt_caching_valid_prompt,
- prompt_token_calculator,
)
# Adds the parent directory to the system path
@@ -4979,19 +4977,3 @@ def test_completion_does_not_leak_rust_flag_into_provider_request_body():
create_kwargs = mock_client.chat.completions.with_raw_response.create.call_args.kwargs
assert "rust" not in create_kwargs
assert "rust" not in (create_kwargs.get("extra_body") or {})
-
-
-def test_prompt_token_calculator_counts_claude_without_the_anthropic_sdk():
- """
- The claude branch used to call the anthropic SDK's `count_tokens`, which the SDK
- removed, so every claude call raised AttributeError. Counting must work with
- `anthropic` unimportable.
- """
- messages: Final = [{"role": "user", "content": "the quick brown fox jumps over the lazy dog"}]
-
- with patch.dict(sys.modules, {"anthropic": None}):
- claude_tokens = prompt_token_calculator("claude-sonnet-4-5", messages)
- gpt_tokens = prompt_token_calculator("gpt-4o", messages)
-
- assert claude_tokens == 9
- assert gpt_tokens == 9