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]

test: update test - anthropic returns input tokens in final usage chunk now

Krrish Dholakia1ec080871349
diff --git a/litellm/proxy/_experimental/out/model_hub_table.html b/litellm/proxy/_experimental/out/model_hub_table/index.html
similarity index 100%
rename from litellm/proxy/_experimental/out/model_hub_table.html
rename to litellm/proxy/_experimental/out/model_hub_table/index.html
diff --git a/litellm/proxy/_experimental/out/onboarding.html b/litellm/proxy/_experimental/out/onboarding.html
deleted file mode 100644
index bdb2a582100..00000000000
--- a/litellm/proxy/_experimental/out/onboarding.html
+++ /dev/null
@@ -1 +0,0 @@
-<!DOCTYPE html><html id="__next_error__"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="preload" as="script" fetchPriority="low" href="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js"/><script src="/litellm-asset-prefix/_next/static/chunks/fd9d1056-205af899b895cbac.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/117-a0da667066d322b6.js" async=""></script><script src="/litellm-asset-prefix/_next/static/chunks/main-app-475d6efe4080647d.js" async=""></script><title>LiteLLM Dashboard</title><meta name="description" content="LiteLLM Proxy Admin UI"/><link rel="icon" href="/favicon.ico" type="image/x-icon" sizes="16x16"/><link rel="icon" href="./favicon.ico"/><meta name="next-size-adjust"/><script src="/litellm-asset-prefix/_next/static/chunks/polyfills-42372ed130431b0a.js" noModule=""></script></head><body><script src="/litellm-asset-prefix/_next/static/chunks/webpack-a426aae3231a8df1.js" async=""></script><script>(self.__next_f=self.__next_f||[]).push([0]);self.__next_f.push([2,null])</script><script>self.__next_f.push([1,"1:HL[\"/litellm-asset-prefix/_next/static/media/e4af272ccee01ff0-s.p.woff2\",\"font\",{\"crossOrigin\":\"\",\"type\":\"font/woff2\"}]\n2:HL[\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"style\"]\n3:HL[\"/litellm-asset-prefix/_next/static/css/618290a32b9bb91a.css\",\"style\"]\n"])</script><script>self.__next_f.push([1,"4:I[12846,[],\"\"]\n6:I[19107,[],\"ClientPageRoot\"]\n7:I[12011,[\"665\",\"static/chunks/3014691f-b7b79b78e27792f3.js\",\"50\",\"static/chunks/50-d0da2dd7acce2eb9.js\",\"154\",\"static/chunks/154-686bf7931b450e3d.js\",\"461\",\"static/chunks/app/onboarding/page-3c5840c907b0a5c8.js\"],\"default\",1]\n8:I[4707,[],\"\"]\n9:I[36423,[],\"\"]\nb:I[61060,[],\"\"]\nc:[]\n"])</script><script>self.__next_f.push([1,"0:[\"$\",\"$L4\",null,{\"buildId\":\"4H8_DZPfnSH0102u_DzG-\",\"assetPrefix\":\"/litellm-asset-prefix\",\"urlParts\":[\"\",\"onboarding\"],\"initialTree\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{}]}]},\"$undefined\",\"$undefined\",true],\"initialSeedData\":[\"\",{\"children\":[\"onboarding\",{\"children\":[\"__PAGE__\",{},[[\"$L5\",[\"$\",\"$L6\",null,{\"props\":{\"params\":{},\"searchParams\":{}},\"Component\":\"$7\"}],null],null],null]},[null,[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\",\"onboarding\",\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":\"$undefined\",\"notFoundStyles\":\"$undefined\"}]],null]},[[[[\"$\",\"link\",\"0\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/31b7f215e119031e.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}],[\"$\",\"link\",\"1\",{\"rel\":\"stylesheet\",\"href\":\"/litellm-asset-prefix/_next/static/css/618290a32b9bb91a.css\",\"precedence\":\"next\",\"crossOrigin\":\"$undefined\"}]],[\"$\",\"html\",null,{\"lang\":\"en\",\"children\":[\"$\",\"body\",null,{\"className\":\"__className_b0dd8a\",\"children\":[\"$\",\"$L8\",null,{\"parallelRouterKey\":\"children\",\"segmentPath\":[\"children\"],\"error\":\"$undefined\",\"errorStyles\":\"$undefined\",\"errorScripts\":\"$undefined\",\"template\":[\"$\",\"$L9\",null,{}],\"templateStyles\":\"$undefined\",\"templateScripts\":\"$undefined\",\"notFound\":[[\"$\",\"title\",null,{\"children\":\"404: This page could not be found.\"}],[\"$\",\"div\",null,{\"style\":{\"fontFamily\":\"system-ui,\\\"Segoe UI\\\",Roboto,Helvetica,Arial,sans-serif,\\\"Apple Color Emoji\\\",\\\"Segoe UI Emoji\\\"\",\"height\":\"100vh\",\"textAlign\":\"center\",\"display\":\"flex\",\"flexDirection\":\"column\",\"alignItems\":\"center\",\"justifyContent\":\"center\"},\"children\":[\"$\",\"div\",null,{\"children\":[[\"$\",\"style\",null,{\"dangerouslySetInnerHTML\":{\"__html\":\"body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}\"}}],[\"$\",\"h1\",null,{\"className\":\"next-error-h1\",\"style\":{\"display\":\"inline-block\",\"margin\":\"0 20px 0 0\",\"padding\":\"0 23px 0 0\",\"fontSize\":24,\"fontWeight\":500,\"verticalAlign\":\"top\",\"lineHeight\":\"49px\"},\"children\":\"404\"}],[\"$\",\"div\",null,{\"style\":{\"display\":\"inline-block\"},\"children\":[\"$\",\"h2\",null,{\"style\":{\"fontSize\":14,\"fontWeight\":400,\"lineHeight\":\"49px\",\"margin\":0},\"children\":\"This page could not be found.\"}]}]]}]}]],\"notFoundStyles\":[]}]}]}]],null],null],\"couldBeIntercepted\":false,\"initialHead\":[null,\"$La\"],\"globalErrorComponent\":\"$b\",\"missingSlots\":\"$Wc\"}]\n"])</script><script>self.__next_f.push([1,"a:[[\"$\",\"meta\",\"0\",{\"name\":\"viewport\",\"content\":\"width=device-width, initial-scale=1\"}],[\"$\",\"meta\",\"1\",{\"charSet\":\"utf-8\"}],[\"$\",\"title\",\"2\",{\"children\":\"LiteLLM Dashboard\"}],[\"$\",\"meta\",\"3\",{\"name\":\"description\",\"content\":\"LiteLLM Proxy Admin UI\"}],[\"$\",\"link\",\"4\",{\"rel\":\"icon\",\"href\":\"/favicon.ico\",\"type\":\"image/x-icon\",\"sizes\":\"16x16\"}],[\"$\",\"link\",\"5\",{\"rel\":\"icon\",\"href\":\"./favicon.ico\"}],[\"$\",\"meta\",\"6\",{\"name\":\"next-size-adjust\"}]]\n5:null\n"])</script></body></html>
\ No newline at end of file
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 a564419c797..b9858202bf8 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
@@ -96,7 +96,6 @@ class AnthropicPassthroughLoggingHandler:
handles streaming and non-streaming responses
"""
try:
-
response_cost = litellm.completion_cost(
completion_response=litellm_model_response,
model=model,
diff --git a/tests/pass_through_tests/test_anthropic_passthrough.py b/tests/pass_through_tests/test_anthropic_passthrough.py
index 4e7aca00084..e0549d17fa2 100644
--- a/tests/pass_through_tests/test_anthropic_passthrough.py
+++ b/tests/pass_through_tests/test_anthropic_passthrough.py
@@ -57,7 +57,7 @@ async def test_anthropic_basic_completion_with_headers():
max_retries = 2
for attempt in range(max_retries):
print(f"Attempt {attempt + 1}/{max_retries} to check spend logs")
-
+
async with session.get(
f"http://0.0.0.0:4000/spend/logs?request_id={litellm_call_id}",
headers={"Authorization": "Bearer sk-1234"},
@@ -66,32 +66,30 @@ async def test_anthropic_basic_completion_with_headers():
print(f"Spend response: {spend_response}")
spend_data = await spend_response.json()
print(f"Spend data: {spend_data}")
-
+
# Check if spend data exists and has entries
if spend_data and len(spend_data) > 0:
print("Spend logs found!")
break
else:
print("Spend logs not found yet...")
- if attempt < max_retries - 1: # Don't wait after the last attempt
+ if (
+ attempt < max_retries - 1
+ ): # Don't wait after the last attempt
print("Waiting 10 seconds before retry...")
await asyncio.sleep(10)
-
+
assert spend_data is not None, "Should have spend data for the request"
assert len(spend_data) > 0, "Should have at least one spend log entry"
- log_entry = spend_data[
- 0
- ] # Get the first (and should be only) log entry
+ log_entry = spend_data[0] # Get the first (and should be only) log entry
# Basic existence checks
assert spend_data is not None, "Should have spend data for the request"
assert isinstance(log_entry, dict), "Log entry should be a dictionary"
# Request metadata assertions
- assert (
- log_entry["request_id"] == litellm_call_id
- ), "Request ID should match"
+ assert log_entry["request_id"] == litellm_call_id, "Request ID should match"
assert (
log_entry["call_type"] == "pass_through_endpoint"
), "Call type should be pass_through_endpoint"
@@ -126,9 +124,7 @@ async def test_anthropic_basic_completion_with_headers():
), "Start time should be before end time"
# Metadata assertions
- assert (
- str(log_entry["cache_hit"]).lower() != "true"
- ), "Cache should be off"
+ assert str(log_entry["cache_hit"]).lower() != "true", "Cache should be off"
assert log_entry["request_tags"] == [
"test-tag-1",
"test-tag-2",
@@ -197,12 +193,19 @@ async def test_anthropic_streaming_with_headers():
json.dumps(anthropic_api_usage_chunks, indent=4, default=str),
)
- anthropic_api_input_tokens = sum(
- [usage.get("input_tokens", 0) for usage in anthropic_api_usage_chunks]
- )
- anthropic_api_output_tokens = max(
- [usage.get("output_tokens", 0) for usage in anthropic_api_usage_chunks]
- )
+ print("anthropic_api_usage_chunks: ", anthropic_api_usage_chunks)
+ # Get the most recent value of input tokens (iterate backwards to find last non-zero value)
+ anthropic_api_input_tokens = 0
+ for usage in reversed(anthropic_api_usage_chunks):
+ if usage.get("input_tokens", 0) > 0:
+ anthropic_api_input_tokens = usage.get("input_tokens", 0)
+ break
+ anthropic_api_output_tokens = 0
+ for usage in reversed(anthropic_api_usage_chunks):
+ if usage.get("output_tokens", 0) > 0:
+ anthropic_api_output_tokens = usage.get("output_tokens", 0)
+ break
+
print("anthropic_api_input_tokens", anthropic_api_input_tokens)
print("anthropic_api_output_tokens", anthropic_api_output_tokens)
@@ -214,39 +217,37 @@ async def test_anthropic_streaming_with_headers():
max_retries = 2
for attempt in range(max_retries):
print(f"Attempt {attempt + 1}/{max_retries} to check spend logs")
-
+
async with session.get(
f"http://0.0.0.0:4000/spend/logs?request_id={litellm_call_id}",
headers={"Authorization": "Bearer sk-1234"},
) as spend_response:
spend_data = await spend_response.json()
print(f"Spend data: {spend_data}")
-
+
# Check if spend data exists and has entries
if spend_data and len(spend_data) > 0:
print("Spend logs found!")
break
else:
print("Spend logs not found yet...")
- if attempt < max_retries - 1: # Don't wait after the last attempt
+ if (
+ attempt < max_retries - 1
+ ): # Don't wait after the last attempt
print("Waiting 10 seconds before retry...")
await asyncio.sleep(10)
-
+
assert spend_data is not None, "Should have spend data for the request"
assert len(spend_data) > 0, "Should have at least one spend log entry"
- log_entry = spend_data[
- 0
- ] # Get the first (and should be only) log entry
+ log_entry = spend_data[0] # Get the first (and should be only) log entry
# Basic existence checks
assert spend_data is not None, "Should have spend data for the request"
assert isinstance(log_entry, dict), "Log entry should be a dictionary"
# Request metadata assertions
- assert (
- log_entry["request_id"] == litellm_call_id
- ), "Request ID should match"
+ assert log_entry["request_id"] == litellm_call_id, "Request ID should match"
assert (
log_entry["call_type"] == "pass_through_endpoint"
), "Call type should be pass_through_endpoint"
@@ -281,9 +282,7 @@ async def test_anthropic_streaming_with_headers():
), "Start time should be before end time"
# Metadata assertions
- assert (
- str(log_entry["cache_hit"]).lower() != "true"
- ), "Cache should be off"
+ assert str(log_entry["cache_hit"]).lower() != "true", "Cache should be off"
assert log_entry["request_tags"] == [
"test-tag-stream-1",
"test-tag-stream-2",