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]

Add cost tracking for /v1/messages

Sameer Kankutec32f42098cf9
diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py
index f07a61c544c..e7bad1e19dd 100644
--- a/litellm/proxy/common_request_processing.py
+++ b/litellm/proxy/common_request_processing.py
@@ -734,7 +734,7 @@ class ProxyBaseLLMRequestProcessing:
"""
Anthropic /messages and Google /generateContent streaming data generator require SSE events
"""
- from litellm.types.utils import ModelResponse, ModelResponseStream
+ from litellm.types.utils import ModelResponse, ModelResponseStream, Usage
verbose_proxy_logger.debug("inside generator")
try:
@@ -759,6 +759,87 @@ class ProxyBaseLLMRequestProcessing:
response_str = litellm.get_response_string(response_obj=chunk)
str_so_far += response_str
+ # Inject cost into Anthropic-style SSE usage for /v1/messages for any provider
+ # Handle both dict SSE events and pre-formatted string SSE lines
+ if getattr(litellm, "include_cost_in_streaming_usage", False) is True:
+ try:
+ def _inject_cost_into_usage_dict(obj: dict) -> Optional[dict]:
+ if (
+ obj.get("type") == "message_delta"
+ and isinstance(obj.get("usage"), dict)
+ ):
+ _usage = obj["usage"]
+ prompt_tokens = int(_usage.get("input_tokens", 0) or 0)
+ completion_tokens = int(_usage.get("output_tokens", 0) or 0)
+ total_tokens = int(
+ _usage.get("total_tokens", prompt_tokens + completion_tokens)
+ or (prompt_tokens + completion_tokens)
+ )
+
+ _mr = ModelResponse(
+ usage=Usage(
+ prompt_tokens=prompt_tokens,
+ completion_tokens=completion_tokens,
+ total_tokens=total_tokens,
+ )
+ )
+ model_name = request_data.get("model", "")
+ try:
+ cost_val = litellm.completion_cost(
+ completion_response=_mr,
+ model=model_name,
+ )
+ except Exception:
+ cost_val = None
+ if cost_val is not None:
+ obj.setdefault("usage", {})["cost"] = cost_val
+ return obj
+ return None
+
+ def _inject_cost_into_sse_frame_str(frame_str: str) -> Optional[str]:
+ # frame_str may contain multiple lines like 'event: ...\ndata: {...}\n\n'
+ # We only modify the JSON in the 'data:' line
+ try:
+ # Split preserving lines
+ lines = frame_str.split("\n")
+ for idx, ln in enumerate(lines):
+ stripped_ln = ln.strip()
+ if stripped_ln.startswith("data:"):
+ json_part = stripped_ln.split("data:", 1)[1].strip()
+ if json_part and json_part != "[DONE]":
+ obj = json.loads(json_part)
+ maybe_modified = _inject_cost_into_usage_dict(obj)
+ if maybe_modified is not None:
+ # Replace just this line with updated JSON using safe_dumps
+ lines[idx] = f"data: {safe_dumps(maybe_modified)}"
+ return "\n".join(lines)
+ return None
+ except Exception:
+ return None
+
+ if isinstance(chunk, dict):
+ maybe_modified = _inject_cost_into_usage_dict(chunk)
+ if maybe_modified is not None:
+ chunk = maybe_modified
+ elif isinstance(chunk, (bytes, bytearray)):
+ # Decode to str, inject, and rebuild as bytes
+ try:
+ s = chunk.decode("utf-8", errors="ignore")
+ maybe_mod = _inject_cost_into_sse_frame_str(s)
+ if maybe_mod is not None:
+ chunk = (maybe_mod + ("" if maybe_mod.endswith("\n\n") else "\n\n")).encode("utf-8")
+ except Exception:
+ pass
+ elif isinstance(chunk, str):
+ # Try to parse SSE frame and inject cost into the data line
+ maybe_mod = _inject_cost_into_sse_frame_str(chunk)
+ if maybe_mod is not None:
+ # Ensure trailing frame separator
+ chunk = maybe_mod if maybe_mod.endswith("\n\n") else (maybe_mod + "\n\n")
+ except Exception:
+ # Never break streaming on optional cost injection
+ pass
+
# Format chunk using helper function
yield ProxyBaseLLMRequestProcessing.return_sse_chunk(chunk)
except Exception as e:
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 b9858202bf8..9b6e22b8196 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
@@ -20,7 +20,7 @@ from litellm.types.utils import ModelResponse, TextCompletionResponse
if TYPE_CHECKING:
from ..success_handler import PassThroughEndpointLogging
- from ..types import EndpointType
+ from litellm.types.passthrough_endpoints.pass_through_endpoints import EndpointType
else:
PassThroughEndpointLogging = Any
EndpointType = Any
@@ -228,6 +228,7 @@ class AnthropicPassthroughLoggingHandler:
except (StopIteration, StopAsyncIteration):
break
complete_streaming_response = litellm.stream_chunk_builder(
- chunks=all_openai_chunks
+ chunks=all_openai_chunks,
+ logging_obj=litellm_logging_obj,
)
return complete_streaming_response
diff --git a/tests/pass_through_tests/test_anthropic_passthrough.py b/tests/pass_through_tests/test_anthropic_passthrough.py
index 002fb20e9e8..6e819f9971c 100644
--- a/tests/pass_through_tests/test_anthropic_passthrough.py
+++ b/tests/pass_through_tests/test_anthropic_passthrough.py
@@ -296,3 +296,123 @@ async def test_anthropic_streaming_with_headers():
assert log_entry["end_user"] == "test-user-1"
assert log_entry["custom_llm_provider"] == "anthropic"
+
+
[email protected](retries=3, delay=2)
+async def test_anthropic_messages_streaming_cost_injection():
+ """
+ Test that cost is injected into message_delta usage for Anthropic Messages API streaming
+ """
+ print("Testing cost injection in Anthropic Messages API streaming response")
+
+ headers = {
+ "Authorization": "Bearer sk-1234",
+ "Content-Type": "application/json",
+ "anthropic-version": "2023-06-01",
+ }
+
+ payload = {
+ "model": "claude-3-7-sonnet-20250219",
+ "max_tokens": 10,
+ "stream": True,
+ "messages": [{"role": "user", "content": "Say 'Hi'"}],
+ }
+
+ async with aiohttp.ClientSession() as session:
+ async with session.post(
+ "http://0.0.0.0:4000/v1/messages",
+ json=payload,
+ headers=headers
+ ) as response:
+ assert response.status == 200
+
+ # Collect all SSE events
+ events = []
+ async for line in response.content:
+ line_str = line.decode('utf-8').strip()
+ if line_str.startswith('data: '):
+ try:
+ data = json.loads(line_str[6:]) # Remove 'data: ' prefix
+ events.append(data)
+ except json.JSONDecodeError:
+ continue
+
+ # Find message_delta event with usage
+ message_delta_events = [
+ event for event in events
+ if event.get('type') == 'message_delta' and 'usage' in event
+ ]
+
+ assert len(message_delta_events) > 0, "No message_delta events with usage found"
+
+ # Check that cost is included in usage
+ for event in message_delta_events:
+ usage = event.get('usage', {})
+ assert 'cost' in usage, f"Cost not found in usage: {usage}"
+ assert isinstance(usage['cost'], (int, float)), f"Cost should be numeric: {usage['cost']}"
+ assert usage['cost'] >= 0, f"Cost should be non-negative: {usage['cost']}"
+
+ print(f"✅ Found message_delta with cost: {usage}")
+
+ print(f"✅ Test passed: Found {len(message_delta_events)} message_delta events with cost")
+
+
[email protected](retries=3, delay=2)
+async def test_anthropic_messages_openai_model_streaming_cost_injection():
+ """
+ Test that cost is injected into message_delta usage for OpenAI model via Anthropic Messages API
+ """
+ print("Testing cost injection in Anthropic Messages API with OpenAI model")
+
+ headers = {
+ "Authorization": "Bearer sk-1234",
+ "Content-Type": "application/json",
+ "anthropic-version": "2023-06-01",
+ }
+
+ payload = {
+ "model": "openai/gpt-4o",
+ "max_tokens": 10,
+ "stream": True,
+ "messages": [{"role": "user", "content": "Say 'Hi'"}],
+ }
+
+ async with aiohttp.ClientSession() as session:
+ async with session.post(
+ "http://0.0.0.0:4000/v1/messages",
+ json=payload,
+ headers=headers
+ ) as response:
+ assert response.status == 200
+
+ # Collect all SSE events
+ events = []
+ async for line in response.content:
+ line_str = line.decode('utf-8').strip()
+ if line_str.startswith('data: '):
+ try:
+ data = json.loads(line_str[6:]) # Remove 'data: ' prefix
+ events.append(data)
+ except json.JSONDecodeError:
+ continue
+
+ # Find message_delta event with usage
+ message_delta_events = [
+ event for event in events
+ if event.get('type') == 'message_delta' and 'usage' in event
+ ]
+
+ assert len(message_delta_events) > 0, "No message_delta events with usage found"
+
+ # Check that cost is included in usage
+ for event in message_delta_events:
+ usage = event.get('usage', {})
+ assert 'cost' in usage, f"Cost not found in usage: {usage}"
+ assert isinstance(usage['cost'], (int, float)), f"Cost should be numeric: {usage['cost']}"
+ assert usage['cost'] >= 0, f"Cost should be non-negative: {usage['cost']}"
+
+ print(f"✅ Found message_delta with cost: {usage}")
+
+ print(f"✅ Test passed: Found {len(message_delta_events)} message_delta events with cost")