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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(router): count deployment itpm reservations off the event loop
mateo-berri83d500bac9c3
diff --git a/litellm/litellm_core_utils/token_counter.py b/litellm/litellm_core_utils/token_counter.pyindex e3a0d4c785a..e3af64fd3bf 100644--- a/litellm/litellm_core_utils/token_counter.py+++ b/litellm/litellm_core_utils/token_counter.py@@ -315,12 +315,12 @@ def calculate_img_tokens(TokenCounterFunction = Callable[[str], int]--EXTRAPOLATION_SAMPLES: Final = 16"""Type for a function that counts tokens in a string."""+EXTRAPOLATION_SAMPLES: Final = 16+def _get_tiktoken_count_function(encode_length: Callable[[str], int],diff --git a/litellm/router_utils/pre_call_checks/io_token_rate_limit_check.py b/litellm/router_utils/pre_call_checks/io_token_rate_limit_check.pyindex 01d42627001..dca5a2be845 100644--- a/litellm/router_utils/pre_call_checks/io_token_rate_limit_check.py+++ b/litellm/router_utils/pre_call_checks/io_token_rate_limit_check.py@@ -21,6 +21,7 @@ import litellmfrom litellm import token_counterfrom litellm._logging import verbose_router_loggerfrom litellm.caching.dual_cache import DualCache+from litellm.litellm_core_utils.asyncify import asyncifyfrom litellm.types.router import RouterCacheEnum, RouterErrorsfrom litellm.utils import get_utc_datetime@@ -466,7 +467,7 @@ async def async_io_token_pre_call_check(request_kwargs: Final = get_io_token_rate_limit_request_kwargs()_model: Final = (deployment.get("litellm_params") or {}).get("model") or ""- estimated_input: Final = _estimate_input_tokens(request_kwargs, model=_model)+ estimated_input: Final = await asyncify(_estimate_input_tokens)(request_kwargs, model=_model)max_tokens: Final = _resolve_max_tokens(request_kwargs, deployment)dt: Final = get_utc_datetime()diff --git a/tests/test_litellm/test_router/test_io_token_rate_limits.py b/tests/test_litellm/test_router/test_io_token_rate_limits.pyindex a5a68271111..3cef1c7bb63 100644--- a/tests/test_litellm/test_router/test_io_token_rate_limits.py+++ b/tests/test_litellm/test_router/test_io_token_rate_limits.py@@ -1039,3 +1039,31 @@ class TestContextSlotRetention:assert deployment is not Nonerouter._update_kwargs_with_deployment(deployment=deployment.model_dump(), kwargs=kwargs)assert get_io_token_rate_limit_request_kwargs() is kwargs+++async def test_the_deployment_itpm_reservation_counts_the_request_off_the_event_loop():+ from litellm.utils import get_utc_datetime+ from tests.large_text import text+ from tests.test_litellm.litellm_core_utils.event_loop_lag import (+ assert_loop_stayed_free,+ timed_with_loop_lags,+ warm_tokenizer,+ )++ dual_cache = DualCache()+ check = ModelRateLimitingCheck(dual_cache=dual_cache)+ warm_tokenizer("anthropic/claude-fable-5")+ deployment = {+ "litellm_params": {"model": "anthropic/claude-fable-5", "itpm": 10_000_000},+ "model_info": {"id": "io-loop-id"},+ "model_name": "claude",+ }+ set_io_token_rate_limit_request_kwargs({"messages": [{"role": "user", "content": text * 100}], "metadata": {}})++ _, took, lags = await timed_with_loop_lags(lambda: check.async_pre_call_check(deployment))++ minute = get_utc_datetime().strftime("%H-%M")+ reserved = await dual_cache.async_get_cache(key=f"global_router:io-loop-id:anthropic/claude-fable-5:itpm:{minute}")+ assert reserved > 100_000+ assert_loop_stayed_free(took, lags)