
    iW                         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Zdedd	d
eeef   fdZ	 	 dded   ded	   d
efdZy)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                     |j                   yt        |      }t        | |      \  }}}}}t        |d         |z  }|d   s|d   |t	        |d   |d   ||      z  }|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.
            r   r   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   cache_read_cost
cache_costs           x/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/anthropic/cost_calculation.py_compute_cache_only_costr      s     ""*8? 	
%@		% ,-?@AOSJ 	56 !?@L2"78O"P)>.* +H 3
 	

     modelc                    t        | |d      \  }}	 t        j                  | d      }|j                  d      xs i }d}t	        |d      rU|j
                  rI|j
                  j                         dvr-||j                  |j
                  j                         d      z  }t	        |d      r$|j                  d	k(  r||j                  d	d      z  }|dk7  rt        ||
      }||z
  |z  |z   }||z  }||fS # t        $ r Y ||fS w xY 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
    	anthropic)r   r   custom_llm_provider)r   r"   provider_specific_entryg      ?inference_geo)globalnot_availablespeedfastr   )
r	   litellmget_model_infogethasattrr$   lowerr'   r   	Exception)r   r   prompt_costcompletion_costr   r#   
multiplierr   s           r   cost_per_tokenr2   :   s/    $:5k$ K
++[

 )37P(Q(WUW
E?+####))+3NN155e6I6I6O6O6QSVWWJ5'"u{{f'<155fcBBJ1ZuUJ&3zAJNKz)O ''  ''s   C!C8 8	DDc                     ddl m} | y|"|j                  |j                  j                  y| j	                  d      xs  |       }|j	                  dd      }||dk(  ry||j                  j                  z  }|S )z@
    Get the cost of using a web search tool for Anthropic.
    r   )SearchContextCostPerQueryr   search_context_cost_per_querysearch_context_size_medium)litellm.types.utilsr4   server_tool_useweb_search_requestsr+   )r   r   r4   search_context_pricingcost_per_web_search_request
total_costs         r   !get_cost_for_anthropic_web_searchr=   d   s     >  	  (  44< 	67V;T;V  #9"<"<$c# #*.IS.P -u/D/D/X/XXJr   )NN)__doc__typingr   r   r   .litellm.litellm_core_utils.llm_cost_calc.utilsr   r   r   r	   r7   r
   r   r)   r   r   strr2   r=    r   r   <module>rC      s   
 2 1  4 # #W # #L'(# '(g '(%u2E '(V )-# % G   r   