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(anthropic): clear the placeholder cost once and let logging price recovered tokens

mateo-berri2dabac186abc
diff --git a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py
index f1387606f4e..7cec3bac207 100644
--- a/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py
+++ b/litellm/proxy/pass_through_endpoints/llm_provider_handlers/anthropic_passthrough_logging_handler.py
@@ -223,9 +223,7 @@ class AnthropicPassthroughLoggingHandler:
)
if recovered_usage is None:
return response
- AnthropicPassthroughLoggingHandler._reprice_recovered_stream(
- response=response, usage=recovered_usage, model=model, logging_obj=litellm_logging_obj
- )
+ AnthropicPassthroughLoggingHandler._clear_placeholder_cost(response=response, usage=recovered_usage)
return response
@staticmethod
@@ -405,22 +403,9 @@ class AnthropicPassthroughLoggingHandler:
return usage
@staticmethod
- def _reprice_recovered_stream(
- response: ModelResponse,
- usage: Usage,
- model: str,
- logging_obj: LiteLLMLoggingObj,
- ) -> None:
- hidden_params: Final = response._hidden_params # pyright: ignore[reportPrivateUsage] # no public accessor
+ def _clear_placeholder_cost(response: ModelResponse, usage: Usage) -> None:
usage.cost = None
- hidden_params.pop("response_cost", None)
- recovered_cost: Final = AnthropicPassthroughLoggingHandler._cost_partial_stream_or_zero(
- partial_response=response, model=model, logging_obj=logging_obj
- )
- if recovered_cost <= 0:
- return
- usage.cost = recovered_cost
- hidden_params["response_cost"] = recovered_cost
+ response._hidden_params.pop("response_cost", None) # pyright: ignore[reportPrivateUsage] # no public accessor
@staticmethod
def _create_anthropic_response_logging_payload(
diff --git a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py
index 89193adca1f..8043496f299 100644
--- a/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py
+++ b/tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py
@@ -834,29 +834,15 @@ class _SuccessRecorder(CustomLogger):
self.success_kwargs.append(kwargs)
-async def test_client_disconnect_partial_billing_prices_recovered_tokens(monkeypatch):
- """
- Regression (LIT-6872): a client disconnect that lands on partial billing
- re-tokenizes the buffered text into completion_tokens, but the logged cost
- stayed priced at the message_start placeholder (1 output token). The success
- row's response_cost must match its recovered completion_tokens.
- """
- import litellm
- from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
-
- monkeypatch.setattr(streaming_iterator_module, "ANTHROPIC_MESSAGES_MAX_DETACHED_STREAM_DRAINS", 0)
- monkeypatch.setattr(streaming_iterator_module, "ANTHROPIC_MESSAGES_STREAM_RELAY_QUEUE_MAXSIZE", 4)
- model = "claude-sonnet-5"
- recorder = _SuccessRecorder()
+def _make_priced_logging_obj(call_id: str, recorder: _SuccessRecorder, model: str) -> LiteLLMLoggingObj:
logging_obj = LiteLLMLoggingObj(
model=model,
messages=[{"role": "user", "content": "hi"}],
stream=True,
call_type="anthropic_messages",
start_time=datetime.now(),
- litellm_call_id="disconnect_partial_cost",
- function_id="disconnect_partial_cost",
+ litellm_call_id=call_id,
+ function_id=call_id,
dynamic_async_success_callbacks=[recorder],
)
logging_obj.update_environment_variables(
@@ -866,8 +852,50 @@ async def test_client_disconnect_partial_billing_prices_recovered_tokens(monkeyp
litellm_params={"custom_llm_provider": "anthropic"},
custom_llm_provider="anthropic",
)
+ return logging_obj
+
+
+class _UpstreamClosedOnDetach:
+ """Upstream that yields its events and then, like a socket read, waits until it is closed."""
+
+ def __init__(self, events: tuple[dict, ...]):
+ self._events = iter(events)
+ self._closed = asyncio.Event()
+
+ def __aiter__(self):
+ return self
+
+ async def __anext__(self) -> dict:
+ if self._closed.is_set():
+ raise StopAsyncIteration
+ try:
+ return next(self._events)
+ except StopIteration:
+ await self._closed.wait()
+ raise StopAsyncIteration
+
+ async def aclose(self) -> None:
+ self._closed.set()
+
+
+async def test_client_disconnect_partial_billing_prices_recovered_tokens(monkeypatch):
+ """
+ Regression (LIT-6872): a client disconnect that lands on partial billing
+ re-tokenizes the buffered text into completion_tokens, but the logged cost
+ stayed priced at the message_start placeholder (1 output token). The success
+ row's response_cost must match its recovered completion_tokens.
+ """
+ import litellm
+ from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
+
+ monkeypatch.setattr(streaming_iterator_module, "ANTHROPIC_MESSAGES_MAX_DETACHED_STREAM_DRAINS", 0)
+ monkeypatch.setattr(streaming_iterator_module, "ANTHROPIC_MESSAGES_STREAM_RELAY_QUEUE_MAXSIZE", 4)
+ model = "claude-sonnet-5"
+ recorder = _SuccessRecorder()
iterator = BaseAnthropicMessagesStreamingIterator(
- litellm_logging_obj=logging_obj, request_body={"model": model, "stream": True}
+ litellm_logging_obj=_make_priced_logging_obj("disconnect_partial_cost", recorder, model),
+ request_body={"model": model, "stream": True},
)
sentence = "The history of computing spans centuries of mechanical and electronic invention. "
@@ -908,6 +936,63 @@ async def test_client_disconnect_partial_billing_prices_recovered_tokens(monkeyp
assert logged["response_cost"] == pytest.approx(prompt_cost + completion_cost)
+async def test_proxy_disconnect_closing_upstream_prices_recovered_tokens():
+ """
+ Regression (LIT-6872), proxy path: after a client disconnect the proxy's
+ shielded cleanup closes the upstream stream while the pump is still reading
+ it, so the pump bills the chunks collected so far without ever seeing
+ message_delta. That row's response_cost must be priced from its recovered
+ completion_tokens, not from the message_start placeholder.
+ """
+ import litellm
+ from litellm.litellm_core_utils.logging_worker import GLOBAL_LOGGING_WORKER
+
+ model = "claude-sonnet-5"
+ recorder = _SuccessRecorder()
+ iterator = BaseAnthropicMessagesStreamingIterator(
+ litellm_logging_obj=_make_priced_logging_obj("disconnect_upstream_closed", recorder, model),
+ request_body={"model": model, "stream": True},
+ )
+ sentence = "The history of computing spans centuries of mechanical and electronic invention. "
+ upstream = _UpstreamClosedOnDetach(
+ (
+ {"type": "message_start", "message": {"id": "msg_1", "usage": {"input_tokens": 29, "output_tokens": 1}}},
+ {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
+ *({"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": sentence}} for _ in range(6)),
+ )
+ )
+ enqueued: list = []
+
+ def _capture(async_coroutine):
+ enqueued.append(async_coroutine)
+
+ with patch.object( # test-quality-ok: GLOBAL_LOGGING_WORKER is a process-global singleton with no injection seam
+ GLOBAL_LOGGING_WORKER, "ensure_initialized_and_enqueue", side_effect=_capture
+ ):
+ gen = iterator.async_sse_wrapper(upstream)
+ for _ in range(4):
+ await gen.__anext__()
+ await gen.aclose()
+ assert not enqueued, "billing must wait for the upstream read to end, not the client detach"
+ await upstream.aclose()
+ for _ in range(500):
+ if enqueued:
+ break
+ await asyncio.sleep(0.01)
+
+ assert len(enqueued) == 1, "closing the upstream never reached partial billing"
+ await enqueued[0]
+
+ assert len(recorder.success_kwargs) == 1
+ logged = recorder.success_kwargs[0]["standard_logging_object"]
+ assert logged["completion_tokens"] > 1
+ prompt_cost, completion_cost = litellm.cost_per_token(
+ model=model, prompt_tokens=29, completion_tokens=logged["completion_tokens"]
+ )
+ assert logged["response_cost"] == pytest.approx(prompt_cost + completion_cost)
+
+
@pytest.mark.asyncio
async def test_async_sse_wrapper_aborts_upstream_when_detached_drain_cap_reached(monkeypatch):
"""
diff --git a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
index dc3e94fcff2..2d7397594aa 100644
--- a/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
+++ b/tests/test_litellm/proxy/pass_through_endpoints/llm_provider_handlers/test_anthropic_passthrough_logging_handler.py
@@ -1752,7 +1752,6 @@ class TestInterruptedStreamOutputTokenRecovery:
)
assert logged["response_cost"] == pytest.approx(prompt_cost + completion_cost)
assert logged["response_cost"] > prompt_cost + placeholder_completion_cost
- assert handled["result"].usage.cost == pytest.approx(prompt_cost + completion_cost)
class TestStreamFalseDeduplication: