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(perplexity): accept float usage.cost in cost_per_token, not just dict

mubashir1osmani782746553c10
diff --git a/litellm/llms/perplexity/cost_calculator.py b/litellm/llms/perplexity/cost_calculator.py
index 337fa8e630d..27835ecbfe8 100644
--- a/litellm/llms/perplexity/cost_calculator.py
+++ b/litellm/llms/perplexity/cost_calculator.py
@@ -21,14 +21,19 @@ def cost_per_token(model: str, usage: Usage) -> tuple[float, float]:
Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
"""
## USE PRE-CALCULATED COST FROM PERPLEXITY IF AVAILABLE
- ## Perplexity returns accurate cost in usage.cost.total_cost including request fees
+ ## Perplexity returns accurate cost in usage.cost.total_cost including request fees.
+ ## By the time it reaches here, ResponseAPIUsage.parse_cost has already flattened
+ ## that dict down to a float, so both shapes must be accepted.
cost_info: Final = getattr(usage, "cost", None)
- if cost_info is not None and isinstance(cost_info, dict):
- total_cost: Final = cost_info.get("total_cost")
- if total_cost is not None:
- # Return total cost as completion_cost (prompt_cost=0) since Perplexity
- # doesn't break down by input/output in their cost object
- return (0.0, float(total_cost))
+ total_cost: float | None = None
+ if isinstance(cost_info, dict):
+ total_cost = cost_info.get("total_cost")
+ elif isinstance(cost_info, (int, float)) and not isinstance(cost_info, bool):
+ total_cost = float(cost_info)
+ if total_cost is not None:
+ # Return total cost as completion_cost (prompt_cost=0) since Perplexity
+ # doesn't break down by input/output in their cost object
+ return (0.0, float(total_cost))
## FALLBACK: Calculate cost manually if Perplexity doesn't provide it
## GET MODEL INFO
diff --git a/tests/test_litellm/llms/perplexity/test_perplexity_cost_calculator.py b/tests/test_litellm/llms/perplexity/test_perplexity_cost_calculator.py
index 46c1e457d7c..71ccb494cc7 100644
--- a/tests/test_litellm/llms/perplexity/test_perplexity_cost_calculator.py
+++ b/tests/test_litellm/llms/perplexity/test_perplexity_cost_calculator.py
@@ -400,6 +400,31 @@ class TestPerplexityCostCalculator:
assert completion_cost == 0.008
assert prompt_cost + completion_cost == 0.008
+ def test_uses_perplexity_provided_cost_when_normalized_to_float(self):
+ """
+ Regression: for Responses API / Agent API models, `ResponseAPIUsage.parse_cost`
+ (litellm/types/llms/openai.py) already flattens Perplexity's
+ `usage.cost.total_cost` dict down to a plain float before
+ `_transform_response_api_usage_to_chat_usage` (litellm/responses/utils.py) copies
+ it onto the chat `Usage` object. So `usage.cost` arrives here as a float, not a
+ dict, on that path.
+
+ Pre-fix, the `isinstance(cost_info, dict)` check was always False for a float,
+ so the pre-calculated cost branch was dead code for every Responses-mode
+ Perplexity model and it silently fell back to manual token-rate calculation,
+ recording $0 for any model missing static per-token rates (e.g.
+ perplexity/openai/gpt-5.2 before rates existed).
+ """
+ usage = Usage(prompt_tokens=100, completion_tokens=50, total_tokens=150)
+ usage.cost = 0.008
+
+ prompt_cost, completion_cost = perplexity_cost_per_token(
+ model="sonar-pro", usage=usage
+ )
+
+ assert prompt_cost == 0.0
+ assert completion_cost == 0.008
+
def test_falls_back_to_manual_calculation_when_no_cost_provided(self):
"""
Test that manual cost calculation is used when Perplexity doesn't