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]

Merge origin/litellm_internal_staging into litellm_lit6874_fireworks_perplexity_off_peak_pricing

mateo-berri656d81e84d45
diff --cc litellm/llms/fireworks_ai/cost_calculator.py
index 3843bad6d8f,08e6f009010..3c43075d940
--- a/litellm/llms/fireworks_ai/cost_calculator.py
+++ b/litellm/llms/fireworks_ai/cost_calculator.py
@@@ -12,12 -10,9 +12,12 @@@ from litellm.constants import
FIREWORKS_AI_56_B_MOE,
FIREWORKS_AI_176_B_MOE,
)
- from litellm.litellm_core_utils.llm_cost_calc.utils import apply_off_peak_pricing
-from litellm.types.utils import Usage
++from litellm.litellm_core_utils.llm_cost_calc.utils import TokenRates, apply_off_peak_pricing
+from litellm.types.utils import ModelInfo, Usage
from litellm.utils import get_model_info
+NO_CACHE_READ_RATE: Final = float("nan")
+
# Extract the number of billion parameters from the model name
# only used for together_computer LLMs
@@@ -80,25 -65,33 +80,29 @@@ def cost_per_token(model: str, usage: U
Returns:
Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
"""
- ## check if model mapped, else use default pricing
- try:
- model_info = get_model_info(model=model, custom_llm_provider="fireworks_ai")
- except Exception:
- base_model: Final = get_base_model_for_pricing(model_name=model)
-
- ## GET MODEL INFO
- model_info = get_model_info(model=base_model, custom_llm_provider="fireworks_ai")
+ model_info: Final = _resolve_model_info(model)
+ standard_cache_read_rate: Final = model_info.get("cache_read_input_token_cost")
- input_rate, output_rate, cache_read_rate_or_unset = apply_off_peak_pricing(
++ rates: Final = apply_off_peak_pricing(
+ model_info,
+ current_time,
- model_info["input_cost_per_token"] or 0.0,
- model_info["output_cost_per_token"] or 0.0,
- standard_cache_read_rate if standard_cache_read_rate is not None else NO_CACHE_READ_RATE,
++ TokenRates(
++ input_rate=model_info["input_cost_per_token"] or 0.0,
++ output_rate=model_info["output_cost_per_token"] or 0.0,
++ cache_read_rate=standard_cache_read_rate if standard_cache_read_rate is not None else NO_CACHE_READ_RATE,
++ cache_creation_rate=0.0,
++ reasoning_rate=None,
++ ),
+ )
- cache_read_rate: Final[float] = input_rate if math.isnan(cache_read_rate_or_unset) else cache_read_rate_or_unset
++ cache_read_rate: Final[float] = rates.input_rate if math.isnan(rates.cache_read_rate) else rates.cache_read_rate
- ## CALCULATE INPUT COST
prompt_tokens_details: Final = usage.prompt_tokens_details
cached_tokens: Final[int] = (
prompt_tokens_details.cached_tokens
if prompt_tokens_details is not None and prompt_tokens_details.cached_tokens is not None
else 0
)
- input_cost_per_token: Final[float] = model_info["input_cost_per_token"] or 0.0
- cache_read_input_token_cost: Final = model_info.get("cache_read_input_token_cost")
- cache_read_cost_per_token: Final[float] = (
- cache_read_input_token_cost if cache_read_input_token_cost is not None else input_cost_per_token
- )
non_cached_prompt_tokens: Final[int] = max(usage.prompt_tokens - cached_tokens, 0)
- prompt_cost: Final[float] = non_cached_prompt_tokens * input_rate + cached_tokens * cache_read_rate
- completion_cost: Final[float] = usage.completion_tokens * output_rate
-
- prompt_cost: float = non_cached_prompt_tokens * input_cost_per_token + cached_tokens * cache_read_cost_per_token
-
- ## CALCULATE OUTPUT COST
- output_cost_per_token: Final[float] = model_info["output_cost_per_token"] or 0.0
- completion_cost: Final[float] = usage.completion_tokens * output_cost_per_token
++ prompt_cost: Final[float] = non_cached_prompt_tokens * rates.input_rate + cached_tokens * cache_read_rate
++ completion_cost: Final[float] = usage.completion_tokens * rates.output_rate
return prompt_cost, completion_cost
diff --cc litellm/llms/perplexity/cost_calculator.py
index 67949e850f0,27835ecbfe8..2d762adf958
--- a/litellm/llms/perplexity/cost_calculator.py
+++ b/litellm/llms/perplexity/cost_calculator.py
@@@ -3,10 -3,8 +3,10 @@@ Helper util for handling perplexity-spe
- e.g.: citation tokens, search queries
"""
+from datetime import datetime
from typing import Final
- from litellm.litellm_core_utils.llm_cost_calc.utils import apply_off_peak_pricing
++from litellm.litellm_core_utils.llm_cost_calc.utils import TokenRates, apply_off_peak_pricing
from litellm.types.utils import Usage
from litellm.utils import get_model_info
@@@ -52,15 -48,8 +52,21 @@@ def cost_per_token(model: str, usage: U
except (ValueError, TypeError):
return default
- input_cost_per_token, output_cost_per_token, _ = apply_off_peak_pricing(
++ rates: Final = apply_off_peak_pricing(
+ model_info,
+ current_time,
- _safe_float_cast(model_info.get("input_cost_per_token")),
- _safe_float_cast(model_info.get("output_cost_per_token")),
- 0.0,
++ TokenRates(
++ input_rate=_safe_float_cast(model_info.get("input_cost_per_token")),
++ output_rate=_safe_float_cast(model_info.get("output_cost_per_token")),
++ cache_read_rate=0.0,
++ cache_creation_rate=0.0,
++ reasoning_rate=None,
++ ),
+ )
++ input_cost_per_token: Final = rates.input_rate
++ output_cost_per_token: Final = rates.output_rate
+
## CALCULATE INPUT COST
- input_cost_per_token: Final = _safe_float_cast(model_info.get("input_cost_per_token"))
prompt_cost: float = (usage.prompt_tokens or 0) * input_cost_per_token
## ADD CITATION TOKENS COST (if present)