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(responses): skip the session lookup retry when spend logs are off

mateo-berri9d22acab110f
diff --git a/litellm/responses/litellm_completion_transformation/session_handler.py b/litellm/responses/litellm_completion_transformation/session_handler.py
index 15267533957..59ff492a79f 100644
--- a/litellm/responses/litellm_completion_transformation/session_handler.py
+++ b/litellm/responses/litellm_completion_transformation/session_handler.py
@@ -258,13 +258,14 @@ class ResponsesSessionHandler:
A just-finished turn gets a short second chance: the worker that served it may
still be writing its spend log when the follow-up arrives, and an empty result
- drops the whole conversation instead of erroring.
+ drops the whole conversation instead of erroring. Deployments that write no spend
+ logs at all have nothing to wait for, so they keep the single original query.
"""
from litellm.constants import (
RESPONSES_SESSION_LOOKUP_MAX_ATTEMPTS,
RESPONSES_SESSION_LOOKUP_RETRY_INTERVAL,
)
- from litellm.proxy.proxy_server import prisma_client
+ from litellm.proxy.proxy_server import disable_spend_logs, prisma_client
verbose_proxy_logger.debug("decoding response id=%s", previous_response_id)
@@ -285,7 +286,8 @@ class ResponsesSessionHandler:
ORDER BY "endTime" ASC;
"""
- for attempt in range(RESPONSES_SESSION_LOOKUP_MAX_ATTEMPTS):
+ max_attempts: Final = 1 if disable_spend_logs else RESPONSES_SESSION_LOOKUP_MAX_ATTEMPTS
+ for attempt in range(max_attempts):
if attempt:
await asyncio.sleep(RESPONSES_SESSION_LOOKUP_RETRY_INTERVAL)
if spend_logs := await prisma_client.db.query_raw(query, response_id):
diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_session_handler.py b/tests/test_litellm/responses/litellm_completion_transformation/test_session_handler.py
index 4fc288a47cb..df477f6d01e 100644
--- a/tests/test_litellm/responses/litellm_completion_transformation/test_session_handler.py
+++ b/tests/test_litellm/responses/litellm_completion_transformation/test_session_handler.py
@@ -617,3 +617,24 @@ async def test_message_history_looks_up_the_decoded_chat_completion_id():
)
assert fake_prisma_client.db.calls == [(request_id,)]
+
+
+async def test_session_lookup_does_not_retry_when_spend_logs_are_disabled(
+ instant_session_lookup_retries: None,
+):
+ """
+ A deployment that writes no spend logs has nothing to wait for, so the miss path keeps
+ the single query it always had.
+ """
+ fake_prisma_client = _FakePrismaClient(results=[])
+
+ with patch("litellm.proxy.proxy_server.prisma_client", fake_prisma_client), patch(
+ "litellm.proxy.proxy_server.disable_spend_logs", True
+ ):
+ spend_logs = await ResponsesSessionHandler.get_all_spend_logs_for_previous_response_id(
+ "chatcmpl-does-not-exist"
+ )
+
+ assert spend_logs == []
+ assert fake_prisma_client.db.calls == [("chatcmpl-does-not-exist",)]
diff --git a/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_response_id.py b/tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator.py
similarity index 100%
rename from tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator_response_id.py
rename to tests/test_litellm/responses/litellm_completion_transformation/test_streaming_iterator.py