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(proxy): prevent OOM/Prisma connection loss from unbounded managed-object poll (#23472)
Ishaan Jaff1b9606460096
diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.mdindex b843a67c4f4..65eaf14471d 100644--- a/docs/my-website/docs/proxy/config_settings.md+++ b/docs/my-website/docs/proxy/config_settings.md@@ -935,6 +935,9 @@ router_settings:| PROXY_BASE_URL | Base URL for proxy service| PROXY_BATCH_WRITE_AT | Time in seconds to wait before batch writing spend logs to the database. Default is 10| PROXY_BATCH_POLLING_INTERVAL | Time in seconds to wait before polling a batch, to check if it's completed. Default is 6000s (1 hour)+| PROXY_BATCH_POLLING_ENABLED | Set to `false` to disable the `CheckBatchCost` and `CheckResponsesCost` background polling jobs entirely. Useful for emergency mitigation on installs with large numbers of stale managed objects. Default is `true`+| MAX_OBJECTS_PER_POLL_CYCLE | Maximum number of managed objects (batches / responses) fetched per polling cycle. Prevents OOM on installs with many stale rows. Default is `50`+| MANAGED_OBJECT_STALENESS_CUTOFF_DAYS | Managed objects older than this many days in a non-terminal state are marked `stale_expired` at the start of each poll cycle and skipped. Default is `7`| PROXY_BUDGET_RESCHEDULER_MAX_TIME | Maximum time in seconds to wait before checking database for budget resets. Default is 605| PROXY_BUDGET_RESCHEDULER_MIN_TIME | Minimum time in seconds to wait before checking database for budget resets. Default is 597| PYTHON_GC_THRESHOLD | GC thresholds ('gen0,gen1,gen2', e.g. '1000,50,50'); defaults to Python’s values.diff --git a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.pyindex 10f7f98b719..42a9acbfd1e 100644--- a/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py+++ b/enterprise/litellm_enterprise/proxy/common_utils/check_batch_cost.py@@ -2,11 +2,15 @@Polls LiteLLM_ManagedObjectTable to check if the batch job is complete, and if the cost has been tracked."""-from litellm._uuid import uuid-from datetime import datetime+from datetime import datetime, timedelta, timezonefrom typing import TYPE_CHECKING, Optionalfrom litellm._logging import verbose_proxy_logger+from litellm._uuid import uuid+from litellm.constants import (+ MANAGED_OBJECT_STALENESS_CUTOFF_DAYS,+ MAX_OBJECTS_PER_POLL_CYCLE,+)if TYPE_CHECKING:from litellm.proxy.utils import PrismaClient, ProxyLogging@@ -29,6 +33,9 @@ class CheckBatchCost:self.proxy_logging_obj: ProxyLogging = proxy_logging_objself.prisma_client: PrismaClient = prisma_clientself.llm_router: Router = llm_router+ # Cached after the first poll cycle. Once we know the column is absent we skip+ # the guaranteed-failing primary query on every subsequent cycle.+ self._has_batch_processed_column: bool = Trueasync def _get_user_info(self, batch_id, user_id) -> dict:"""@@ -49,6 +56,47 @@ class CheckBatchCost:verbose_proxy_logger.error(f"CheckBatchCost: could not look up user {user_id} for batch {batch_id}: {e}")return {}+ async def _cleanup_stale_managed_objects(self) -> None:+ """+ Mark managed objects older than MANAGED_OBJECT_STALENESS_CUTOFF_DAYS days+ in non-terminal states as 'stale_expired'. These will never complete and+ should not be polled.+ """+ cutoff = datetime.now(timezone.utc) - timedelta(days=MANAGED_OBJECT_STALENESS_CUTOFF_DAYS)+ result = await self.prisma_client.db.litellm_managedobjecttable.update_many(+ where={+ "file_purpose": "batch",+ "status": {"not_in": ["completed", "complete", "failed", "expired", "cancelled", "stale_expired"]},+ "created_at": {"lt": cutoff},+ },+ data={"status": "stale_expired"},+ )+ if result > 0:+ verbose_proxy_logger.warning(+ f"CheckBatchCost: marked {result} stale managed objects "+ f"(older than {MANAGED_OBJECT_STALENESS_CUTOFF_DAYS} days) as stale_expired"+ )++ async def _fallback_find_jobs(self) -> list:+ """Query batch jobs without the batch_processed filter (for older schemas)."""+ return await self.prisma_client.db.litellm_managedobjecttable.find_many(+ where={+ "file_purpose": "batch",+ "status": {+ "not_in": [+ "failed",+ "expired",+ "cancelled",+ "complete",+ "completed",+ "stale_expired",+ ]+ },+ },+ take=MAX_OBJECTS_PER_POLL_CYCLE,+ order={"created_at": "asc"},+ )+async def check_batch_cost(self):"""Check if the batch JOB has been tracked.@@ -70,14 +118,48 @@ class CheckBatchCost:get_model_id_from_unified_batch_id,)- # Look for all batches that have not yet been processed by CheckBatchCost- jobs = await self.prisma_client.db.litellm_managedobjecttable.find_many(- where={- "file_purpose": "batch",- "batch_processed" : False,- "status": {"not_in": ["failed", "expired", "cancelled"]}- }- )+ try:+ await self._cleanup_stale_managed_objects()+ except Exception as cleanup_err:+ verbose_proxy_logger.warning(+ f"CheckBatchCost: stale cleanup failed (poll will continue): {cleanup_err}"+ )++ # Look for all batches that have not yet been processed by CheckBatchCost.+ # self._has_batch_processed_column is cached after the first probe so that+ # older schemas don't pay a guaranteed-failing primary query + warning on+ # every subsequent poll cycle.+ if self._has_batch_processed_column:+ try:+ jobs = await self.prisma_client.db.litellm_managedobjecttable.find_many(+ where={+ "file_purpose": "batch",+ "batch_processed": False,+ "status": {+ "not_in": [+ "failed",+ "expired",+ "cancelled",+ "complete",+ "completed",+ "stale_expired",+ ]+ },+ },+ take=MAX_OBJECTS_PER_POLL_CYCLE,+ order={"created_at": "asc"},+ )+ except Exception as query_err:+ if "batch_processed" not in str(query_err).lower() and "unknown column" not in str(query_err).lower() and "does not exist" not in str(query_err).lower():+ raise+ # Permanent schema gap — cache the result so future cycles skip straight to fallback+ self._has_batch_processed_column = False+ verbose_proxy_logger.warning(+ "CheckBatchCost: batch_processed column not found, querying without it"+ )+ jobs = await self._fallback_find_jobs()+ else:+ jobs = await self._fallback_find_jobs()for job in jobs:# get the model from the jobunified_object_id = job.unified_object_id@@ -163,14 +245,14 @@ class CheckBatchCost:# Access content - handle both direct attribute and method callif hasattr(_file_content, 'content'):- content_bytes = _file_content.content+ content_bytes = _file_content.content # type: ignore[union-attr]elif hasattr(_file_content, 'read'):- content_bytes = await _file_content.read()+ content_bytes = await _file_content.read() # type: ignore[misc]else:- content_bytes = _file_content+ content_bytes = _file_content # type: ignore[assignment]file_content_as_dict = _get_file_content_as_dictionary(- content_bytes+ content_bytes # type: ignore[arg-type])deployment_info = self.llm_router.get_deployment(model_id=model_id)@@ -195,7 +277,7 @@ class CheckBatchCost:file_content_dictionary=file_content_as_dict,custom_llm_provider=llm_provider, # type: ignoremodel_name=model_name,- model_info=deployment_model_info,+ model_info=deployment_model_info, # type: ignore[arg-type]))logging_obj = LiteLLMLogging(@@ -236,13 +318,15 @@ class CheckBatchCost:# mark the job as completetry:+ update_data: dict = {+ "status": "complete",+ "file_object": response.model_dump_json(),+ }+ if self._has_batch_processed_column:+ update_data["batch_processed"] = Trueawait self.prisma_client.db.litellm_managedobjecttable.update(where={"id": job.id},- data={- "batch_processed": True,- "status": "complete",- "file_object": response.model_dump_json(),- },+ data=update_data,)except Exception as db_err:verbose_proxy_logger.error(diff --git a/enterprise/litellm_enterprise/proxy/common_utils/check_responses_cost.py b/enterprise/litellm_enterprise/proxy/common_utils/check_responses_cost.pyindex 4ee6a89cc98..54fbc7abcc5 100644--- a/enterprise/litellm_enterprise/proxy/common_utils/check_responses_cost.py+++ b/enterprise/litellm_enterprise/proxy/common_utils/check_responses_cost.py@@ -3,10 +3,15 @@ Polls LiteLLM_ManagedObjectTable to check if the response is complete.Cost tracking is handled automatically by litellm.aget_responses()."""+from datetime import datetime, timedelta, timezonefrom typing import TYPE_CHECKINGimport litellmfrom litellm._logging import verbose_proxy_logger+from litellm.constants import (+ MANAGED_OBJECT_STALENESS_CUTOFF_DAYS,+ MAX_OBJECTS_PER_POLL_CYCLE,+)if TYPE_CHECKING:from litellm.proxy.utils import PrismaClient, ProxyLogging@@ -27,6 +32,27 @@ class CheckResponsesCost:self.prisma_client: PrismaClient = prisma_clientself.llm_router: Router = llm_router+ async def _cleanup_stale_managed_objects(self) -> None:+ """+ Mark managed objects older than MANAGED_OBJECT_STALENESS_CUTOFF_DAYS days+ in non-terminal states as 'stale_expired'. These will never complete and+ should not be polled.+ """+ cutoff = datetime.now(timezone.utc) - timedelta(days=MANAGED_OBJECT_STALENESS_CUTOFF_DAYS)+ result = await self.prisma_client.db.litellm_managedobjecttable.update_many(+ where={+ "file_purpose": "response",+ "status": {"not_in": ["completed", "complete", "failed", "expired", "cancelled", "stale_expired"]},+ "created_at": {"lt": cutoff},+ },+ data={"status": "stale_expired"},+ )+ if result > 0:+ verbose_proxy_logger.warning(+ f"CheckResponsesCost: marked {result} stale managed objects "+ f"(older than {MANAGED_OBJECT_STALENESS_CUTOFF_DAYS} days) as stale_expired"+ )+async def check_responses_cost(self):"""Check if background responses are complete and track their cost.@@ -35,11 +61,20 @@ class CheckResponsesCost:- Cost is automatically tracked by litellm.aget_responses()- Mark completed/failed/cancelled responses as complete in the database"""+ try:+ await self._cleanup_stale_managed_objects()+ except Exception as cleanup_err:+ verbose_proxy_logger.warning(+ f"CheckResponsesCost: stale cleanup failed (poll will continue): {cleanup_err}"+ )+jobs = await self.prisma_client.db.litellm_managedobjecttable.find_many(where={"status": {"in": ["queued", "in_progress"]},"file_purpose": "response",- }+ },+ take=MAX_OBJECTS_PER_POLL_CYCLE,+ order={"created_at": "asc"},)verbose_proxy_logger.debug(f"Found {len(jobs)} response jobs to check")diff --git a/litellm/constants.py b/litellm/constants.pyindex 34b6950a214..dbc79b69a67 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -1351,6 +1351,15 @@ PROXY_BUDGET_RESCHEDULER_MIN_TIME = int(os.getenv("PROXY_BUDGET_RESCHEDULER_MIN_TIME", 597))PROXY_BATCH_POLLING_INTERVAL = int(os.getenv("PROXY_BATCH_POLLING_INTERVAL", 3600))+MAX_OBJECTS_PER_POLL_CYCLE = max(1, int(os.getenv("MAX_OBJECTS_PER_POLL_CYCLE", 50)))+MANAGED_OBJECT_STALENESS_CUTOFF_DAYS = max(+ 1, int(os.getenv("MANAGED_OBJECT_STALENESS_CUTOFF_DAYS", 7))+)+# Set PROXY_BATCH_POLLING_ENABLED=false to disable the CheckBatchCost and+# CheckResponsesCost background polling jobs entirely (e.g. to avoid DB load on+# installations with large numbers of stale managed objects).+_batch_polling_env = os.getenv("PROXY_BATCH_POLLING_ENABLED", "true").lower()+PROXY_BATCH_POLLING_ENABLED = _batch_polling_env == "true"PROXY_BUDGET_RESCHEDULER_MAX_TIME = int(os.getenv("PROXY_BUDGET_RESCHEDULER_MAX_TIME", 605))diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 77e2a88796e..2a9be0a67c9 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -214,6 +214,7 @@ from litellm.constants import (DEFAULT_MODEL_CREATED_AT_TIME,LITELLM_PROXY_ADMIN_NAME,PROMETHEUS_FALLBACK_STATS_SEND_TIME_HOURS,+ PROXY_BATCH_POLLING_ENABLED,PROXY_BATCH_POLLING_INTERVAL,PROXY_BATCH_WRITE_AT,PROXY_BUDGET_RESCHEDULER_MAX_TIME,@@ -260,7 +261,6 @@ from litellm.proxy.anthropic_endpoints.claude_code_endpoints import (claude_code_marketplace_router,)from litellm.proxy.anthropic_endpoints.endpoints import router as anthropic_router-from litellm.proxy.realtime_endpoints.endpoints import router as webrtc_routerfrom litellm.proxy.anthropic_endpoints.skills_endpoints import (router as anthropic_skills_router,)@@ -471,6 +471,7 @@ from litellm.proxy.policy_engine.policy_resolve_endpoints import (from litellm.proxy.prompts.prompt_endpoints import router as prompts_routerfrom litellm.proxy.public_endpoints import router as public_endpoints_routerfrom litellm.proxy.rag_endpoints.endpoints import router as rag_router+from litellm.proxy.realtime_endpoints.endpoints import router as webrtc_routerfrom litellm.proxy.rerank_endpoints.endpoints import router as rerank_routerfrom litellm.proxy.response_api_endpoints.endpoints import router as response_routerfrom litellm.proxy.route_llm_request import route_request@@ -6069,7 +6070,7 @@ class ProxyStartupEvent:"Invalid maximum_spend_logs_retention_interval value")### CHECK BATCH COST ###- if llm_router is not None:+ if llm_router is not None and PROXY_BATCH_POLLING_ENABLED:try:from litellm_enterprise.proxy.common_utils.check_batch_cost import (CheckBatchCost,@@ -6100,7 +6101,7 @@ class ProxyStartupEvent:pass### CHECK RESPONSES COST ###- if llm_router is not None:+ if llm_router is not None and PROXY_BATCH_POLLING_ENABLED:try:from litellm_enterprise.proxy.common_utils.check_responses_cost import (CheckResponsesCost,diff --git a/tests/proxy_unit_tests/test_check_batch_cost.py b/tests/proxy_unit_tests/test_check_batch_cost.pynew file mode 100644index 00000000000..f6b8d567848--- /dev/null+++ b/tests/proxy_unit_tests/test_check_batch_cost.py@@ -0,0 +1,340 @@+"""+Unit tests for CheckBatchCost class.+Covers: stale-row cleanup (file_purpose scoping), paginated find_many,+and the batch_processed-column fallback query.+"""++from unittest.mock import AsyncMock, MagicMock, patch++import pytest+++class TestCheckBatchCost:+ """Test suite for CheckBatchCost class"""++ @pytest.fixture+ def mock_prisma_client(self):+ client = MagicMock()+ client.db = MagicMock()+ client.db.litellm_managedobjecttable = MagicMock()+ client.db.litellm_usertable = MagicMock()+ return client++ @pytest.fixture+ def mock_proxy_logging_obj(self):+ return MagicMock()++ @pytest.fixture+ def mock_llm_router(self):+ return MagicMock()++ @pytest.fixture+ def check_batch_cost_instance(+ self, mock_proxy_logging_obj, mock_prisma_client, mock_llm_router+ ):+ from litellm_enterprise.proxy.common_utils.check_batch_cost import CheckBatchCost++ return CheckBatchCost(+ proxy_logging_obj=mock_proxy_logging_obj,+ prisma_client=mock_prisma_client,+ llm_router=mock_llm_router,+ )++ @pytest.mark.asyncio+ async def test_cleanup_scoped_to_batch_file_purpose(+ self, check_batch_cost_instance, mock_prisma_client+ ):+ """_cleanup_stale_managed_objects scopes its update to file_purpose='batch' only."""+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ # Return empty so the main poll loop exits immediately+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[]+ )++ await check_batch_cost_instance.check_batch_cost()++ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ stale_call = calls[0]+ assert stale_call[1]["data"] == {"status": "stale_expired"}+ where = stale_call[1]["where"]+ assert where["file_purpose"] == "batch"+ assert "stale_expired" in where["status"]["not_in"]+ assert "created_at" in where++ @pytest.mark.asyncio+ async def test_find_many_uses_pagination_and_excludes_stale(+ self, check_batch_cost_instance, mock_prisma_client+ ):+ """find_many is called with take, order, and all terminal statuses excluded."""+ from litellm.constants import MAX_OBJECTS_PER_POLL_CYCLE++ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[]+ )++ await check_batch_cost_instance.check_batch_cost()++ find_call = mock_prisma_client.db.litellm_managedobjecttable.find_many.call_args+ assert find_call[1]["take"] == MAX_OBJECTS_PER_POLL_CYCLE+ assert find_call[1]["order"] == {"created_at": "asc"}+ not_in = find_call[1]["where"]["status"]["not_in"]+ assert "stale_expired" in not_in+ assert "complete" in not_in+ assert "completed" in not_in++ @pytest.mark.asyncio+ async def test_fallback_query_used_when_batch_processed_missing(+ self, check_batch_cost_instance, mock_prisma_client+ ):+ """Falls back to query without batch_processed when primary query raises."""+ from litellm.constants import MAX_OBJECTS_PER_POLL_CYCLE++ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ # First find_many (primary query) raises with a schema error; second (fallback) returns empty+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ side_effect=[Exception("column batch_processed does not exist"), []]+ )++ await check_batch_cost_instance.check_batch_cost()++ calls = mock_prisma_client.db.litellm_managedobjecttable.find_many.call_args_list+ assert len(calls) == 2+ fallback_where = calls[1][1]["where"]+ assert "batch_processed" not in fallback_where+ assert "stale_expired" in fallback_where["status"]["not_in"]+ assert calls[1][1]["take"] == MAX_OBJECTS_PER_POLL_CYCLE+ # Column absence is now cached — next call should go straight to fallback+ assert check_batch_cost_instance._has_batch_processed_column is False++ @pytest.mark.asyncio+ async def test_column_absence_cached_across_cycles(+ self, check_batch_cost_instance, mock_prisma_client+ ):+ """After column absence is discovered, subsequent cycles skip the primary query entirely."""+ from litellm.constants import MAX_OBJECTS_PER_POLL_CYCLE++ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ # Simulate column already known absent from a previous cycle+ check_batch_cost_instance._has_batch_processed_column = False+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[]+ )++ await check_batch_cost_instance.check_batch_cost()++ # Only one find_many call — the fallback directly, no primary query attempt+ assert mock_prisma_client.db.litellm_managedobjecttable.find_many.call_count == 1+ fallback_where = mock_prisma_client.db.litellm_managedobjecttable.find_many.call_args[1]["where"]+ assert "batch_processed" not in fallback_where++ @pytest.mark.asyncio+ async def test_fallback_completion_update_omits_batch_processed(+ self, check_batch_cost_instance, mock_prisma_client, mock_llm_router+ ):+ """When batch_processed column is absent, completion update must not include it.++ If it did, the update would fail silently, the job would never be marked done,+ and every subsequent poll cycle would re-log the cost (duplicate billing).+ """+ from unittest.mock import patch++ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ mock_prisma_client.db.litellm_managedobjecttable.update = AsyncMock()+ mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(+ return_value=None+ )++ mock_job = MagicMock()+ mock_job.id = "job-fallback-1"+ mock_job.unified_object_id = "dW5pZmllZF9iYXRjaF9pZA=="+ mock_job.created_by = "user-1"++ # Simulate column already known absent (e.g. discovered on a previous cycle)+ check_batch_cost_instance._has_batch_processed_column = False+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[mock_job]+ )++ # Build a fake batch response whose status triggers the completion branch+ mock_response = MagicMock()+ mock_response.status = "completed"+ mock_response.output_file_id = "file-output-123"+ mock_response.model_dump_json.return_value = '{"id":"batch-1","status":"completed"}'++ mock_llm_router.aretrieve_batch = AsyncMock(return_value=mock_response)+ mock_llm_router.get_deployment_credentials_with_provider = MagicMock(+ return_value={"api_key": "sk-test"}+ )++ mock_deployment = MagicMock()+ mock_deployment.litellm_params.custom_llm_provider = "openai"+ mock_deployment.litellm_params.model = "gpt-4"+ mock_deployment.model_info.model_dump.return_value = {}+ mock_llm_router.get_deployment = MagicMock(return_value=mock_deployment)++ mock_file_content = MagicMock()+ mock_file_content.content = b'{"id":"req-1"}'++ decoded_id = "llm_model_id,model-123;llm_batch_id,batch-456;"++ with (+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils._is_base64_encoded_unified_file_id",+ side_effect=[decoded_id, None],+ ),+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils.get_model_id_from_unified_batch_id",+ return_value="model-123",+ ),+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils.get_batch_id_from_unified_batch_id",+ return_value="batch-456",+ ),+ patch(+ "litellm.files.main.afile_content",+ new_callable=AsyncMock,+ return_value=mock_file_content,+ ),+ patch(+ "litellm.batches.batch_utils._get_file_content_as_dictionary",+ return_value=[{"id": "req-1"}],+ ),+ patch(+ "litellm.batches.batch_utils.calculate_batch_cost_and_usage",+ new_callable=AsyncMock,+ return_value=(0.01, {"prompt_tokens": 10, "completion_tokens": 5}, ["gpt-4"]),+ ),+ patch(+ "litellm.litellm_core_utils.get_llm_provider_logic.get_llm_provider",+ return_value=("gpt-4", "openai", None, None),+ ),+ patch(+ "litellm.litellm_core_utils.litellm_logging.Logging"+ ) as mock_logging_cls,+ ):+ mock_logging_obj = MagicMock()+ mock_logging_obj.async_success_handler = AsyncMock()+ mock_logging_cls.return_value = mock_logging_obj++ await check_batch_cost_instance.check_batch_cost()++ # The update must have been called — this is the core assertion.+ assert mock_prisma_client.db.litellm_managedobjecttable.update.call_count == 1, (+ "Expected update() to be called exactly once for the completed job"+ )+ update_data = mock_prisma_client.db.litellm_managedobjecttable.update.call_args[1]["data"]+ assert "batch_processed" not in update_data, (+ "update() must NOT include batch_processed when column is absent"+ )+ assert update_data["status"] == "complete"++ @pytest.mark.asyncio+ async def test_primary_path_completion_update_includes_batch_processed(+ self, check_batch_cost_instance, mock_prisma_client, mock_llm_router+ ):+ """When batch_processed column IS present, completion update must set it to True.++ This is the symmetric counterpart to test_fallback_completion_update_omits_batch_processed+ and proves the conditional on _has_batch_processed_column governs the update data.+ """+ from unittest.mock import patch++ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )+ mock_prisma_client.db.litellm_managedobjecttable.update = AsyncMock()+ mock_prisma_client.db.litellm_usertable.find_unique = AsyncMock(+ return_value=None+ )++ mock_job = MagicMock()+ mock_job.id = "job-primary-1"+ mock_job.unified_object_id = "dW5pZmllZF9iYXRjaF9pZA=="+ mock_job.created_by = "user-1"++ assert check_batch_cost_instance._has_batch_processed_column is True+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[mock_job]+ )++ mock_response = MagicMock()+ mock_response.status = "completed"+ mock_response.output_file_id = "file-output-123"+ mock_response.model_dump_json.return_value = '{"id":"batch-1","status":"completed"}'++ mock_llm_router.aretrieve_batch = AsyncMock(return_value=mock_response)+ mock_llm_router.get_deployment_credentials_with_provider = MagicMock(+ return_value={"api_key": "sk-test"}+ )++ mock_deployment = MagicMock()+ mock_deployment.litellm_params.custom_llm_provider = "openai"+ mock_deployment.litellm_params.model = "gpt-4"+ mock_deployment.model_info.model_dump.return_value = {}+ mock_llm_router.get_deployment = MagicMock(return_value=mock_deployment)++ mock_file_content = MagicMock()+ mock_file_content.content = b'{"id":"req-1"}'++ decoded_id = "llm_model_id,model-123;llm_batch_id,batch-456;"++ with (+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils._is_base64_encoded_unified_file_id",+ side_effect=[decoded_id, None],+ ),+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils.get_model_id_from_unified_batch_id",+ return_value="model-123",+ ),+ patch(+ "litellm.proxy.openai_files_endpoints.common_utils.get_batch_id_from_unified_batch_id",+ return_value="batch-456",+ ),+ patch(+ "litellm.files.main.afile_content",+ new_callable=AsyncMock,+ return_value=mock_file_content,+ ),+ patch(+ "litellm.batches.batch_utils._get_file_content_as_dictionary",+ return_value=[{"id": "req-1"}],+ ),+ patch(+ "litellm.batches.batch_utils.calculate_batch_cost_and_usage",+ new_callable=AsyncMock,+ return_value=(0.01, {"prompt_tokens": 10, "completion_tokens": 5}, ["gpt-4"]),+ ),+ patch(+ "litellm.litellm_core_utils.get_llm_provider_logic.get_llm_provider",+ return_value=("gpt-4", "openai", None, None),+ ),+ patch(+ "litellm.litellm_core_utils.litellm_logging.Logging"+ ) as mock_logging_cls,+ ):+ mock_logging_obj = MagicMock()+ mock_logging_obj.async_success_handler = AsyncMock()+ mock_logging_cls.return_value = mock_logging_obj++ await check_batch_cost_instance.check_batch_cost()++ assert mock_prisma_client.db.litellm_managedobjecttable.update.call_count == 1, (+ "Expected update() to be called exactly once for the completed job"+ )+ update_data = mock_prisma_client.db.litellm_managedobjecttable.update.call_args[1]["data"]+ assert update_data["batch_processed"] is True, (+ "update() must include batch_processed=True when column is present"+ )+ assert update_data["status"] == "complete"diff --git a/tests/proxy_unit_tests/test_check_responses_cost.py b/tests/proxy_unit_tests/test_check_responses_cost.pyindex 3bcacdfc05d..601df9c4c7f 100644--- a/tests/proxy_unit_tests/test_check_responses_cost.py+++ b/tests/proxy_unit_tests/test_check_responses_cost.py@@ -8,6 +8,7 @@ from unittest.mock import AsyncMock, MagicMock, Mock, patchimport pytest+from litellm.constants import MAX_OBJECTS_PER_POLL_CYCLEfrom litellm.types.llms.openai import ResponseAPIUsage, ResponsesAPIResponse@@ -63,21 +64,46 @@ class TestCheckResponsesCost:self, check_responses_cost_instance, mock_prisma_client):"""Test check_responses_cost when there are no jobs to process"""- # Mock empty job listmock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[])+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )- # Should not raise any errorsawait check_responses_cost_instance.check_responses_cost()- # Verify find_many was called with correct parameters- mock_prisma_client.db.litellm_managedobjecttable.find_many.assert_called_once_with(- where={- "status": {"in": ["queued", "in_progress"]},- "file_purpose": "response",- }+ # Verify find_many was called with pagination params+ find_many_call = mock_prisma_client.db.litellm_managedobjecttable.find_many.call_args+ assert find_many_call[1]["where"] == {+ "status": {"in": ["queued", "in_progress"]},+ "file_purpose": "response",+ }+ assert find_many_call[1]["take"] == MAX_OBJECTS_PER_POLL_CYCLE+ assert find_many_call[1]["order"] == {"created_at": "asc"}++ @pytest.mark.asyncio+ async def test_cleanup_stale_managed_objects(+ self, check_responses_cost_instance, mock_prisma_client+ ):+ """Stale rows (older than cutoff) are bulk-updated to stale_expired before polling."""+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=5)+ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[]+ )++ await check_responses_cost_instance.check_responses_cost()++ # The first update_many call should be the stale-row cleanup scoped to "response"+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ stale_call = calls[0]+ assert stale_call[1]["data"] == {"status": "stale_expired"}+ where = stale_call[1]["where"]+ assert where["file_purpose"] == "response"+ assert "stale_expired" in where["status"]["not_in"]+ assert "created_at" in where@pytest.mark.asyncioasync def test_check_responses_cost_with_completed_response(@@ -89,6 +115,7 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_123"mock_job.created_by = "test-user"mock_job.id = "job-123"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_123"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job]@@ -108,8 +135,9 @@ class TestCheckResponsesCost:),)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the check with mocked litellm.aget_responseswith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -117,11 +145,12 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify the job was marked as completed- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_called_once()- call_args = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args- assert call_args[1]["data"]["status"] == "completed"- assert call_args[1]["where"]["id"]["in"] == ["job-123"]+ # calls[0] = stale cleanup, calls[1] = job completion+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 2+ completion_call = calls[1]+ assert completion_call[1]["data"]["status"] == "completed"+ assert completion_call[1]["where"]["id"]["in"] == ["job-123"]@pytest.mark.asyncioasync def test_check_responses_cost_with_failed_response(@@ -133,6 +162,7 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_456"mock_job.created_by = "test-user"mock_job.id = "job-456"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_456"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job]@@ -148,8 +178,9 @@ class TestCheckResponsesCost:usage=None,)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the checkwith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -157,10 +188,10 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify the job was marked as completed (even though response failed)- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_called_once()- call_args = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args- assert call_args[1]["data"]["status"] == "completed"+ # calls[0] = stale cleanup, calls[1] = job completion+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 2+ assert calls[1][1]["data"]["status"] == "completed"@pytest.mark.asyncioasync def test_check_responses_cost_with_cancelled_response(@@ -172,6 +203,7 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_789"mock_job.created_by = "test-user"mock_job.id = "job-789"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_789"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job]@@ -187,8 +219,9 @@ class TestCheckResponsesCost:usage=None,)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the checkwith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -196,8 +229,10 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify the job was marked as completed- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_called_once()+ # calls[0] = stale cleanup, calls[1] = job completion+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 2+ assert calls[1][1]["data"]["status"] == "completed"@pytest.mark.asyncioasync def test_check_responses_cost_with_in_progress_response(@@ -209,6 +244,7 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_in_progress"mock_job.created_by = "test-user"mock_job.id = "job-in-progress"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_in_progress"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job]@@ -224,8 +260,9 @@ class TestCheckResponsesCost:usage=None,)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the checkwith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -233,8 +270,10 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify no updates were made (response still in progress)- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_not_called()+ # Only the stale-cleanup call should have fired — no completion update+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 1+ assert calls[0][1]["data"] == {"status": "stale_expired"}@pytest.mark.asyncioasync def test_check_responses_cost_with_queued_response(@@ -246,6 +285,7 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_queued"mock_job.created_by = "test-user"mock_job.id = "job-queued"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_queued"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job]@@ -261,8 +301,9 @@ class TestCheckResponsesCost:usage=None,)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the checkwith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -270,8 +311,10 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify no updates were made (response still queued)- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_not_called()+ # Only the stale-cleanup call should have fired — no completion update+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 1+ assert calls[0][1]["data"] == {"status": "stale_expired"}@pytest.mark.asyncioasync def test_check_responses_cost_with_exception(@@ -283,13 +326,15 @@ class TestCheckResponsesCost:mock_job.unified_object_id = "resp_test_error"mock_job.created_by = "test-user"mock_job.id = "job-error"+ mock_job.file_object = {"model": "gpt-4o", "id": "resp_test_error"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job])- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the check with mocked exceptionwith patch(@@ -300,8 +345,10 @@ class TestCheckResponsesCost:# Should not raise, just skip the jobawait check_responses_cost_instance.check_responses_cost()- # Verify no updates were made (job was skipped due to error)- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_not_called()+ # Only the stale-cleanup call should have fired — no completion update+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 1+ assert calls[0][1]["data"] == {"status": "stale_expired"}@pytest.mark.asyncioasync def test_check_responses_cost_multiple_jobs(@@ -313,16 +360,19 @@ class TestCheckResponsesCost:mock_job1.unified_object_id = "resp_test_1"mock_job1.created_by = "user1"mock_job1.id = "job-1"+ mock_job1.file_object = {"model": "gpt-4o", "id": "resp_test_1"}mock_job2 = MagicMock()mock_job2.unified_object_id = "resp_test_2"mock_job2.created_by = "user2"mock_job2.id = "job-2"+ mock_job2.file_object = {"model": "gpt-4o", "id": "resp_test_2"}mock_job3 = MagicMock()mock_job3.unified_object_id = "resp_test_3"mock_job3.created_by = "user3"mock_job3.id = "job-3"+ mock_job3.file_object = {"model": "gpt-4o", "id": "resp_test_3"}mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(return_value=[mock_job1, mock_job2, mock_job3]@@ -364,8 +414,9 @@ class TestCheckResponsesCost:),)- # Mock update_many- mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock()+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )# Run the checkwith patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:@@ -373,10 +424,41 @@ class TestCheckResponsesCost:await check_responses_cost_instance.check_responses_cost()- # Verify only the 2 completed jobs were marked as complete- mock_prisma_client.db.litellm_managedobjecttable.update_many.assert_called_once()- call_args = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args- assert len(call_args[1]["where"]["id"]["in"]) == 2- assert "job-1" in call_args[1]["where"]["id"]["in"]- assert "job-3" in call_args[1]["where"]["id"]["in"]- assert "job-2" not in call_args[1]["where"]["id"]["in"]+ # calls[0] = stale cleanup, calls[1] = completion of 2 finished jobs+ calls = mock_prisma_client.db.litellm_managedobjecttable.update_many.call_args_list+ assert len(calls) == 2+ completion_call = calls[1]+ assert len(completion_call[1]["where"]["id"]["in"]) == 2+ assert "job-1" in completion_call[1]["where"]["id"]["in"]+ assert "job-3" in completion_call[1]["where"]["id"]["in"]+ assert "job-2" not in completion_call[1]["where"]["id"]["in"]++ @pytest.mark.asyncio+ async def test_check_responses_cost_no_model_in_file_object(+ self, check_responses_cost_instance, mock_prisma_client+ ):+ """When file_object has no 'model' key, model_name is None and metadata skips model fields."""+ mock_job = MagicMock()+ mock_job.unified_object_id = "resp_test_no_model"+ mock_job.created_by = "test-user"+ mock_job.id = "job-no-model"+ mock_job.file_object = {} # no "model" key → model_name=None branch++ mock_prisma_client.db.litellm_managedobjecttable.find_many = AsyncMock(+ return_value=[mock_job]+ )+ mock_prisma_client.db.litellm_managedobjecttable.update_many = AsyncMock(+ return_value=0+ )++ mock_response = MagicMock()+ mock_response.status = "completed"++ with patch("litellm.aget_responses", new_callable=AsyncMock) as mock_aget:+ mock_aget.return_value = mock_response+ await check_responses_cost_instance.check_responses_cost()++ # aget_responses should be called without model metadata+ call_kwargs = mock_aget.call_args[1]+ assert "model" not in call_kwargs.get("litellm_metadata", {})+ assert "model_group" not in call_kwargs.get("litellm_metadata", {})