o
    ưi>                     @   s   d Z ddlmZmZmZ ddlmZmZmZm	Z	 er"ddl
mZmZ ddlZdddd	d
efddZdedd	d
eeef fddZ		dded ded	 d
efddZdS )zU
Helper util for handling anthropic-specific cost calculation
- e.g.: prompt caching
    )TYPE_CHECKINGOptionalTuple)_get_token_base_cost_parse_prompt_tokens_detailscalculate_cache_writing_costgeneric_cost_per_token)	ModelInfoUsageN
model_infor	   usager
   returnc                 C   sp   |j du rdS t|}t| |d\}}}}}t|d | }|d s(|d dur6|t|d |d ||d7 }|S )a/  
    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.
    N        r   r   Zcache_hit_tokenscache_creation_tokenscache_creation_token_details)r   r   cache_creation_cost_above_1hrcache_creation_cost)prompt_tokens_detailsr   r   floatr   )r   r   r   _r   r   Zcache_read_cost
cache_cost r   ^/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/anthropic/cost_calculation.py_compute_cache_only_cost   s$   

	r   modelc                 C   s   t | |dd\}}z\tj| dd}|dpi }d}t|dr4|jr4|j dvr4|||j d9 }t|drF|jd	krF||d	d9 }|dkrat||d
}|| | | }||9 }W ||fS W ||fS  t	yq   Y ||fS w )aO  
    Calculates the cost per token for a given model, prompt tokens, and completion tokens.

    Input:
        - model: str, the model name without provider prefix
        - usage: LiteLLM Usage block, containing anthropic caching information

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
    Z	anthropic)r   r   custom_llm_provider)r   r   provider_specific_entryg      ?inference_geo)globalZnot_availablespeedfastr   )
r   litellmZget_model_infogethasattrr   lowerr    r   	Exception)r   r   Zprompt_costZcompletion_costr   r   
multiplierr   r   r   r   cost_per_token6   s8   


r(   c                 C   sz   ddl m} | du rdS |du s|jdu s|jjdu rdS | dp$| }|dd}|du s3|dkr5dS ||jj }|S )z@
    Get the cost of using a web search tool for Anthropic.
    r   )SearchContextCostPerQueryNr   Zsearch_context_cost_per_queryZsearch_context_size_medium)litellm.types.utilsr)   Zserver_tool_useZweb_search_requestsr#   )r   r   r)   Zsearch_context_pricingZcost_per_web_search_requestZ
total_costr   r   r   !get_cost_for_anthropic_web_search`   s    
r+   )NN)__doc__typingr   r   r   Z.litellm.litellm_core_utils.llm_cost_calc.utilsr   r   r   r   r*   r	   r
   r"   r   r   strr(   r+   r   r   r   r   <module>   s"    "+