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: lazy load get_modified_max_tokens (#18002)

Alexsander Hamir764a31f62455
diff --git a/litellm/__init__.py b/litellm/__init__.py
index 67fd8a7b075..80625e0b189 100644
--- a/litellm/__init__.py
+++ b/litellm/__init__.py
@@ -1065,7 +1065,6 @@ openai_video_generation_models = ["sora-2"]
from .timeout import timeout
from litellm.litellm_core_utils.get_llm_provider_logic import get_llm_provider
from litellm.litellm_core_utils.core_helpers import remove_index_from_tool_calls
-from litellm.litellm_core_utils.token_counter import get_modified_max_tokens
# client must be imported immediately as it's used as a decorator at function definition time
from .utils import client
# Note: Most other utils imports are lazy-loaded via __getattr__ to avoid loading utils.py
@@ -1567,6 +1566,7 @@ def __getattr__(name: str) -> Any:
COST_CALCULATOR_NAMES,
LITELLM_LOGGING_NAMES,
UTILS_NAMES,
+ TOKEN_COUNTER_NAMES,
CACHING_NAMES,
HTTP_HANDLER_NAMES,
)
@@ -1586,6 +1586,11 @@ def __getattr__(name: str) -> Any:
from ._lazy_imports import _lazy_import_utils
return _lazy_import_utils(name)
+ # Lazy load token counter utilities
+ if name in TOKEN_COUNTER_NAMES:
+ from ._lazy_imports import _lazy_import_token_counter
+ return _lazy_import_token_counter(name)
+
# Lazy load caching classes
if name in CACHING_NAMES:
from ._lazy_imports import _lazy_import_caching
diff --git a/litellm/_lazy_imports.py b/litellm/_lazy_imports.py
index c9655f0d2ff..1fbf3f1be0f 100644
--- a/litellm/_lazy_imports.py
+++ b/litellm/_lazy_imports.py
@@ -34,6 +34,11 @@ UTILS_NAMES = (
"ModelResponseListIterator", "get_valid_models",
)
+# Token counter names that support lazy loading via _lazy_import_token_counter
+TOKEN_COUNTER_NAMES = (
+ "get_modified_max_tokens",
+)
+
# Caching / cache classes that support lazy loading via _lazy_import_caching
CACHING_NAMES = (
"Cache",
@@ -279,6 +284,21 @@ def _lazy_import_cost_calculator(name: str) -> Any:
raise AttributeError(f"Cost calculator lazy import: unknown attribute {name!r}")
+def _lazy_import_token_counter(name: str) -> Any:
+ """Lazy import for token_counter utilities."""
+ _globals = _get_litellm_globals()
+
+ if name == "get_modified_max_tokens":
+ from litellm.litellm_core_utils.token_counter import (
+ get_modified_max_tokens as _get_modified_max_tokens,
+ )
+
+ _globals["get_modified_max_tokens"] = _get_modified_max_tokens
+ return _get_modified_max_tokens
+
+ raise AttributeError(f"Token counter lazy import: unknown attribute {name!r}")
+
+
def _lazy_import_caching(name: str) -> Any:
"""Lazy import for caching module classes."""
_globals = _get_litellm_globals()
diff --git a/tests/test_litellm/test_lazy_imports.py b/tests/test_litellm/test_lazy_imports.py
index c623ecc4b0e..08737df4e4a 100644
--- a/tests/test_litellm/test_lazy_imports.py
+++ b/tests/test_litellm/test_lazy_imports.py
@@ -12,11 +12,13 @@ from litellm._lazy_imports import (
COST_CALCULATOR_NAMES,
LITELLM_LOGGING_NAMES,
UTILS_NAMES,
+ TOKEN_COUNTER_NAMES,
CACHING_NAMES,
HTTP_HANDLER_NAMES,
_lazy_import_cost_calculator,
_lazy_import_litellm_logging,
_lazy_import_utils,
+ _lazy_import_token_counter,
_lazy_import_caching,
_lazy_import_http_handlers,
)
@@ -97,6 +99,18 @@ def test_caching_lazy_imports():
_verify_only_requested_name_imported(name, CACHING_NAMES)
+def test_token_counter_lazy_imports():
+ """Test that token counter utilities can be lazy imported."""
+ for name in TOKEN_COUNTER_NAMES:
+ _clear_names_from_globals(TOKEN_COUNTER_NAMES)
+
+ func = _lazy_import_token_counter(name)
+ assert func is not None
+ assert name in litellm.__dict__
+
+ _verify_only_requested_name_imported(name, TOKEN_COUNTER_NAMES)
+
+
def test_http_handler_lazy_imports():
"""Test that HTTP handler singletons can be lazy imported."""
for name in HTTP_HANDLER_NAMES:
@@ -123,3 +137,6 @@ def test_unknown_attribute_raises_error():
with pytest.raises(AttributeError):
_lazy_import_caching("unknown")
+ with pytest.raises(AttributeError):
+ _lazy_import_token_counter("unknown")
+