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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(cost): make the shared token-details parsers public
mateo-berri0176e4b3f614
diff --git a/litellm/batches/batch_utils.py b/litellm/batches/batch_utils.pyindex e73b887ae0a..baf522c0bb1 100644--- a/litellm/batches/batch_utils.py+++ b/litellm/batches/batch_utils.py@@ -5,7 +5,7 @@ from typing import Any, Final, Literalimport litellmfrom litellm._logging import verbose_logger-from litellm.litellm_core_utils.llm_cost_calc.utils import _parse_prompt_tokens_details+from litellm.litellm_core_utils.llm_cost_calc.utils import parse_prompt_tokens_detailsfrom litellm.types.llms.openai import Batchfrom litellm.types.utils import CallTypes, ModelInfo, Usagefrom litellm.utils import token_counter@@ -101,7 +101,7 @@ def _iter_successful_output_line_stats(continueresponse_body = _get_response_from_batch_job_output_file(entry, custom_llm_provider)usage = _get_batch_job_usage_from_response_body(response_body, custom_llm_provider)- prompt_details = _parse_prompt_tokens_details(usage)+ prompt_details = parse_prompt_tokens_details(usage)raw_model = response_body.get("model")response_model = raw_model if isinstance(raw_model, str) and raw_model else Noneif model_info is not None or custom_llm_provider in ("anthropic", "bedrock"):diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.pyindex eb17a53a46e..b37ff865c65 100644--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@ -26,11 +26,11 @@ from litellm.litellm_core_utils.llm_cost_calc.utils import (_generic_cost_per_character,_get_regional_uplift_multiplier,_get_service_tier_cost_key,- _parse_prompt_tokens_details,calculate_cost_component,generic_cost_per_token,get_billable_input_tokens,get_token_type_cost_breakdown,+ parse_prompt_tokens_details,select_cost_metric_for_model,)from litellm.llms.anthropic.cost_calculation import (@@ -2163,7 +2163,7 @@ def batch_cost_calculator(if input_cost_per_token_batches:total_prompt_cost = usage.prompt_tokens * input_cost_per_token_batcheselif input_cost_per_token:- details: Final = _parse_prompt_tokens_details(usage)+ details: Final = parse_prompt_tokens_details(usage)cache_read_tokens: Final = details["cache_hit_tokens"]cache_creation_tokens: Final = details["cache_creation_tokens"]diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.pyindex 440447809de..4a33424e88f 100644--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py@@ -99,7 +99,7 @@ def get_billable_input_tokens(usage: Usage) -> int:Returns the number of billable input tokens.Subtracts cached tokens from prompt tokens if applicable."""- details: Final = _parse_prompt_tokens_details(usage)+ details: Final = parse_prompt_tokens_details(usage)return usage.prompt_tokens - details["cache_hit_tokens"]@@ -519,7 +519,7 @@ class PromptTokensDetailsResult(TypedDict):audio_length_seconds: float-def _parse_prompt_tokens_details(usage: Usage) -> PromptTokensDetailsResult:+def parse_prompt_tokens_details(usage: Usage) -> PromptTokensDetailsResult:cache_hit_tokens: Final = cast(int | None, getattr(usage.prompt_tokens_details, "cached_tokens", 0)) or 0cache_creation_tokens: Final = (cast(@@ -589,7 +589,7 @@ class CompletionTokensDetailsResult(TypedDict):video_tokens: int-def _parse_completion_tokens_details(usage: Usage) -> CompletionTokensDetailsResult:+def parse_completion_tokens_details(usage: Usage) -> CompletionTokensDetailsResult:audio_tokens: Final = (cast(int | None,@@ -826,7 +826,7 @@ def generic_cost_per_token(audio_length_seconds=0.0,)if usage.prompt_tokens_details:- prompt_tokens_details = _parse_prompt_tokens_details(usage)+ prompt_tokens_details = parse_prompt_tokens_details(usage)## EDGE CASE - text tokens not set or includes cached tokens (double-counting)## Some providers (like xAI) report text_tokens = prompt_tokens (including cached)@@ -881,7 +881,7 @@ def generic_cost_per_token(video_tokens = 0is_text_tokens_total = Falseif usage.completion_tokens_details is not None:- completion_tokens_details: Final = _parse_completion_tokens_details(usage)+ completion_tokens_details: Final = parse_completion_tokens_details(usage)audio_tokens = completion_tokens_details["audio_tokens"]text_tokens = completion_tokens_details["text_tokens"]reasoning_tokens = completion_tokens_details["reasoning_tokens"]@@ -1006,9 +1006,7 @@ def get_token_type_cost_breakdown()reasoning_tokens = (- _parse_completion_tokens_details(usage)["reasoning_tokens"]- if usage.completion_tokens_details is not None- else 0+ parse_completion_tokens_details(usage)["reasoning_tokens"] if usage.completion_tokens_details is not None else 0)if not reasoning_tokens:reasoning_tokens = _coerce_token_count(getattr(usage, "reasoning_tokens", 0))@@ -1030,7 +1028,7 @@ def get_token_type_cost_breakdown(cache_creation_tokens = 0cache_creation_token_details: CacheCreationTokenDetails | None = Noneif usage.prompt_tokens_details is not None:- prompt_tokens_details: Final = _parse_prompt_tokens_details(usage)+ prompt_tokens_details: Final = parse_prompt_tokens_details(usage)cache_read_tokens = prompt_tokens_details["cache_hit_tokens"]cache_creation_tokens = prompt_tokens_details["cache_creation_tokens"]cache_creation_token_details = prompt_tokens_details["cache_creation_token_details"]diff --git a/litellm/llms/anthropic/cost_calculation.py b/litellm/llms/anthropic/cost_calculation.pyindex e792f69622c..7bb3e0294f0 100644--- a/litellm/llms/anthropic/cost_calculation.py+++ b/litellm/llms/anthropic/cost_calculation.py@@ -10,10 +10,10 @@ from pydantic import BaseModel, ValidationErrorfrom litellm.litellm_core_utils.llm_cost_calc.utils import (_get_token_base_cost,_get_web_search_requests,- _parse_prompt_tokens_details,calculate_cache_writing_cost,generic_cost_per_token,get_provider_specific_geo_multiplier,+ parse_prompt_tokens_details,)if TYPE_CHECKING:@@ -33,7 +33,7 @@ def _compute_cache_only_cost(model_info: "ModelInfo", usage: "Usage", service_tiif usage.prompt_tokens_details is None:return 0.0- prompt_tokens_details: Final = _parse_prompt_tokens_details(usage)+ prompt_tokens_details: Final = parse_prompt_tokens_details(usage)(_,_,diff --git a/litellm/llms/dashscope/cost_calculator.py b/litellm/llms/dashscope/cost_calculator.pyindex ea6d50f5b00..e22d3e06be1 100644--- a/litellm/llms/dashscope/cost_calculator.py+++ b/litellm/llms/dashscope/cost_calculator.py@@ -12,8 +12,8 @@ from typing import Finalfrom litellm.litellm_core_utils.llm_cost_calc.tiered_pricing import select_tier_for_input, tier_ratefrom litellm.litellm_core_utils.llm_cost_calc.utils import (- _parse_completion_tokens_details,- _parse_prompt_tokens_details,+ parse_completion_tokens_details,+ parse_prompt_tokens_details,)from litellm.types.utils import ModelInfo, Usagefrom litellm.utils import get_model_info@@ -33,12 +33,12 @@ class TokenBreakdown:def _extract_token_breakdown(usage: Usage) -> TokenBreakdown:- prompt_details: Final = _parse_prompt_tokens_details(usage)+ prompt_details: Final = parse_prompt_tokens_details(usage)cached_tokens: Final = prompt_details["cache_hit_tokens"]cache_creation_tokens: Final = prompt_details["cache_creation_tokens"]text_tokens: Final = max(usage.prompt_tokens - cached_tokens - cache_creation_tokens, 0)- reasoning_tokens: Final = _parse_completion_tokens_details(usage)["reasoning_tokens"]+ reasoning_tokens: Final = parse_completion_tokens_details(usage)["reasoning_tokens"]completion_tokens: Final = max((usage.completion_tokens or 0) - reasoning_tokens, 0)return TokenBreakdown(