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]

refactor(cost-calc): move cache token extractors out of proxy savings (#45771)

nate-berri8d510a6b1bdd
diff --git a/litellm/integrations/datadog/datadog_llm_obs.py b/litellm/integrations/datadog/datadog_llm_obs.py
index 9565a64e69c..6793838cea0 100644
--- a/litellm/integrations/datadog/datadog_llm_obs.py
+++ b/litellm/integrations/datadog/datadog_llm_obs.py
@@ -33,6 +33,10 @@ from litellm.integrations.datadog.datadog_mock_client import (
should_use_datadog_mock,
)
from litellm.litellm_core_utils.dd_tracing import tracer
+from litellm.litellm_core_utils.llm_cost_calc.cache_tokens import (
+ extract_cache_creation_tokens,
+ extract_cache_read_tokens,
+)
from litellm.litellm_core_utils.prompt_templates.common_utils import (
convert_content_list_to_str,
handle_any_messages_to_chat_completion_str_messages_conversion,
@@ -44,7 +48,6 @@ from litellm.llms.custom_httpx.http_handler import (
get_async_httpx_client,
httpxSpecialProvider,
)
-from litellm.proxy.spend_tracking.savings import extract_cache_creation_tokens, extract_cache_read_tokens
from litellm.types.integrations.datadog_llm_obs import *
from litellm.types.utils import (
AUDIT_GUARDRAIL_FIELDS,
diff --git a/litellm/litellm_core_utils/llm_cost_calc/cache_tokens.py b/litellm/litellm_core_utils/llm_cost_calc/cache_tokens.py
new file mode 100644
index 00000000000..f3e83580181
--- /dev/null
+++ b/litellm/litellm_core_utils/llm_cost_calc/cache_tokens.py
@@ -0,0 +1,49 @@
+from collections.abc import Mapping
+from typing import Final
+
+
+def extract_cache_read_tokens(usage_object: Mapping[str, object] | None) -> int:
+ """Cache-read tokens from a logged usage object, whatever shape recorded them.
+
+ Anthropic writes a top-level ``cache_read_input_tokens``; OpenAI-compatible
+ providers (moonshotai, openai, deepseek, etc.) write
+ ``prompt_tokens_details.cached_tokens``. This is the one owner of that
+ normalization: callers hand over the usage object rather than threading a
+ count that could disagree with it.
+ """
+ if not usage_object:
+ return 0
+ explicit: Final = usage_object.get("cache_read_input_tokens")
+ if isinstance(explicit, (int, float)) and explicit:
+ return int(explicit)
+ details: Final = usage_object.get("prompt_tokens_details")
+ if not isinstance(details, Mapping):
+ return 0
+ cached: Final = details.get("cached_tokens")
+ return int(cached) if isinstance(cached, (int, float)) else 0
+
+
+def extract_cache_creation_tokens(usage_object: Mapping[str, object] | None) -> int:
+ """Cache-write tokens from a logged usage object, whatever shape recorded them.
+
+ Anthropic writes a top-level ``cache_creation_input_tokens``; OpenAI-compatible
+ providers (kimi-k2 etc.) write ``prompt_tokens_details.cache_write_tokens`` or
+ ``prompt_tokens_details.cache_creation_tokens``.
+ """
+ if not usage_object:
+ return 0
+ explicit: Final = usage_object.get("cache_creation_input_tokens")
+ if isinstance(explicit, (int, float)) and explicit:
+ return int(explicit)
+ details: Final = usage_object.get("prompt_tokens_details")
+ if not isinstance(details, Mapping):
+ return 0
+ written: Final = next(
+ (
+ value
+ for value in (details.get("cache_write_tokens"), details.get("cache_creation_tokens"))
+ if isinstance(value, (int, float)) and value
+ ),
+ 0,
+ )
+ return int(written)
diff --git a/litellm/proxy/spend_tracking/savings.py b/litellm/proxy/spend_tracking/savings.py
index e1e25f26ea8..bca91d710e1 100644
--- a/litellm/proxy/spend_tracking/savings.py
+++ b/litellm/proxy/spend_tracking/savings.py
@@ -415,51 +415,12 @@ def marks_gateway_injection(metadata: Mapping[str, object] | None, model_id: str
return injected_deployment in (GATEWAY_INJECTED_FOR_EVERY_DEPLOYMENT, model_id)
-def extract_cache_read_tokens(usage_object: Mapping[str, object] | None) -> int:
- """Cache-read tokens from a logged usage object, whatever shape recorded them.
-
- Anthropic writes a top-level ``cache_read_input_tokens``; OpenAI-compatible
- providers (moonshotai, openai, deepseek, etc.) write
- ``prompt_tokens_details.cached_tokens``. This is the one owner of that
- normalization: callers hand over the usage object rather than threading a
- count that could disagree with it.
- """
- if not usage_object:
- return 0
- explicit: Final = usage_object.get("cache_read_input_tokens")
- if isinstance(explicit, (int, float)) and explicit:
- return int(explicit)
- details: Final = usage_object.get("prompt_tokens_details")
- if not isinstance(details, Mapping):
- return 0
- cached: Final = details.get("cached_tokens")
- return int(cached) if isinstance(cached, (int, float)) else 0
-
-
-def extract_cache_creation_tokens(usage_object: Mapping[str, object] | None) -> int:
- """Cache-write tokens from a logged usage object, whatever shape recorded them.
-
- Anthropic writes a top-level ``cache_creation_input_tokens``; OpenAI-compatible
- providers (kimi-k2 etc.) write ``prompt_tokens_details.cache_write_tokens`` or
- ``prompt_tokens_details.cache_creation_tokens``.
- """
- if not usage_object:
- return 0
- explicit: Final = usage_object.get("cache_creation_input_tokens")
- if isinstance(explicit, (int, float)) and explicit:
- return int(explicit)
- details: Final = usage_object.get("prompt_tokens_details")
- if not isinstance(details, Mapping):
- return 0
- written: Final = next(
- (
- value
- for value in (details.get("cache_write_tokens"), details.get("cache_creation_tokens"))
- if isinstance(value, (int, float)) and value
- ),
- 0,
- )
- return int(written)
+from litellm.litellm_core_utils.llm_cost_calc.cache_tokens import ( # noqa: E402 # re-export at the old path for existing importers
+ extract_cache_creation_tokens as extract_cache_creation_tokens, # noqa: PLC0414 # explicit re-export marker
+)
+from litellm.litellm_core_utils.llm_cost_calc.cache_tokens import ( # noqa: E402 # re-export at the old path for existing importers
+ extract_cache_read_tokens as extract_cache_read_tokens, # noqa: PLC0414 # explicit re-export marker
+)
def _proxy_llm_router() -> "Router | None":