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(cost): price streamed Messages usage via calculate_usage and the logging obj

mateo5d5dc4523fb9
diff --git a/litellm/llms/anthropic/chat/transformation.py b/litellm/llms/anthropic/chat/transformation.py
index ef4ad7011c5..b4f040b0a8c 100644
--- a/litellm/llms/anthropic/chat/transformation.py
+++ b/litellm/llms/anthropic/chat/transformation.py
@@ -2152,7 +2152,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
def calculate_usage(
self,
- usage_object: dict,
+ usage_object: Mapping[str, Any],
reasoning_content: str | None,
completion_response: dict | None = None,
speed: str | None = None,
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py
index a0b69ecb0bf..f5299b273b4 100644
--- a/litellm/proxy/common_request_processing.py
+++ b/litellm/proxy/common_request_processing.py
@@ -158,7 +158,7 @@ ProxyRouteType: TypeAlias = Literal[
"acancel_run",
"adelete_run",
]
-from litellm.types.utils import ServerToolUse
+from litellm.llms.anthropic.chat.transformation import AnthropicConfig
# Type alias for streaming chunk serializer (chunk after hooks + cost injection -> wire format)
StreamChunkSerializer = Callable[[Any], str]
@@ -3321,7 +3321,9 @@ class ProxyBaseLLMRequestProcessing:
str_so_far += str(chunk.get("content", ""))
model_name = request_data.get("model", "")
- chunk = ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection(chunk, model_name)
+ chunk = ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection(
+ chunk, model_name, request_data.get("litellm_logging_obj")
+ )
# Set before the yield: an async generator suspends at the yield,
# so a GeneratorExit on client disconnect is raised there and any
@@ -3418,20 +3420,28 @@ class ProxyBaseLLMRequestProcessing:
@overload
@staticmethod
- def _process_chunk_with_cost_injection(chunk: bytes, model_name: str) -> bytes: ...
+ def _process_chunk_with_cost_injection(
+ chunk: bytes, model_name: str, litellm_logging_obj: LiteLLMLoggingObj | None = None
+ ) -> bytes: ...
@overload
@staticmethod
- def _process_chunk_with_cost_injection(chunk: object, model_name: str) -> object: ...
+ def _process_chunk_with_cost_injection(
+ chunk: object, model_name: str, litellm_logging_obj: LiteLLMLoggingObj | None = None
+ ) -> object: ...
@staticmethod
- def _process_chunk_with_cost_injection(chunk: object, model_name: str) -> object:
+ def _process_chunk_with_cost_injection(
+ chunk: object, model_name: str, litellm_logging_obj: LiteLLMLoggingObj | None = None
+ ) -> object:
"""
Process a streaming chunk and inject cost information if enabled.
Args:
chunk: The streaming chunk (dict, str, bytes, or bytearray)
model_name: Model name for cost calculation
+ litellm_logging_obj: The call's logging object, used to price the chunk with
+ the same custom/deployment pricing as the logging callback
Returns:
The processed chunk with cost information injected if applicable
@@ -3441,21 +3451,27 @@ class ProxyBaseLLMRequestProcessing:
try:
if isinstance(chunk, dict):
- maybe_modified: Final = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(chunk, model_name)
+ maybe_modified: Final = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(
+ chunk, model_name, litellm_logging_obj
+ )
if maybe_modified is not None:
return maybe_modified
elif isinstance(chunk, (bytes, bytearray)):
try:
s: Final = chunk.decode("utf-8")
if s.endswith(("\n\n", "\r\n\r\n")):
- maybe_mod = ProxyBaseLLMRequestProcessing._inject_cost_into_sse_frame_str(s, model_name)
+ maybe_mod = ProxyBaseLLMRequestProcessing._inject_cost_into_sse_frame_str(
+ s, model_name, litellm_logging_obj
+ )
if maybe_mod is not None:
return maybe_mod.encode("utf-8")
except Exception:
pass
elif isinstance(chunk, str):
# Try to parse SSE frame and inject cost into the data line
- maybe_mod = ProxyBaseLLMRequestProcessing._inject_cost_into_sse_frame_str(chunk, model_name)
+ maybe_mod = ProxyBaseLLMRequestProcessing._inject_cost_into_sse_frame_str(
+ chunk, model_name, litellm_logging_obj
+ )
if maybe_mod is not None:
# Ensure trailing frame separator
return maybe_mod if maybe_mod.endswith("\n\n") else (maybe_mod + "\n\n")
@@ -3466,13 +3482,16 @@ class ProxyBaseLLMRequestProcessing:
return chunk
@staticmethod
- def _inject_cost_into_sse_frame_str(frame_str: str, model_name: str) -> str | None:
+ def _inject_cost_into_sse_frame_str(
+ frame_str: str, model_name: str, litellm_logging_obj: LiteLLMLoggingObj | None = None
+ ) -> str | None:
"""
Inject cost information into an SSE frame string by modifying the JSON in the 'data:' line.
Args:
frame_str: SSE frame string that may contain multiple lines
model_name: Model name for cost calculation
+ litellm_logging_obj: The call's logging object, forwarded for pricing
Returns:
Modified SSE frame string with cost injected, or None if no modification needed
@@ -3486,7 +3505,9 @@ class ProxyBaseLLMRequestProcessing:
json_part = stripped_ln.split("data:", 1)[1].strip()
if json_part and json_part != "[DONE]":
obj = json.loads(json_part)
- maybe_modified = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(obj, model_name)
+ maybe_modified = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(
+ obj, model_name, litellm_logging_obj
+ )
if maybe_modified is not None:
lines[idx] = "data: " + safe_dumps(maybe_modified) + ("\r" if ln.endswith("\r") else "")
return "\n".join(lines)
@@ -3494,34 +3515,6 @@ class ProxyBaseLLMRequestProcessing:
except Exception:
return None
- @staticmethod
- def _anthropic_stream_usage_kwargs(usage: Mapping[str, Any]) -> Mapping[str, Any]:
- prompt_tokens: Final = int(usage.get("input_tokens", 0) or 0)
- completion_tokens: Final = int(usage.get("output_tokens", 0) or 0)
- total_tokens: Final = int(
- usage.get("total_tokens", prompt_tokens + completion_tokens) or (prompt_tokens + completion_tokens)
- )
- web_search_requests: Final = usage.get("web_search_requests")
- server_tool_use: Final = (
- ServerToolUse(web_search_requests=web_search_requests) if web_search_requests is not None else None
- )
- return MappingProxyType(
- {
- key: value
- for key, value in (
- ("prompt_tokens", prompt_tokens),
- ("completion_tokens", completion_tokens),
- ("total_tokens", total_tokens),
- ("completion_tokens_details", usage.get("completion_tokens_details")),
- ("prompt_tokens_details", usage.get("prompt_tokens_details")),
- ("cache_creation_input_tokens", usage.get("cache_creation_input_tokens")),
- ("cache_read_input_tokens", usage.get("cache_read_input_tokens")),
- ("server_tool_use", server_tool_use),
- )
- if value is not None
- }
- )
-
@staticmethod
def _openai_stream_usage_kwargs(usage: Mapping[str, Any]) -> Mapping[str, Any]:
prompt_tokens: Final = int(usage.get("prompt_tokens", 0) or 0)
@@ -3544,11 +3537,19 @@ class ProxyBaseLLMRequestProcessing:
)
@staticmethod
- def _stream_usage_kwargs_for_event(obj: Mapping[str, object], usage: Mapping[str, Any]) -> Mapping[str, Any] | None:
+ def _stream_usage_for_event(obj: Mapping[str, object], usage: Mapping[str, Any]) -> Usage | None:
+ """
+ Build the ``Usage`` to price a streamed usage event.
+
+ Anthropic goes through ``AnthropicConfig.calculate_usage``, the same transformation
+ the non-streaming path uses, so ``prompt_tokens`` is the full input total and the
+ cache read plus 5m/1h cache creation split land in ``prompt_tokens_details`` where
+ the pricer looks for them.
+ """
if obj.get("type") == "message_delta":
- return ProxyBaseLLMRequestProcessing._anthropic_stream_usage_kwargs(usage)
+ return AnthropicConfig().calculate_usage(usage_object=usage, reasoning_content=None)
if obj.get("object") == "chat.completion.chunk":
- return ProxyBaseLLMRequestProcessing._openai_stream_usage_kwargs(usage)
+ return Usage(**ProxyBaseLLMRequestProcessing._openai_stream_usage_kwargs(usage))
return None
@staticmethod
@@ -3563,7 +3564,41 @@ class ProxyBaseLLMRequestProcessing:
return None
@staticmethod
- def _inject_cost_into_usage_dict(obj: dict, model_name: str) -> dict | None:
+ def _logging_obj_cost_or_none(
+ model_response: ModelResponse, litellm_logging_obj: LiteLLMLoggingObj
+ ) -> float | None:
+ try:
+ cost: Final = litellm_logging_obj._response_cost_calculator(result=model_response) # pyright: ignore[reportPrivateUsage] # reuse the call's own cost calc for pricing parity with the logging callback
+ except Exception:
+ return None
+ return float(cost) if isinstance(cost, (int, float)) and not isinstance(cost, bool) else None
+
+ @staticmethod
+ def _streamed_usage_cost(
+ model_response: ModelResponse,
+ model_name: str,
+ service_tier: str | None,
+ litellm_logging_obj: LiteLLMLoggingObj | None,
+ ) -> float | None:
+ """
+ Price a streamed usage response through the call's logging object when there is one,
+ so the streamed cost picks up the same custom/deployment pricing (``custom_pricing``,
+ ``custom_llm_provider``, ``base_model``, ``router_model_id``) as the logging callback
+ rather than the model's sticker price; fall back to ``completion_cost`` by model name.
+ """
+ cost_from_logging_obj: Final = (
+ ProxyBaseLLMRequestProcessing._logging_obj_cost_or_none(model_response, litellm_logging_obj)
+ if litellm_logging_obj is not None
+ else None
+ )
+ if cost_from_logging_obj is not None:
+ return cost_from_logging_obj
+ return ProxyBaseLLMRequestProcessing._completion_cost_or_none(model_response, model_name, service_tier)
+
+ @staticmethod
+ def _inject_cost_into_usage_dict(
+ obj: dict, model_name: str, litellm_logging_obj: LiteLLMLoggingObj | None = None
+ ) -> dict | None:
"""
Inject cost information into the usage object of a streamed usage event
(Anthropic ``message_delta`` or OpenAI ``chat.completion.chunk``).
@@ -3571,6 +3606,8 @@ class ProxyBaseLLMRequestProcessing:
Args:
obj: Dictionary containing the SSE event data
model_name: Model name for cost calculation
+ litellm_logging_obj: The call's logging object, used so the injected cost
+ matches the cost the logging callback records
Returns:
Modified dictionary with cost injected, or None if no modification needed
@@ -3578,14 +3615,15 @@ class ProxyBaseLLMRequestProcessing:
usage: Final = obj.get("usage")
if not isinstance(usage, dict):
return None
- usage_kwargs: Final = ProxyBaseLLMRequestProcessing._stream_usage_kwargs_for_event(obj, usage)
- if usage_kwargs is None:
+ stream_usage: Final = ProxyBaseLLMRequestProcessing._stream_usage_for_event(obj, usage)
+ if stream_usage is None:
return None
service_tier: Final = obj.get("service_tier")
- cost_val: Final = ProxyBaseLLMRequestProcessing._completion_cost_or_none(
- ModelResponse(usage=Usage(**usage_kwargs)),
+ cost_val: Final = ProxyBaseLLMRequestProcessing._streamed_usage_cost(
+ ModelResponse(usage=stream_usage),
model_name,
service_tier if isinstance(service_tier, str) else None,
+ litellm_logging_obj,
)
if cost_val is None:
return None
diff --git a/litellm/proxy/pass_through_endpoints/streaming_handler.py b/litellm/proxy/pass_through_endpoints/streaming_handler.py
index 697eb7b96eb..b71622fc33d 100644
--- a/litellm/proxy/pass_through_endpoints/streaming_handler.py
+++ b/litellm/proxy/pass_through_endpoints/streaming_handler.py
@@ -106,7 +106,7 @@ class PassThroughStreamingHandler:
) # rebind-ok: SSE frame reassembly buffer across transport chunks
if complete_frames:
yield ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection(
- complete_frames, resolved_model_name
+ complete_frames, resolved_model_name, litellm_logging_obj
)
if pending:
yield pending
diff --git a/tests/test_litellm/proxy/test_common_request_processing.py b/tests/test_litellm/proxy/test_common_request_processing.py
index 355c6d27eb2..083982778b9 100644
--- a/tests/test_litellm/proxy/test_common_request_processing.py
+++ b/tests/test_litellm/proxy/test_common_request_processing.py
@@ -6082,6 +6082,121 @@ class TestInjectCostIntoUsageDict:
injected = json.loads(result.split("\n")[0].split("data:", 1)[1].strip())
assert injected["usage"]["cost"] == pytest.approx(self._expected_cost("gpt-4o-mini", 11, 4))
+ def test_message_delta_cost_charges_the_non_cached_input_tokens(self):
+ """Anthropic reports ``input_tokens`` excluding cache tokens, so reading it as the whole
+ prompt total drops the non-cached input from the bill on every cache hit."""
+ model = "claude-haiku-4-5"
+ pricing = litellm.model_cost[model]
+ event = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "input_tokens": 14,
+ "output_tokens": 8,
+ "cache_read_input_tokens": 3202,
+ "cache_creation_input_tokens": 0,
+ },
+ }
+
+ result = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(event, model)
+
+ assert result is not None
+ expected = (
+ 14 * pricing["input_cost_per_token"]
+ + 3202 * pricing["cache_read_input_token_cost"]
+ + 8 * pricing["output_cost_per_token"]
+ )
+ dropped_input = expected - 14 * pricing["input_cost_per_token"]
+ assert result["usage"]["cost"] == pytest.approx(expected)
+ assert result["usage"]["cost"] > dropped_input
+
+ def test_message_delta_prices_1h_cache_creation_above_the_5m_rate(self):
+ """The ``cache_creation`` 5m/1h split has to survive into ``prompt_tokens_details``,
+ otherwise a 1h write is billed at the cheaper 5m rate."""
+ model = "claude-haiku-4-5"
+ pricing = litellm.model_cost[model]
+ event = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "input_tokens": 14,
+ "output_tokens": 8,
+ "cache_read_input_tokens": 0,
+ "cache_creation_input_tokens": 2000,
+ "cache_creation": {"ephemeral_5m_input_tokens": 0, "ephemeral_1h_input_tokens": 2000},
+ },
+ }
+
+ result = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(event, model)
+
+ assert result is not None
+ base = 14 * pricing["input_cost_per_token"] + 8 * pricing["output_cost_per_token"]
+ expected_1h = base + 2000 * pricing["cache_creation_input_token_cost_above_1hr"]
+ flat_5m = base + 2000 * pricing["cache_creation_input_token_cost"]
+ assert expected_1h != pytest.approx(flat_5m)
+ assert result["usage"]["cost"] == pytest.approx(expected_1h)
+
+ def test_message_delta_prices_through_the_logging_obj_so_custom_pricing_applies(self):
+ """Costing by model name alone yields sticker price, so a deployment with a negotiated
+ discount streamed a ``usage.cost`` that disagreed with the callback's ``response_cost``."""
+
+ class _StubLoggingObj:
+ def __init__(self, cost):
+ self._cost = cost
+ self.captured_result = None
+
+ def _response_cost_calculator(self, result):
+ self.captured_result = result
+ return self._cost
+
+ model = "claude-haiku-4-5"
+ discounted_cost = 0.00099
+ stub = _StubLoggingObj(discounted_cost)
+ event = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {
+ "input_tokens": 14,
+ "output_tokens": 8,
+ "cache_read_input_tokens": 3202,
+ "cache_creation_input_tokens": 500,
+ "cache_creation": {"ephemeral_5m_input_tokens": 100, "ephemeral_1h_input_tokens": 400},
+ },
+ }
+
+ result = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(event, model, stub)
+
+ assert result is not None
+ assert result["usage"]["cost"] == discounted_cost
+ assert result["usage"]["cost"] != pytest.approx(self._expected_cost(model, 14 + 500 + 3202, 8))
+ usage = stub.captured_result.usage
+ assert usage.prompt_tokens == 14 + 500 + 3202
+ details = usage.prompt_tokens_details.cache_creation_token_details
+ assert details.ephemeral_5m_input_tokens == 100
+ assert details.ephemeral_1h_input_tokens == 400
+
+ def test_message_delta_falls_back_to_model_pricing_when_the_logging_obj_returns_no_cost(self):
+ class _StubLoggingObj:
+ def _response_cost_calculator(self, result):
+ return None
+
+ model = "claude-haiku-4-5"
+ pricing = litellm.model_cost[model]
+ event = {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn"},
+ "usage": {"input_tokens": 14, "output_tokens": 8, "cache_read_input_tokens": 3202},
+ }
+
+ result = ProxyBaseLLMRequestProcessing._inject_cost_into_usage_dict(event, model, _StubLoggingObj())
+
+ assert result is not None
+ assert result["usage"]["cost"] == pytest.approx(
+ 14 * pricing["input_cost_per_token"]
+ + 3202 * pricing["cache_read_input_token_cost"]
+ + 8 * pricing["output_cost_per_token"]
+ )
+
class TestProcessChunkWithCostInjection:
def test_complete_usage_frame_chunk_is_injected(self, monkeypatch):
@@ -6116,6 +6231,31 @@ class TestProcessChunkWithCostInjection:
assert ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection(chunk, "gpt-4o-mini") == chunk
+ def test_message_delta_frame_is_priced_with_the_logging_obj(self, monkeypatch):
+ """Pins that the logging object reaches the pricer through the byte-frame entry point,
+ which is how the proxy actually calls this on a streamed Messages API request."""
+ monkeypatch.setattr(litellm, "include_cost_in_streaming_usage", True)
+
+ class _StubLoggingObj:
+ def _response_cost_calculator(self, result):
+ return 0.00042
+
+ chunk = (
+ b"event: message_delta\n"
+ b'data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},'
+ b'"usage":{"input_tokens":14,"output_tokens":8,"cache_read_input_tokens":3202}}\n\n'
+ )
+
+ result = ProxyBaseLLMRequestProcessing._process_chunk_with_cost_injection(
+ chunk, "claude-haiku-4-5", _StubLoggingObj()
+ )
+
+ assert result != chunk
+ data_line = next(ln for ln in result.decode("utf-8").splitlines() if ln.startswith("data:"))
+ payload = json.loads(data_line.split("data:", 1)[1].strip())
+ assert payload["usage"]["cost"] == 0.00042
+ assert payload["usage"]["cache_read_input_tokens"] == 3202
+
# ---------------------------------------------------------------------------
# SSE keepalive during the time-to-first-token (issue #34819)