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 PR review comments

Sameer Kankutef23e549cce94
diff --git a/litellm/llms/anthropic/cost_calculation.py b/litellm/llms/anthropic/cost_calculation.py
index a96d9e006c9..cf9b18c4643 100644
--- a/litellm/llms/anthropic/cost_calculation.py
+++ b/litellm/llms/anthropic/cost_calculation.py
@@ -5,13 +5,52 @@ Helper util for handling anthropic-specific cost calculation
from typing import TYPE_CHECKING, Optional, Tuple
-from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
+from litellm.litellm_core_utils.llm_cost_calc.utils import (
+ _get_token_base_cost,
+ _parse_prompt_tokens_details,
+ calculate_cache_writing_cost,
+ generic_cost_per_token,
+)
if TYPE_CHECKING:
from litellm.types.utils import ModelInfo, Usage
import litellm
+def _compute_cache_only_cost(model_info: "ModelInfo", usage: "Usage") -> float:
+ """
+ Return only the cache-related portion of the prompt cost (cache read + cache write).
+
+ These costs must NOT be scaled by geo/speed multipliers because the old
+ explicit ``fast/`` model entries carried unchanged cache rates while
+ multiplying only the regular input/output token costs.
+ """
+ if usage.prompt_tokens_details is None:
+ return 0.0
+
+ prompt_tokens_details = _parse_prompt_tokens_details(usage)
+ _, _, cache_creation_cost, cache_creation_cost_above_1hr, cache_read_cost = (
+ _get_token_base_cost(model_info=model_info, usage=usage)
+ )
+
+ cache_cost = float(prompt_tokens_details["cache_hit_tokens"]) * cache_read_cost
+
+ if (
+ prompt_tokens_details["cache_creation_tokens"]
+ or prompt_tokens_details["cache_creation_token_details"] is not None
+ ):
+ cache_cost += calculate_cache_writing_cost(
+ cache_creation_tokens=prompt_tokens_details["cache_creation_tokens"],
+ cache_creation_token_details=prompt_tokens_details[
+ "cache_creation_token_details"
+ ],
+ cache_creation_cost_above_1hr=cache_creation_cost_above_1hr,
+ cache_creation_cost=cache_creation_cost,
+ )
+
+ return cache_cost
+
+
def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
"""
Calculates the cost per token for a given model, prompt tokens, and completion tokens.
@@ -45,7 +84,8 @@ def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
multiplier *= provider_specific_entry.get("fast", 1.0)
if multiplier != 1.0:
- prompt_cost *= multiplier
+ cache_cost = _compute_cache_only_cost(model_info=model_info, usage=usage)
+ prompt_cost = (prompt_cost - cache_cost) * multiplier + cache_cost
completion_cost *= multiplier
except Exception:
pass
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index f7ce1196cf5..18cbe6d1d86 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -8489,7 +8489,7 @@
"tool_use_system_prompt_tokens": 346,
"provider_specific_entry": {
"us": 1.1,
- "fast": 1.1
+ "fast": 6.0
}
},
"claude-opus-4-6-20260205": {
@@ -8524,7 +8524,7 @@
"tool_use_system_prompt_tokens": 346,
"provider_specific_entry": {
"us": 1.1,
- "fast": 1.1
+ "fast": 6.0
}
},
"claude-sonnet-4-20250514": {
diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json
index f7ce1196cf5..18cbe6d1d86 100644
--- a/model_prices_and_context_window.json
+++ b/model_prices_and_context_window.json
@@ -8489,7 +8489,7 @@
"tool_use_system_prompt_tokens": 346,
"provider_specific_entry": {
"us": 1.1,
- "fast": 1.1
+ "fast": 6.0
}
},
"claude-opus-4-6-20260205": {
@@ -8524,7 +8524,7 @@
"tool_use_system_prompt_tokens": 346,
"provider_specific_entry": {
"us": 1.1,
- "fast": 1.1
+ "fast": 6.0
}
},
"claude-sonnet-4-20250514": {