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]

Merge remote-tracking branch 'origin/litellm_internal_staging' into litellm_batch_cost_accounted_once

mateo-berribb1c3366cff7
diff --cc litellm/proxy/batches_endpoints/endpoints.py
index 554a286555b,87f9927b191..6952c0c6f89
--- a/litellm/proxy/batches_endpoints/endpoints.py
+++ b/litellm/proxy/batches_endpoints/endpoints.py
@@@ -23,8 -23,8 +23,9 @@@ from litellm.proxy.common_utils.openai_
)
from litellm.proxy.openai_files_endpoints.common_utils import (
_is_base64_encoded_unified_file_id,
+ add_internal_model_credentials,
apply_team_provider_credentials,
+ batch_cost_poller_is_active,
decode_model_from_file_id,
encode_batch_response_ids,
encode_file_id_with_model,
diff --cc tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py
index 590f74d946f,ad7f5e4725a..6ffb7daaa2d
--- a/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py
+++ b/tests/test_litellm/proxy/openai_files_endpoint/test_files_common_utils.py
@@@ -97,270 -97,69 +97,337 @@@ def test_apply_unified_file_ids_swaps_a
)
+class _FakeScheduler:
+ def __init__(self, job):
+ self._job = job
+
+ def get_job(self, job_id):
+ assert job_id == "check_batch_cost_job"
+ return self._job
+
+
+class _FakePoller:
+ def __init__(self, confirmed):
+ self.batch_processed_support_confirmed = confirmed
+
+ def check_batch_cost(self):
+ return None
+
+
+def _job_for(poller):
+ if poller is None:
+ return None
+ job = MagicMock()
+ job.func = poller.check_batch_cost
+ return job
+
+
+ "polling_enabled, job, expected",
+ [
+ (True, _job_for(_FakePoller(confirmed=True)), True),
+ (True, _job_for(_FakePoller(confirmed=False)), False),
+ (True, None, False),
+ (False, _job_for(_FakePoller(confirmed=True)), False),
+ ],
+ ids=[
+ "poller-running-and-column-confirmed",
+ "poller-running-but-column-unconfirmed",
+ "job-absent-enterprise-import-failed",
+ "polling-disabled-by-config",
+ ],
+)
+def test_batch_cost_poller_is_active(monkeypatch, polling_enabled, job, expected):
+ import litellm.constants
+ import litellm.proxy.proxy_server as proxy_server_module
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ batch_cost_poller_is_active,
+ )
+
+ monkeypatch.setattr(litellm.constants, "PROXY_BATCH_POLLING_ENABLED", polling_enabled, raising=False)
+ monkeypatch.setattr(proxy_server_module, "scheduler", _FakeScheduler(job), raising=False)
+
+ assert batch_cost_poller_is_active() is expected
+
+
+def test_batch_cost_poller_is_active_is_false_when_no_scheduler_exists(monkeypatch):
+ import litellm.constants
+ import litellm.proxy.proxy_server as proxy_server_module
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ batch_cost_poller_is_active,
+ )
+
+ monkeypatch.setattr(litellm.constants, "PROXY_BATCH_POLLING_ENABLED", True, raising=False)
+ monkeypatch.setattr(proxy_server_module, "scheduler", None, raising=False)
+
+ assert batch_cost_poller_is_active() is False
+
+
+def _completed_batch() -> LiteLLMBatch:
+ return LiteLLMBatch(
+ id="batch-done",
+ completion_window="24h",
+ created_at=1234567890,
+ endpoint="/v1/chat/completions",
+ input_file_id="file-in",
+ object="batch",
+ status="completed",
+ output_file_id="file-out",
+ )
+
+
+async def _run_update(monkeypatch, poller_active: bool) -> dict:
+ import litellm.proxy.openai_files_endpoints.common_utils as cu
+
+ monkeypatch.setattr(cu, "batch_cost_poller_is_active", lambda: poller_active)
+ monkeypatch.setattr(cu, "ensure_batch_response_managed_file_ids", AsyncMock())
+
+ prisma_client = MagicMock()
+ update_mock = AsyncMock()
+ prisma_client.db.litellm_managedobjecttable.update = update_mock
+
+ db_batch_object = MagicMock()
+ db_batch_object.status = "in_progress"
+
+ await cu.update_batch_in_database(
+ batch_id="unified-batch-id",
+ unified_batch_id="unified-batch-id",
+ response=_completed_batch(),
+ managed_files_obj=MagicMock(),
+ prisma_client=prisma_client,
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=db_batch_object,
+ operation="retrieve",
+ )
+
+ assert update_mock.await_count == 1
+ return update_mock.await_args.kwargs["data"]
+
+
+async def test_retrieving_a_completed_batch_leaves_batch_processed_to_the_cost_poller(monkeypatch):
+ data = await _run_update(monkeypatch, poller_active=True)
+
+ assert "batch_processed" not in data
+ assert data["status"] == "complete"
+
+
+async def test_retrieving_a_completed_batch_still_marks_processed_without_a_cost_poller(monkeypatch):
+ data = await _run_update(monkeypatch, poller_active=False)
+
+ assert data["batch_processed"] is True
+ assert data["status"] == "complete"
+
+
+def test_batch_cost_poller_is_active_is_false_when_the_job_has_no_bound_poller(monkeypatch):
+ import litellm.constants
+ import litellm.proxy.proxy_server as proxy_server_module
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ batch_cost_poller_is_active,
+ )
+
+ def unbound_check_batch_cost():
+ return None
+
+ job = MagicMock()
+ job.func = unbound_check_batch_cost
+
+ monkeypatch.setattr(litellm.constants, "PROXY_BATCH_POLLING_ENABLED", True, raising=False)
+ monkeypatch.setattr(proxy_server_module, "scheduler", _FakeScheduler(job), raising=False)
+
+ assert batch_cost_poller_is_active() is False
+
+
+def test_batch_cost_poller_is_active_is_false_when_get_job_raises(monkeypatch):
+ import litellm.constants
+ import litellm.proxy.proxy_server as proxy_server_module
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ batch_cost_poller_is_active,
+ )
+
+ class _ExplodingScheduler:
+ def get_job(self, job_id):
+ raise RuntimeError("scheduler not started")
+
+ monkeypatch.setattr(litellm.constants, "PROXY_BATCH_POLLING_ENABLED", True, raising=False)
+ monkeypatch.setattr(proxy_server_module, "scheduler", _ExplodingScheduler(), raising=False)
+
+ assert batch_cost_poller_is_active() is False
+
+
-
+async def test_retrieving_a_batch_whose_status_is_unchanged_writes_nothing(monkeypatch):
+ import litellm.proxy.openai_files_endpoints.common_utils as cu
+
+ monkeypatch.setattr(cu, "batch_cost_poller_is_active", lambda: False)
+ monkeypatch.setattr(cu, "ensure_batch_response_managed_file_ids", AsyncMock())
+
+ prisma_client = MagicMock()
+ update_mock = AsyncMock()
+ prisma_client.db.litellm_managedobjecttable.update = update_mock
+
+ db_batch_object = MagicMock()
+ db_batch_object.status = "completed"
+
+ await cu.update_batch_in_database(
+ batch_id="unified-batch-id",
+ unified_batch_id="unified-batch-id",
+ response=_completed_batch(),
+ managed_files_obj=MagicMock(),
+ prisma_client=prisma_client,
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=db_batch_object,
+ operation="retrieve",
+ )
+
+ update_mock.assert_not_awaited()
+
+
+async def test_update_batch_in_database_is_a_noop_for_unmanaged_batches(monkeypatch):
+ import litellm.proxy.openai_files_endpoints.common_utils as cu
+
+ prisma_client = MagicMock()
+ update_mock = AsyncMock()
+ prisma_client.db.litellm_managedobjecttable.update = update_mock
+
+ await cu.update_batch_in_database(
+ batch_id="batch-raw-xyz",
+ unified_batch_id=False,
+ response=_completed_batch(),
+ managed_files_obj=MagicMock(),
+ prisma_client=prisma_client,
+ verbose_proxy_logger=MagicMock(),
+ operation="retrieve",
+ )
+
+ update_mock.assert_not_awaited()
+
+
+async def test_the_caller_s_accounting_decision_wins_over_a_later_poller_transition(monkeypatch):
+ import litellm.proxy.openai_files_endpoints.common_utils as cu
+
+ monkeypatch.setattr(cu, "batch_cost_poller_is_active", lambda: True)
+ monkeypatch.setattr(cu, "ensure_batch_response_managed_file_ids", AsyncMock())
+
+ prisma_client = MagicMock()
+ update_mock = AsyncMock()
+ prisma_client.db.litellm_managedobjecttable.update = update_mock
+ db_batch_object = MagicMock()
+ db_batch_object.status = "in_progress"
+
+ await cu.update_batch_in_database(
+ batch_id="unified-batch-id",
+ unified_batch_id="unified-batch-id",
+ response=_completed_batch(),
+ managed_files_obj=MagicMock(),
+ prisma_client=prisma_client,
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=db_batch_object,
+ operation="retrieve",
+ poller_owns_accounting=False,
+ )
+
+ data = update_mock.await_args.kwargs["data"]
+ assert data["batch_processed"] is True
+ assert data["status"] == "complete"
+
+
+async def test_a_caller_that_handed_off_accounting_still_leaves_the_marker_alone(monkeypatch):
+ import litellm.proxy.openai_files_endpoints.common_utils as cu
+
+ monkeypatch.setattr(cu, "batch_cost_poller_is_active", lambda: False)
+ monkeypatch.setattr(cu, "ensure_batch_response_managed_file_ids", AsyncMock())
+
+ prisma_client = MagicMock()
+ update_mock = AsyncMock()
+ prisma_client.db.litellm_managedobjecttable.update = update_mock
+ db_batch_object = MagicMock()
+ db_batch_object.status = "in_progress"
+
+ await cu.update_batch_in_database(
+ batch_id="unified-batch-id",
+ unified_batch_id="unified-batch-id",
+ response=_completed_batch(),
+ managed_files_obj=MagicMock(),
+ prisma_client=prisma_client,
+ verbose_proxy_logger=MagicMock(),
+ db_batch_object=db_batch_object,
+ operation="retrieve",
+ poller_owns_accounting=True,
+ )
+
+ data = update_mock.await_args.kwargs["data"]
+ assert "batch_processed" not in data
+ assert data["status"] == "complete"
++
++
+ # =========================================================================== #
+ # add_internal_model_credentials - the snapshot that lets a completed
+ # batch's output file be read, and therefore its cost be recorded
+ # =========================================================================== #
+
+
+ def test_add_internal_model_credentials_attaches_an_immutable_snapshot():
+ """Cost accounting for a completed batch reads its output file, and Bedrock resolves
+ that bucket only from this snapshot. It must be immutable so nothing downstream can
+ redirect the bucket that managed file ids are validated against."""
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ add_internal_model_credentials,
+ )
+
+ router = MagicMock()
+ router.get_deployment_credentials_with_provider = MagicMock(
+ return_value={"s3_bucket_name": "configured-bucket", "aws_region_name": "us-east-1"}
+ )
+ data = {"batch_id": "unified-batch-id"}
+
+ add_internal_model_credentials(data=data, llm_router=router, model_id="deployment-1")
+
+ snapshot = data["_litellm_internal_model_credentials"]
+ assert snapshot["s3_bucket_name"] == "configured-bucket"
+ assert isinstance(snapshot, MappingProxyType)
+ with pytest.raises(TypeError):
+ snapshot["s3_bucket_name"] = "attacker-bucket"
+ router.get_deployment_credentials_with_provider.assert_called_once_with(model_id="deployment-1")
+
+
+ @pytest.mark.parametrize(
+ "model_id, credentials",
+ [(None, {"s3_bucket_name": "b"}), ("deployment-1", None)],
+ ids=["no-model-id", "deployment-has-no-credentials"],
+ )
+ def test_add_internal_model_credentials_is_a_noop_without_a_resolvable_deployment(model_id, credentials):
+ """An unroutable batch must be left alone rather than given an empty snapshot, which
+ would look like a configured bucket of nothing."""
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ add_internal_model_credentials,
+ )
+
+ router = MagicMock()
+ router.get_deployment_credentials_with_provider = MagicMock(return_value=credentials)
+ data = {"batch_id": "unified-batch-id"}
+
+ add_internal_model_credentials(data=data, llm_router=router, model_id=model_id)
+
+ assert "_litellm_internal_model_credentials" not in data
+
+
+ def test_add_internal_model_credentials_survives_a_failing_deployment_lookup():
+ """The snapshot only enables cost accounting, so a batch whose deployment no longer
+ resolves, which happens when a model group is removed while batches are in flight,
+ must still be retrievable rather than failing the request on the lookup."""
+ from litellm.proxy.openai_files_endpoints.common_utils import (
+ add_internal_model_credentials,
+ )
+
+ router = MagicMock()
+ router.get_deployment_credentials_with_provider = MagicMock(side_effect=KeyError("deployment-gone"))
+ data = {"batch_id": "unified-batch-id"}
+
+ add_internal_model_credentials(data=data, llm_router=router, model_id="deployment-gone")
+
+ assert data == {"batch_id": "unified-batch-id"}