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(interactions): stop the cost poll loop instead of spinning on a non-positive interval
mateo-berri6befeb8a17f9
diff --git a/litellm/interactions/background_cost_polling.py b/litellm/interactions/background_cost_polling.pyindex 262d8e3260d..f2957d08840 100644--- a/litellm/interactions/background_cost_polling.py+++ b/litellm/interactions/background_cost_polling.py@@ -74,7 +74,7 @@ async def _fetch_interaction(context: BackgroundInteractionPollContext) -> Interdef _poll_intervals(initial: float, maximum: float, timeout: float) -> Iterator[float]:elapsed = 0.0interval = initial- while elapsed + interval <= timeout:+ while interval > 0 and elapsed + interval <= timeout:yield intervalelapsed += intervalinterval = min(interval * 2, maximum)diff --git a/tests/test_litellm/interactions/test_background_cost_polling.py b/tests/test_litellm/interactions/test_background_cost_polling.pyindex 64436ebe4a7..7908e0e8f17 100644--- a/tests/test_litellm/interactions/test_background_cost_polling.py+++ b/tests/test_litellm/interactions/test_background_cost_polling.py@@ -1,11 +1,13 @@import asyncioimport time+from itertools import islicefrom typing import Optionalimport pytestfrom litellm.interactions.background_cost_polling import (_SETTLED_KEY,+ _poll_intervals,BackgroundInteractionPollContext,maybe_schedule_background_interaction_cost_polling,maybe_settle_background_interaction_before_delete,@@ -92,6 +94,17 @@ def _fetch_sequence(*responses):return fetch, calls+ "initial, maximum",+ [(0.0, 0.002), (0.001, 0.0), (-1.0, 0.002), (0.0, 0.0)],+)+def test_poll_intervals_stops_instead_of_looping_on_a_non_positive_interval(initial, maximum):+ intervals = list(islice(_poll_intervals(initial=initial, maximum=maximum, timeout=3600.0), 10))++ assert len(intervals) < 10+ assert all(interval > 0 for interval in intervals)++@pytest.mark.asyncioasync def test_poller_bills_once_when_interaction_completes():logging_obj = _logging_obj()