From defd8661f4e359994bf57a7f9e51ed8c479f17ff Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Sun, 6 Sep 2026 00:04:24 -0700 Subject: [PATCH] refactor(spend): stop queueing a batch's claim row for a writer the proxy never builds SPEND_LOGS_URL only diverts spend logs when db_writer_client is set, and nothing in the proxy ever assigns that global, so the queued copy was only ever skipped as a duplicate by the local insert. --- litellm/proxy/db/db_spend_update_writer.py | 15 ------- .../proxy/db/test_db_spend_update_writer.py | 43 ------------------- 2 files changed, 58 deletions(-) diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py index bdc014d7f13..9230be8055e 100644 --- a/litellm/proxy/db/db_spend_update_writer.py +++ b/litellm/proxy/db/db_spend_update_writer.py @@ -380,9 +380,6 @@ class DBSpendUpdateWriter: skip_duplicates=True, ) if claimed == 1: - await self._forward_batch_cost_row( - row=row, prisma_client=prisma_client, disable_spend_logs=disable_spend_logs - ) return True existing: Final = await spend_logs.find_unique( where={"request_id": request_id} # mutable-ok: prisma where clause @@ -405,18 +402,6 @@ class DBSpendUpdateWriter: return False return await self._take_over_uncharged_batch_cost_row(payload=payload, prisma_client=prisma_client, row=row) - async def _forward_batch_cost_row( - self, row: Mapping[str, object], prisma_client: "PrismaClient", disable_spend_logs: bool - ) -> None: - """Queue the claimed row for an external spend log writer, which the claim went around. - - With ``SPEND_LOGS_URL`` set the queue posts every spend log to that writer instead of - inserting it, so a batch's cost row reaches it only by being queued here as well. - """ - if disable_spend_logs is True or os.getenv("SPEND_LOGS_URL") is None: - return - await self._insert_spend_log_to_db(payload=prisma_client.jsonify_object(row), prisma_client=prisma_client) - async def _take_over_uncharged_batch_cost_row( self, payload: SpendLogsPayload, prisma_client: "PrismaClient", row: Mapping[str, object] ) -> bool: diff --git a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py b/tests/test_litellm/proxy/db/test_db_spend_update_writer.py index a8aeaced55f..0bca7c9492c 100644 --- a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py +++ b/tests/test_litellm/proxy/db/test_db_spend_update_writer.py @@ -1,7 +1,6 @@ import asyncio import copy import json -import os import re @@ -3212,48 +3211,6 @@ async def test_update_database_claims_a_batch_without_logging_the_request_that_p assert db_writer._batch_database_updates.await_count == 1 -@pytest.mark.asyncio -@pytest.mark.parametrize( - ("spend_logs_url", "forwarded"), - [("http://spend-logs.internal", True), (None, False)], - ids=["an_external_writer_takes_the_rows", "rows_are_written_to_this_db"], -) -async def test_update_database_sends_a_claimed_batch_cost_row_on_to_an_external_spend_log_writer( - monkeypatch, spend_logs_url: str | None, forwarded: bool -): - """ - SPEND_LOGS_URL makes the flush post spend logs to that writer instead of inserting them, - and the claim writes straight to this table, so the batch's row reaches the writer only - by being queued as well. Queueing it with no writer configured would insert it twice. - """ - db_writer = DBSpendUpdateWriter() - db_writer._batch_database_updates = AsyncMock() - prisma = _spend_logs_prisma(1, None) - if spend_logs_url is None: - monkeypatch.delenv("SPEND_LOGS_URL", raising=False) - else: - monkeypatch.setenv("SPEND_LOGS_URL", spend_logs_url) - - assert await _update_database_with(db_writer, prisma, _batch_cost_payload()) is True - - queued = [row["request_id"] for row in prisma.spend_log_transactions] - assert queued == (["batch_abc_batch_cost"] if forwarded else []) - - -@pytest.mark.asyncio -async def test_update_database_forwards_no_batch_cost_row_a_later_retrieve_had_already_claimed(monkeypatch): - """The retrieve that lost the claim charges nothing, so it must not post a row either.""" - db_writer = DBSpendUpdateWriter() - db_writer._batch_database_updates = AsyncMock() - existing = SimpleNamespace(call_type="aretrieve_batch", status="success", spend=0.25) - prisma = _spend_logs_prisma(0, existing) - monkeypatch.setenv("SPEND_LOGS_URL", "http://spend-logs.internal") - - assert await _update_database_with(db_writer, prisma, _batch_cost_payload()) is False - - assert prisma.spend_log_transactions == [] - - @pytest.mark.asyncio async def test_update_database_queues_only_the_claim_for_a_batch_it_could_not_write_with_logs_disabled(): """A refused claim is retried through the queue, so what it queues has to stay unlogged too."""