Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions litellm/llms/anthropic/cost_calculation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Optional, Tuple

from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token
from litellm.types.utils import SearchContextCostPerQuery

if TYPE_CHECKING:
from litellm.types.utils import ModelInfo, Usage
Expand All @@ -22,9 +23,7 @@ def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
Returns:
Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
"""
return generic_cost_per_token(
model=model, usage=usage, custom_llm_provider="anthropic"
)
return generic_cost_per_token(model=model, usage=usage, custom_llm_provider="anthropic")


def get_cost_for_anthropic_web_search(
Expand All @@ -34,29 +33,21 @@ def get_cost_for_anthropic_web_search(
"""
Get the cost of using a web search tool for Anthropic.
"""
from litellm.types.utils import SearchContextCostPerQuery

## Check if web search requests are in the usage object
if model_info is None:
return 0.0

if (
usage is None
model_info is None
or usage is None
or usage.server_tool_use is None
or usage.server_tool_use.web_search_requests is None
):
return 0.0

## Get the cost per web search request
# Get the cost per web search request
search_context_pricing: SearchContextCostPerQuery = (
model_info.get("search_context_cost_per_query") or SearchContextCostPerQuery()
)
cost_per_web_search_request = search_context_pricing.get(
"search_context_size_medium", 0.0
)
if cost_per_web_search_request is None or cost_per_web_search_request == 0.0:
cost_per_web_search_request = search_context_pricing.get("search_context_size_medium", 0.0)
if not cost_per_web_search_request:
return 0.0

## Calculate the total cost
total_cost = cost_per_web_search_request * usage.server_tool_use.web_search_requests
return total_cost
# Calculate the total cost
return cost_per_web_search_request * usage.server_tool_use.web_search_requests
13 changes: 3 additions & 10 deletions litellm/llms/gemini/cost_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from typing import TYPE_CHECKING, Tuple
from litellm.types.utils import PromptTokensDetailsWrapper

if TYPE_CHECKING:
from litellm.types.utils import ModelInfo, Usage
Expand All @@ -18,22 +19,16 @@ def cost_per_token(model: str, usage: "Usage") -> Tuple[float, float]:
"""
from litellm.litellm_core_utils.llm_cost_calc.utils import generic_cost_per_token

return generic_cost_per_token(
model=model, usage=usage, custom_llm_provider="gemini"
)
return generic_cost_per_token(model=model, usage=usage, custom_llm_provider="gemini")


def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> float:
"""
Calculates the cost per web search request for a given model, prompt tokens, and completion tokens.
"""
from litellm.types.utils import PromptTokensDetailsWrapper

# cost per web search request
cost_per_web_search_request = 35e-3

number_of_web_search_requests = 0
# Get number of web search requests
if (
usage is not None
and usage.prompt_tokens_details is not None
Expand All @@ -46,6 +41,4 @@ def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> floa
number_of_web_search_requests = 0

# Calculate total cost
total_cost = cost_per_web_search_request * number_of_web_search_requests

return total_cost
return cost_per_web_search_request * number_of_web_search_requests
8 changes: 1 addition & 7 deletions litellm/llms/vertex_ai/gemini/cost_calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""

from typing import TYPE_CHECKING
from litellm.types.utils import PromptTokensDetailsWrapper

if TYPE_CHECKING:
from litellm.types.utils import ModelInfo, Usage
Expand All @@ -25,21 +26,14 @@ def cost_per_web_search_request(usage: "Usage", model_info: "ModelInfo") -> floa
Returns:
The cost of the web search request.
"""
from litellm.types.utils import PromptTokensDetailsWrapper

# check if usage object has web search requests
cost_per_llm_call_with_web_search = 35e-3

makes_web_search_request = False
if (
usage is not None
and usage.prompt_tokens_details is not None
and isinstance(usage.prompt_tokens_details, PromptTokensDetailsWrapper)
):
makes_web_search_request = True

# Calculate total cost
if makes_web_search_request:
return cost_per_llm_call_with_web_search
else:
return 0.0