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]

fix(main): use local tiktoken cache in lazy loading (#19774)

Cesar Garcia807ba011ebd1
diff --git a/litellm/main.py b/litellm/main.py
index 5b8c569a390..319a59771f4 100644
--- a/litellm/main.py
+++ b/litellm/main.py
@@ -7300,8 +7300,11 @@ def _get_encoding():
def __getattr__(name: str) -> Any:
"""Lazy import handler for main module"""
if name == "encoding":
- # Lazy load encoding to avoid heavy tiktoken import at module load time
- _encoding = tiktoken.get_encoding("cl100k_base")
+ # Use _get_default_encoding which properly sets TIKTOKEN_CACHE_DIR
+ # before loading tiktoken, ensuring the local cache is used
+ # instead of downloading from the internet
+ from litellm._lazy_imports import _get_default_encoding
+ _encoding = _get_default_encoding()
# Cache it in the module's __dict__ for subsequent accesses
import sys
diff --git a/tests/test_litellm/test_eager_tiktoken_load.py b/tests/test_litellm/test_eager_tiktoken_load.py
index 1264c68b99e..33dd57fad8d 100644
--- a/tests/test_litellm/test_eager_tiktoken_load.py
+++ b/tests/test_litellm/test_eager_tiktoken_load.py
@@ -78,6 +78,35 @@ def test_lazy_loading_default():
assert len(tokens) > 0, "Encoding should work"
+def test_tiktoken_cache_dir_set_on_lazy_load():
+ """Test that TIKTOKEN_CACHE_DIR is set when encoding is lazy loaded.
+
+ This ensures the local tiktoken cache is used instead of downloading
+ from the internet. Regression test for issue #19768.
+ """
+ # Remove environment variables to ensure clean state
+ if "LITELLM_DISABLE_LAZY_LOADING" in os.environ:
+ del os.environ["LITELLM_DISABLE_LAZY_LOADING"]
+ if "TIKTOKEN_CACHE_DIR" in os.environ:
+ del os.environ["TIKTOKEN_CACHE_DIR"]
+
+ # Clear any cached modules
+ modules_to_clear = [k for k in sys.modules.keys() if k.startswith("litellm")]
+ for module in modules_to_clear:
+ del sys.modules[module]
+
+ # Import litellm fresh
+ import litellm
+
+ # Access encoding (triggers lazy load)
+ _ = litellm.encoding
+
+ # Verify TIKTOKEN_CACHE_DIR is now set and points to local tokenizers
+ assert "TIKTOKEN_CACHE_DIR" in os.environ, "TIKTOKEN_CACHE_DIR should be set after lazy loading encoding"
+ cache_dir = os.environ["TIKTOKEN_CACHE_DIR"]
+ assert "tokenizers" in cache_dir, f"TIKTOKEN_CACHE_DIR should point to tokenizers directory, got: {cache_dir}"
+
+
@pytest.fixture(autouse=True)
def cleanup_env():
"""Clean up environment variable after each test"""