
    ^h              #           S SK r S SKJrJrJrJrJrJr  S SKJ	r	  S SK
r
S SKr
S SK
Jr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S S	KJr  S S
KJr  S S	K Jr!  S SK"Jr#  S SK$Jr%  S SK&J'r(  S SK&Jr)  S SK*J+r+  S S
K,Jr-  S SK.J/r/  S SK0J1r1  S SK2J3r3J4r4J5r5  S SK6J7r7J8r8J9r9J:r:J;r;J<r<J=r=J>r>J?r?       S?S\@S\@S\\@   S\\8   S\\@   S\\\@\@4      4S jjrA                S@S\BS\CS\CS\\@   S\\B   S\\C   S\\C   S\\C   S\\C   S\\8   S\\@   S \\C   S!\\5   S"\3S#\@S\\@\@4   4 S$ jjrSAS%\D4S& jjrES'\S\F4S( jrG SBS\\B   S\\B   S\\B   4S) jjrH   SCS\\B   S%\\   S*\\B   S+\\F   S\\B   S\\B   4S, jjrIS%\S\\5   4S- jrJS"\\3   S%\S\\3   4S. jrKSSS/ SSSSSSSSSSSSS4S\\B   S/\S0\\@   S"\\3   S1\\B   S2\\B   S3\\C   S\\8   S\\@   S4\\D   S+\\F   S*\\B   S\@4S5 jjrL    SDS6\\;\9\:\=\<\/\14   S\BS\\B   S"\S7   S4\DS8\\F   S*\\B   S+\\F   S9\BS\\@   4S: jjrMS\BS\\B   S\\@\@4   4S; jrNS\BS\\B   S<\@S\\@\@4   4S= jrO     SES\BS\\B   S2\\B   S3\\C   S1\\B   S4\\D   S\@4S> jjrPg)F    N)AnyListLiteralOptionalTupleUnion)	BaseModel)verbose_logger)cost_per_character)cost_per_token)cost_router)_generic_cost_per_character)cost_per_query)cost_calculator)cost_per_second)get_model_params_and_category)HttpxBinaryResponseContent)RerankResponse)CallTypesLiteralPassthroughCallTypesUsage)		CallTypesCostPerTokenEmbeddingResponseImageResponseModelResponseTextCompletionResponseTranscriptionResponseprint_verbosetoken_counter        prompt_tokenscompletion_tokensresponse_time_mscustom_cost_per_tokencustom_cost_per_secondreturnc                 Z    Uc  Uc  gUb  US   U -  nUS   U-  nXV4$ Ub  XB-  S-  nSU4$ g)zFInternal helper function for calculating cost, if custom pricing givenNinput_cost_per_tokenoutput_cost_per_token  r    )r"   r#   r$   r%   r&   
input_costoutput_costs          Q/home/james-whalen/.local/lib/python3.13/site-packages/litellm/cost_calculator.py%_cost_per_token_custom_pricing_helperr0   B   sc     $)?)G(*+AB]R
+,CDGXX&&		+,?$F+~     modelcustom_llm_providerprompt_characterscompletion_characterscache_creation_input_tokenscache_read_input_tokensnumber_of_queriesusage_object	call_type!audio_transcription_file_durationc           
         U c  [        S5      eUb  UnO[        UUX-   UU	S9n[        UUUUU
S9nUb
  US   US   4$ SnSn[        R                  nU nUb  US-   U -   nUb  U SU SU  3nUU;   a  UnO[        R
                  " U S9u  nn  nU nU R                  SS5      n[        U5      S:  a  US   nOU n UU;   a  Un OU U;   a  U n OUU;   a  Un US	:X  d  US
:X  aZ  Uc  [        SR                  UU UU5      5      e[        UUUSSSS9u  nnUb  Uc  [        SR                  UUUUUU5      5      eUU4$ US:X  d  US:X  a
  [        U US9$ US:X  d  US:X  a  [        U UUS9$ US:X  a2  [        UUUS9nUS:X  a  [        UUUUUUS9$ US:X  a  [        UUUUS9$ gUS:X  a
  [!        U US9$ US:X  a
  [#        U US9$ US:X  a
  [%        U US9$ US:X  a
  ['        U US9$ US:X  a  [)        U UUS 9$ US!:X  a  [        UUUUS9$ [        R*                  " XS9nUS"   S:  a	  US"   U-  nODUR-                  S#S5      b1  Ub.  [/        S$U  S%UR-                  S#5       S&U 35        US#   U-  S'-  nUS(   S:  a	  US(   U-  nODUR-                  S)S5      b1  Ub.  [/        S$U  S*UR-                  S)5       S&U 35        US)   U-  S'-  n[/        S+U  S,U S-U 35        UU4$ ).a6  
Calculates the cost per token for a given model, prompt tokens, and completion tokens.

Parameters:
    model (str): The name of the model to use. Default is ""
    prompt_tokens (int): The number of tokens in the prompt.
    completion_tokens (int): The number of tokens in the completion.
    response_time (float): The amount of time, in milliseconds, it took the call to complete.
    prompt_characters (float): The number of characters in the prompt. Used for vertex ai cost calculation.
    completion_characters (float): The number of characters in the completion response. Used for vertex ai cost calculation.
    custom_llm_provider (str): The llm provider to whom the call was made (see init.py for full list)
    custom_cost_per_token: Optional[CostPerToken]: the cost per input + output token for the llm api call.
    custom_cost_per_second: Optional[float]: the cost per second for the llm api call.
    call_type: Optional[str]: the call type

Returns:
    tuple: A tuple containing the cost in USD dollars for prompt tokens and completion tokens, respectively.
Nz"Invalid arg. Model cannot be none.)r"   r#   total_tokensr7   r8   )r"   r#   r$   r&   r%   r      /r3   speechaspeechzvprompt_characters must be provided for tts calls. prompt_characters={}, model={}, custom_llm_provider={}, call_type={})r3   r4   r5   r6   custom_prompt_costcustom_completion_costzcost for tts call is None. prompt_cost={}, completion_cost={}, model={}, custom_llm_provider={}, prompt_characters={}, completion_characters={}arerankrerankr3   r4   atranscriptiontranscriptionr3   r4   duration	vertex_ai)r3   r4   r;   r   )r3   r4   r5   r6   r"   r#   r   )r3   r4   r"   r#   	anthropic)r3   usageopenai
databricksfireworks_aiazure)r3   rO   r$   geminir)   input_cost_per_secondz
For model=z - input_cost_per_second: z; response time: r+   r*   output_cost_per_secondz - output_cost_per_second: zReturned custom cost for model=z" - prompt_tokens_cost_usd_dollar: z%, completion_tokens_cost_usd_dollar: )	Exceptionr   r0   litellm
model_costget_llm_providersplitlen
ValueErrorformatr   rerank_costopenai_cost_per_secondgoogle_cost_routergoogle_cost_per_charactergoogle_cost_per_tokenanthropic_cost_per_tokenopenai_cost_per_tokendatabricks_cost_per_tokenfireworks_ai_cost_per_tokenazure_openai_cost_per_tokenget_model_infogetr   )r3   r"   r#   r$   r4   region_namer5   r6   r7   r8   r%   r&   r9   r:   r;   r<   usage_blockresponse_costprompt_tokens_cost_usd_dollar!completion_tokens_cost_usd_dollarmodel_cost_refmodel_with_providermodel_with_provider_and_region_model_without_prefixmodel_partsprompt_costcompletion_costr   
model_infos                                 r/   r   r   Y   s   T }<== "'/&:(C$;
 :#+)53M  Qq!111 ,-!/0%''N&1C7%?"&'qQug> + /.@&D#'.'?'?e'L$1 ++c1%K
;!*1~$ 	~-#	.	 .$ H	Y 6$ I  P  P%'	  (C& 3/"###$(
$_ /"9 b  i  i#('%)	 	 O++	i	9#8 3
 	
 
&	&)*F% 36
 	

 
	+(& 3

 ..,*$7"3&;+"3  ,,(*$7+"3	  - 
	+'e;GG		($5DD		,(uKHH		.*kJJ		'*{=M
 	
 
	($& 3'/	
 	
 ++

 ,-1 12]B * NN2D9E ,UG#=jnnMd>e=ffw  yI  xJ  K
 236FFM * -.2236GG . NN3T:F ,UG#>z~~Nf?g>hhy  {K  zL  M
 347GG$N . 	-eW4VWtVu  v[  \}  [~  	
 -.OOOr1   completion_responsec                     SnUS:X  aI  U R                  S[        R                  " 5       5      n[        U S[        R                  " 5       5      nXC-
  nX!-  S-  $ )NgǺV?r!   createdendedr+   )rj   timegetattr)ry   
total_time!a100_80gb_price_per_second_public
start_timeend_times        r/    get_replicate_completion_pricingr   E  sY     	 & S(,,Y		D
.E*
,9D@@r1   objc                     [        U S5      $ )N_hidden_params)hasattr)r   s    r/   has_hidden_paramsr   S  s    3())r1   c                     Ub  U$ U c  g  [         R                  " U S9u  p!  nU$ ! [         a,  n[        R                  " S[        U5       35         S nAg S nAff = f)NrA   zblitellm.cost_calculator.py::_get_provider_for_cost_calc() - Error inferring custom_llm_provider - )rX   rZ   rW   r
   debugstr)r3   r4   rs   es       r/   _get_provider_for_cost_calcr   W  ss     &""}'.'?'?e'L$1   pqtuvqwpxy	
 	s   % 
A"AA
base_modelcustom_pricingc                 D   SnSn[        XS9nUSL a  U nUb  UnSnUb  [        U[        5      (       a  [        USS5      n[        USS5      nUc:  Ub7  UR	                  SS5      b$  [        US   5      S:  a  UR	                  SU 5      nUb%  UR	                  SS5      b  UR	                  SS5      nO*Ub'  [        U[        5      (       a  UR	                  SS5      nUc  Ub  UnUc  U b  U nUb/  Ub,  UR                  U5      (       d  Ub  U SU SU 3nU$ U SU 3nU$ )	z
1. If custom pricing is true, return received model name
2. If base_model is set (e.g. for azure models), return that
3. If completion response has model set return that
4. Check if model is passed in return that
NrH   Tr3   r   r   rk   r@   )r   
isinstancer	   r~   rj   r\   dict
startswith)	r3   ry   r   r   r4   return_modelrk   completion_response_modelhidden_paramss	            r/    _select_model_name_for_cost_calcr   j  s    #'L!%K5 !/3&:6I9+U+U$+,?$$O! 35EtL$,1J!!'40<g./!3,00%@%!!-6B'++M4@K		(Z8KT-R-R$7$;$;GT$J! 9 E0 1 	 +''(;<<"12!K=,PL  22!L>BLr1   c                 \    S nU b&  [        U [        5      (       a  U R                  S5      nU$ )NrO   )r   r   rj   )ry   	usage_objs     r/   _get_usage_objectr     s8     "&I&:], , (++G4	r1   c                 L   U b  U $ Uc  g [        U[        5      (       a  g[        U[        5      (       a  g[        U[        5      (       a  g[        U[        5      (       a  g[        U[
        5      (       a  g[        U[        5      (       a  g[        U[        5      (       a  gU $ )N
completion	embeddingrJ   rB   rG   image_generationtext_completion)r   r   r   r   r   r   r   r   )r;   ry   s     r/   _infer_call_typer     s     "%}55	'):	;	;	')>	?	?	')C	D	D	'	8	8	'	7	7!	')?	@	@ r1   messagesr   sizequalitynoptional_paramsc                 h    [        X`5      =(       d    SnUS:X  d  US:X  a/  Ub,  [        U[        5      (       a  [        U5      S:X  a  US:X  a  SnSnSnSnSnSnSnSn[	        U S	9n[        UU UUUS
9nU Gb)  [        U [        5      (       d  [        U [        5      (       Ga  U R                  S0 5      n[        U[        5      (       aN  [        U[        R                  5      (       d/  [        U S[        R                  " S70 UR                  5       D65        Uc  0 nO([        U[        5      (       a  UR                  5       nOUnUR                  SS5      nUR                  SS5      nUR                  SS5      nUR                  SS5      nSU;   a7  US   0 :w  a.  US   (       a$  UR                  S0 5      nUR                  SS5      n[        U SS5      n[        R                  " S[        U SS5       S35        [        U SS5      nUb  UR                  SU=(       d    S5      nUR                  SU5      nUR                  S0 5      R                  SS5      n	UR                  S0 5      R                  SS5      n
UR                  S0 5      R                  SS5      nONUc  [!        SU  S U 35      e[        U5      S:  a
  [#        XS!9nO[        U5      S:  a	  [#        XS"9n[#        XS"9nUc  [!        SU  S U 35      e [        R$                  " US#9u  p  nU[*        R,                  R.                  :X  d<  U[*        R0                  R.                  :X  d  U[2        R4                  R.                  :X  ag  US%:X  a!  [        U [6        5      (       a
  [9        UU S&9$ GOUS':X  a,  [        U [6        5      (       a  [;        UU	U US(9$ [=        S)5      e[?        UU
UUU	US*9$ U[*        R@                  R.                  :X  d  U[*        RB                  R.                  :X  a  [        RD                  RG                  US+9nOU[*        RH                  R.                  :X  d  U[*        RJ                  R.                  :X  a  [        U S,S5      nOU[*        RL                  R.                  :X  d  U[*        RN                  R.                  :X  aa  U b^  [        U [P        5      (       aI  U RR                  nUb  UR                  S-0 5      =(       d    0 n O0 n U R                  S.5      =(       d    Sn!U!nS/U;   d  S0U;   d  US0:X  a  [U        U[+        U5      S19nO9U[        RV                  ;   d  S2U;   a  U[        RX                  ;  a  [[        X5      $ Uc  [!        SU  S U 35      eUb  US%:X  a  [        U5      S:  a=  [        RD                  R]                  S3U0SS49n"[        RD                  RG                  U"S+9nU bO  [        U [^        5      (       a:  [        RD                  Ra                  U S59n#[        RD                  RG                  U#S+9n[c        UUUUUUUUUUUUUUUS69u  n$n%U$U%-   n&U&$ ! [&         a9  n[        R                  " S$R)                  [        U5      5      5         SnAGN{SnAff = f! [&         a  nUeSnAff = f)8aW  
Calculate the cost of a given completion call fot GPT-3.5-turbo, llama2, any litellm supported llm.

Parameters:
    completion_response (litellm.ModelResponses): [Required] The response received from a LiteLLM completion request.

    [OPTIONAL PARAMS]
    model (str): Optional. The name of the language model used in the completion calls
    prompt (str): Optional. The input prompt passed to the llm
    completion (str): Optional. The output completion text from the llm
    total_time (float, int): Optional. (Only used for Replicate LLMs) The total time used for the request in seconds
    custom_cost_per_token: Optional[CostPerToken]: the cost per input + output token for the llm api call.
    custom_cost_per_second: Optional[float]: the cost per second for the llm api call.

Returns:
    float: The cost in USD dollars for the completion based on the provided parameters.

Exceptions:
    Raises exception if model not in the litellm model cost map. Register model, via custom pricing or PR - https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json


Note:
    - If completion_response is provided, the function extracts token information and the model name from it.
    - If completion_response is not provided, the function calculates token counts based on the model and input text.
    - The cost is calculated based on the model, prompt tokens, and completion tokens.
    - For certain models containing "togethercomputer" in the name, prices are based on the model size.
    - For un-mapped Replicate models, the cost is calculated based on the total time used for the request.
r   aimage_generationr   Nr   rS   zdall-e-2r!   )ry   )r3   ry   r4   r   r   rO   r"   r#   r7   r8   prompt_tokens_detailscached_tokens_response_msz!completion_response response ms:  r   r4   rk   r   r   1024-x-1024r   standardr   r?   z[Model is None and does not exist in passed completion_response. Passed completion_response=z, model=)r3   r   )r3   textrA   zXlitellm.cost_calculator.py::completion_cost() - Error inferring custom_llm_provider - {}rM   )r3   image_responsebedrock)r3   r   r   r   zTcompletion_response must be of type ImageResponse for bedrock image cost calculation)r3   r   r4   r   r   r   )r   rL   billed_unitssearch_unitstogethercomputertogether_ai)r;   	replicater   )datar;   )response_obj)r3   r"   r#   r4   r$   rk   r&   r%   r5   r6   r7   r8   r:   r;   r<   r,   )2r   r   r   r\   r   r   r	   r   rj   rX   r   setattr
model_dumpr~   r
   r   r]   r    rZ   rW   r^   r   r   valuer   r   passthrough_image_generationr   vertex_ai_image_cost_calculatorbedrock_image_cost_calculator	TypeErrordefault_image_cost_calculatorrB   rC   utils_count_charactersrI   rJ   rG   rF   r   metar   replicate_modelsrY   r   get_formatted_promptr   get_response_stringr   )'ry   r3   promptr   r   r   r;   r4   rk   r   r   r   r%   r&   r   r   r   r"   r5   r#   r6   r7   r8   r<   cost_per_token_usage_objectr   _usager   r   rs   r   meta_objr   r   prompt_stringcompletion_stringrn   ro   _final_costs'                                          r/   rw   rw     sx   dh$YDT	-->P1P!5#&&E
a#w.E+//359#1536)7H 38
# 1 3 3)!
 **I66-t446I6M6M7I )Y//
7==9 9 'MM;I$8$8$:;
  Iy11"--/""JJ:M &

+> B*0**5RTU*V'&,jj1JA&N#'6123r923(.

3JB(O%*?*C*COUV*W' !4naHJ  3G<OQ_ae4f3gghi $$79I4PM(&3&7&7)+>+F$'# ,//{K$(():B?CCM (++,=rBFFz "%%&7<@@ } q  sF  rG  GO  PU  OV  W  8}q  -E MVq -E G -E K=m  oB  nC  CK  LQ  KR  S 		/6/G/G0,E1 33999I77===0MMSSS #k11=AA:#':  B
 %	11=AA8#!':(7	   j  5#(;$3  ))///9	@Q@Q@W@W3W ' ? ?V ? L11777I3399907#Z1- ))///9	@Q@Q@W@W3W".:#^4 4 /33'#+<<#C#IrL#%L !$$^49  %1! %'%"m3
 2%9YCWXE
 W---1E7---34GTT=m  oB  nC  CK  LQ  KR  S  */Bk/Q8}q  ' B B$h/< !C ! %,MM$C$C$C$W!".:#]4 4 %,MM$E$E!4 %F %! )0(G(G* )H )% '/ 3'##9"7/"7(C$;4.O
	
)-$ 46WWC  	  jqqF 	D  s]   LZ  Y A>Z  (Z  Z  FZ  >CZ  
Z$.ZZ  ZZ   
Z1*Z,,Z1response_object)r   
aembeddingr   acompletionatext_completionr   r   r   
moderationamoderationrI   rJ   rC   rB   rG   rF   	cache_hitr   c	                      Sn	Ub	  USL a  Sn	U	$ [        U [        5      (       a  X@R                  S'   [        U UUUUUUUS9n	U	$ ! [         a  n
U
eSn
A
ff = f)z+
Returns
- float or None: cost of response
r!   NTr   )ry   r3   r;   r4   r   r   r   r   )r   r	   r   rw   rW   )r   r3   r4   r;   r   r   r   r   r   rm   r   s              r/   response_cost_calculatorr     s    P" Y$%6M  /955DS../@A+$3#$7 /-%	M  s   A 4A 
AAAc                     Sn[         R                  " XS9u  p1  n US:X  a	  [        XS9$ US:X  a	  [        XS9$ [	        SU  SU 35      e! [
         a  nUeSnAff = f)	z=
Returns
- float or None: cost of response OR none if error.
r?   rH   cohere)r3   num_queriesazure_aiz.invalid custom_llm_provider for rerank model: z, custom_llm_provider: N)rX   rZ   cohere_rerank_cost_per_queryazure_ai_rerank_cost_per_queryr]   rW   )r3   r4   default_num_queriesrs   r   s        r/   r_   r_   *  s     #*#;#;$ AAq(*/  !J.1  <UGCZ[nZop
 	
  s!   A A A 
AAArL   c                     [        XUS9$ )NrK   )r`   rK   s      r/   transcription_costr   G  s     "x r1   c                 L   U=(       d    SnSU;   a  SU;  a  UR                  SS5      OUn[        [        UR                  S5      5      u  pxU SU  3n	U(       a0  U R	                  U5      (       a  U SU SU R                  US5       3n	U(       a  U SU	 3OU	n
[
        R                  " SU
 SU	 35        U
[        R                  ;   a  [        R                  U
   nO9U	[        R                  ;   a  [        R                  U	   nO[        SU
 S	U	 35      eUS
   U-  U-  U-  $ )a  
Default image cost calculator for image generation

Args:
    model (str): Model name
    image_response (ImageResponse): Response from image generation
    quality (Optional[str]): Image quality setting
    n (Optional[int]): Number of images generated
    size (Optional[str]): Image size (e.g. "1024x1024" or "1024-x-1024")

Returns:
    float: Cost in USD for the image generation

Raises:
    Exception: If model pricing not found in cost map
r   xz-x-r@   r2   zLooking up cost for models: z, z#Model not found in cost map. Tried z and input_cost_per_pixel)
replacemapintr[   r   r
   r   rX   rY   rW   )r3   r4   r   r   r   r   size_strheightwidthbase_model_namemodel_name_with_quality	cost_infos               r/   r   r   O  sY   2 )MH (?uH4 	e$  X^^E23MF "
!E7+Ou//0CDD"#1XJa>QSU0V/WX 	 +27)1_%&  
&'>&?r/ARS
 '"4"44&&'>?		G..	.&&7	12I1J%P_O`a
 	
 +,v5=AAr1   )r   r   r!   NN)r2   r   r   r!   NNNNr   r   NNNNr   r!   )r!   )N)NNN)NNNr2   )NNr?   r   N)Qr}   typingr   r   r   r   r   r   pydanticr	   rX   litellm._loggingr
   /litellm.litellm_core_utils.llm_cost_calc.googler   rb   r   rc   r   ra   .litellm.litellm_core_utils.llm_cost_calc.utilsr   'litellm.llms.anthropic.cost_calculationrd   #litellm.llms.azure.cost_calculationrh   %litellm.llms.azure_ai.cost_calculatorr   r   *litellm.llms.bedrock.image.cost_calculatorr   r   #litellm.llms.cohere.cost_calculatorr   'litellm.llms.databricks.cost_calculatorrf   )litellm.llms.fireworks_ai.cost_calculatorrg   $litellm.llms.openai.cost_calculationr   r`   re   (litellm.llms.together_ai.cost_calculatorr   7litellm.llms.vertex_ai.image_generation.cost_calculatorr   litellm.types.llms.openair   litellm.types.rerankr   litellm.types.utilsr   r   r   litellm.utilsr   r   r   r   r   r   r   r   r    floatr0   r   r   r   r   boolr   r   r   r   r   rw   r   r_   r   r   r,   r1   r/   <module>r      s    = =    " W Y R A / M M
 
 
  (+48.2 uo
 $L1 %UO eE5L!"0 (+)-'++/12-.48.2'+$("./2-iPiPiP iP uo	iP
 "#iP  }iP $C=iP "*#iP &c]iP $L1iP %UOiP"  }#iP& 5/'iP*  +iP, (--iP. 5%</iPXA$ A*3 *4 * *.C=!# c], !%%))-;C=;!#; ; TN	;
 "#; c];|		e_	()@C8 "%,0!48.2&*%) $)ZC=Z 	Z Z ()Z 3-Z c]Z }Z  $L1!Z" %UO#Z$ d^%Z& TN'Z( )Z* +Zx	 !% $%)E;"	; ; "#; 	;< =;> ~?;@ A;B TNC;D E;F e_G;|!# 5%<:%-c]>C
5%< *.!'&*;B;B!#;B c];B }	;B
 3-;B d^;B ;Br1   