
    h              %          d dl Z d dlmZ d dlmZmZmZmZmZm	Z	m
Z
mZ d dlmZ d dlmZ d dlZd dlZd dlmZ d dlmZmZ d dlmZ d d	lmZmZmZmZ d d
lmZ d d
l mZ! d d
l"mZ# d dl$m%Z& d d
l'mZ( d d
l)mZ* d d
l+mZ, d d
l-mZ. d dl/m0Z1 d d
l/mZ2 d d
l3mZ4 d dl5m6Z6 d dl7m8Z9 d d
l7mZ: d dl7m;Z< d dl=m%Z> d dl?m@Z@ d dlAmBZBmCZCmDZDmEZEmFZFmGZGmHZHmIZI d dlJmKZKmLZL d dlMmNZNmOZOmPZPmQZQmRZRmSZSmTZT d dlUmVZVmWZWmXZXmYZYmZZZm[Z[m\Z\m]Z]m^Z^m_Z_ erd dl`maZb neZb	 	 	 	 	 dXdecdecdeec   deeW   deec   dee	ececf      fdZd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dYdeedefdefdeec   d eee   d!eef   d"eef   d#eef   d$eef   deeW   deec   d%eef   d&eeT   d'eeK   d(eNd)ecde	ececf   f"d*ZdZd+egfd,Zhd-edeifd.Zj	 d[deee   d eee   deee   fd/Zk	 	 	 	 d\deee   d+ee   d0eee   d1eei   d eee   d2eee   deee   fd3Zl ee4      deedeifd5       Zmd+edeeT   fd6Znd7 Zod(eeN   d+edeeN   fd8Zpdddg dddddddddddddddddfdeee   d9ed:eec   d(eeN   d;eee   d<eee   d=eef   deeW   deec   d>eeg   d1eei   d0eee   d?eeS   d@eee   d2eee   dAeeb   decf"dBZqdCe
egef   deec   fdDZr	 	 	 	 	 	 	 	 d]dEe
eZeXeYe]e\eBeLeIeOeDef   deed eee   d(edF   d>egdGeei   d0eee   d1eei   dHeed?eeS   d@eee   d2eee   dAeeb   decfdIZs	 d[deed eee   dJeeK   de	ececf   fdKZtdeed eee   dLecde	ececf   fdMZu	 	 	 	 	 d^deed eee   d<eee   d=eef   d;eee   d>eeg   decfdNZv	 d[dOeTdeed eee   de	ececf   fdPZw G dQ dR      Zx G dS dTex      ZydUeEdVeTd eed@eedecf
dWZzy)_    N)	lru_cache)TYPE_CHECKINGAnyListLiteralOptionalTupleUnioncast)Response)	BaseModel)verbose_logger)DEFAULT_MAX_LRU_CACHE_SIZE&DEFAULT_REPLICATE_GPU_PRICE_PER_SECOND)StandardBuiltInToolCostTracking)CostCalculatorUtils_generic_cost_per_charactergeneric_cost_per_tokenselect_cost_metric_for_model)cost_per_tokencost_calculator)cost_per_second)get_model_params_and_category)cost_per_character)cost_router)ResponseAPILoggingUtils)HttpxBinaryResponseContentImageGenerationRequestQualityOpenAIModerationResponseOpenAIRealtimeStreamList&OpenAIRealtimeStreamResponseBaseObject!OpenAIRealtimeStreamSessionEventsResponseAPIUsageResponsesAPIResponse)RerankBilledUnitsRerankResponse)CallTypesLiteral"LiteLLMRealtimeStreamLoggingObjectLlmProvidersLlmProvidersSet	ModelInfoStandardBuiltInToolsParamsUsage)
	CallTypesCostPerTokenEmbeddingResponseImageResponseModelResponseProviderConfigManagerTextCompletionResponseTranscriptionResponse_cached_get_model_info_helpertoken_counter)Logging        prompt_tokenscompletion_tokensresponse_time_mscustom_cost_per_tokencustom_cost_per_secondreturnc                 V    ||y||d   | z  }|d   |z  }||fS |||z  dz  }d|fS y)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          S/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/cost_calculator.py%_cost_per_token_custom_pricing_helperrI   l   sh     $)?)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rerank_billed_units	call_type!audio_transcription_file_durationc           
      D   | t        d      ||}nt        ||||z   ||	      }t        |||||
      }|
|d   |d   fS d}d}t        j                  }| }||dz   | z   }|,| d| d|  }||v r|}nt        j
                  |       \  }}}}| }| j                  dd      }t        |      dkD  r|d   }n| }	 ||v r|} n| |v r| } n||v r|} |d	k(  s|d
k(  rt        j                  ||      }t        |      }d}d}|dk(  r^|t        dj                  || ||            t        |||ddd      \  }} || t        dj                  || ||||            |}| }||fS |dk(  rt        |||      \  }}||fS |dk(  s|dk(  rt        | ||      S |dk(  s+|dk(  s&|t        j                   k(  s|t        j"                  k(  rt%        || |      S |dk(  s|dk(  rt'        | ||      S |dk(  r7t)        |||      }!|!dk(  rt+        |||||      S |!dk(  rt-        |||      S y|d k(  rt/        | |!      S |d"k(  rt1        | |!      S |d#k(  rt3        | |!      S |d$k(  rt5        | |!      S |d%k(  rt7        | |!      S |d&k(  rt9        | ||'      S |d(k(  rt;        | |!      S |d)k(  rt=        | |!      S |d*k(  rt?        | |!      S tA        | |      }"|"d+   dkD  r	|"d+   |z  }nG|"jC                  d,d      5|3tE        jF                  d-| |"jC                  d,d      |       |"d,   |z  d.z  }|"d/   dkD  r	|"d/   |z  }nG|"jC                  d0d      5|3tE        jF                  d1| |"jC                  d0d      |       |"d0   |z  d.z  }tE        jF                  d2| ||       ||fS )3ar  
    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_tokensrP   rQ   )r;   r<   r=   r?   r>   r      /rL   speechaspeechrL   rM   r:   r   zvprompt_characters must be provided for tts calls. prompt_characters={}, model={}, custom_llm_provider={}, call_type={})rL   rM   rN   rO   custom_prompt_costcustom_completion_costzcost for tts call is None. prompt_cost={}, completion_cost={}, model={}, custom_llm_provider={}, prompt_characters={}, completion_characters={}r   rL   usagerM   arerankrerank)rL   rM   billed_unitsaretrieve_batchretrieve_batch)rb   rL   rM   atranscriptiontranscriptionrL   rM   duration	vertex_ai)rL   rM   rU   )rL   rM   rN   rO   rb   )rL   rM   rb   	anthropic)rL   rb   bedrockopenai
databricksfireworks_aiazure)rL   rb   r=   geminideepseek
perplexityrB   input_cost_per_secondz;For model=%s - input_cost_per_second: %s; response time: %srD   rC   output_cost_per_secondz<For model=%s - output_cost_per_second: %s; response time: %szlReturned custom cost for model=%s - prompt_tokens_cost_usd_dollar: %s, completion_tokens_cost_usd_dollar: %s)$	Exceptionr.   rI   litellm
model_costget_llm_providersplitlenget_model_infor   
ValueErrorformatr   r   rerank_costr/   rf   rg   batch_cost_calculatoropenai_cost_per_secondgoogle_cost_routergoogle_cost_per_charactergoogle_cost_per_tokenanthropic_cost_per_tokenbedrock_cost_per_tokenopenai_cost_per_tokendatabricks_cost_per_tokenfireworks_ai_cost_per_tokenazure_openai_cost_per_tokengemini_cost_per_tokendeepseek_cost_per_tokenperplexity_cost_per_tokenr7   getr   debug)#rL   r;   r<   r=   rM   region_namerN   rO   rP   rQ   r>   r?   rR   rS   rT   rU   rV   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speech_model_infocost_metricprompt_costcompletion_cost_prompt_cost_completion_costr   
model_infos#                                      rH   r   r      s   X }<== "'/&)::(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#22&<O
 33DE !$.. (  M  T  T)+!	  .I*$7"3&'#''(.*L* #'7'?  f  m  m$(,+)-	 	 'K.O O++ ,,+A*!$7,(K O++	i	9#8 3,
 	
 	&&((	111	000$U@S
 	
 
&	&)*F% 36
 	

 
	+(& 3

 ..,*$7"3&;!  ,,(*$7!  - 
	+'e;GG			)%EEE		($5DD		,(uKHH		.*kJJ		'*{=M
 	
 
	($5DD	
	*&U+FF		,(uKHH2-@

 ,-1 12]B * NN2D9E ,  M6= 	 236FFM * -.2236GG . NN3T:F ,  N7> 	 347GG$N . 	z)-		
 -.OOOrJ   completion_responsec                     t         }|dk(  rH| j                  dt        j                               }t        | dt        j                               }||z
  }||z  dz  S )Nr:   createdendedrD   )r   r   timegetattr)r   
total_time!a100_80gb_price_per_second_public
start_timeend_times        rH    get_replicate_completion_pricingr     sZ     )O%S(,,Y		D
.E
*
,z9D@@rJ   objc                     t        | d      S )N_hidden_params)hasattr)r   s    rH   has_hidden_paramsr     s    3())rJ   c                     ||S | y 	 t        j                  |       \  }}}}|S # t        $ r+}t        j                  dt        |              Y d }~y d }~ww xY w)Nr[   zblitellm.cost_calculator.py::_get_provider_for_cost_calc() - Error inferring custom_llm_provider - )ry   r{   rx   r   r   str)rL   rM   r   es       rH   _get_provider_for_cost_calcr     sv     &""}'.'?'?e'L$1   pqtuvqwpxy	
 	s   & 	A!AA
base_modelcustom_pricingrouter_model_idc                 *   d}d}t        | |      }d}|@t        |t              rt        |dd      }n"t        |t              r|j                  dd      }t        |dd      }	|du r||t        j                  v r|}n| }||}|7|	5|	j                  dd      #t        |	d         dkD  r|	j                  d|       }|	$|	j                  dd      |	j                  dd      }|||}|| | }|"| t        |      s|| d| d| }|S | d| }|S )	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
    Nr^   rL   r   Tr   r   rZ   )
r   
isinstancer   r   dictr   ry   rz   r}   "_model_contains_known_llm_provider)
rL   r   r   r   rM   r   return_modelr   completion_response_modelhidden_paramss
             rH    _select_model_name_for_cost_calcr     s    #'L!%K5)< 04&)95(/0CWd(S%+T2(;(?(?(N%$+,?AQSW$XM&?g>P>P+P*L L! (]-Fgt,8M'*+a/(,,We<L ]%6%6}d%K%W#''t< 9 E0 1 	 +2<@"12!K=,PL  22!L>BLrJ   )maxsizec                 :    | j                  d      d   }|t        v S )z:
    Check if the model contains a known llm provider
    rZ   r   )r|   r+   )rL   _provider_prefixs     rH   r   r     s#    
 {{3'*..rJ   c           	      J   t        t        t        t        t        t
        f   t        | t              r| j                  d      n t        | dd       d            }|y t        |t              r|S |Jt        |t              st        |t              r*t        j                  |      rt        j                  |      S t        |t              rt        di |S t        |t
              rt        di |j                         S t        j                  dt        |       d|        y )Nrb   r   c                      y NrE   )xs    rH   <lambda>z#_get_usage_object.<locals>.<lambda>	  s    trJ   zUnknown usage object type: z, usage_obj: rE   )r   r
   r.   r$   r   r   r   r   r   r   _is_response_api_usage+_transform_response_api_usage_to_chat_usage
model_dumpr   r   type)r   	usage_objs     rH   _get_usage_objectr     s    e%tY67 -t4  ##G,D,e^DWMI )U#	4(JyBR,S#::9E&RR
 	
 
It	$!y!!	Iy	).y++-..)$y/):-	{S	
 rJ   c                 Z    t        | t        j                        xs t        | t              S )z3Returns True if the usage obj is a known Usage type)r   ry   r.   r$   r   s    rH   _is_known_usage_objectsr   $  s'    i/ :#4 rJ   c                    | | S |y t        |t              ryt        |t              ryt        |t              ryt        |t              ryt        |t
              ryt        |t              ryt        |t              ry| S )N
completion	embeddingri   r\   rd   image_generationtext_completion)r   r3   r1   r6   r   r'   r2   r5   )rU   r   s     rH   _infer_call_typer   +  s     "%}5	'):	;	')>	?	')C	D	'	8	'	7!	')?	@ rJ   messagesr   sizequalitynoptional_paramsstandard_built_in_tools_paramslitellm_model_namelitellm_logging_objc                    	 t        ||       xs d}|dk(  s|dk(  r'|%t        |t              rt        |      dk(  r|dk(  rd}d}d}d}d}d}d}d}t	        | 	      }d}t        || ||||
      }|g}||j                  |       t        |      D ]  \  } }	 t        j                  d|        | #t        | t              st        | t              rt        | t              r| j                  di       }!nt        | di       }!t        |!t              r:t        |!      s.t        | dt!        j"                  dJi |!j%                                |!i }"n#t        |!t              r|!j%                         }"n|!}"t'        j(                  |"      r#t'        j*                  |"      j%                         }"|"j                  dd      }|"j                  dd      }|"j                  dd      }|"j                  dd      }d|"v r1|"d   i k7  r)|"d   r$|"j                  di       }#|#j                  dd      }t        | dd      }t        | dd      }$|$|$j                  d|xs d      }|$j                  d|      }|$j                  di       j                  dd      }	|$j                  di       j                  dd      }
|$j                  di       j                  dd      }nW|t-        d|  d |       t        |      dkD  rt/        ||!      }nt        |      dkD  rt/        ||"      }t/        ||"      }|t-        d|  d |       |	 t!        j0                  |#      \  }}}%}%t7        j8                  |      r|d%k(  r t        | t:              rt=        || &      c S |d'k(  r,t        | t:              rt?        ||	| |(      c S tA        d)      |t         jB                  jD                  jF                  k(  rdd*l$m%}'  |'|| &      c S tM        ||
|||	|+      c S |tN        jP                  jF                  k(  s|tN        jR                  jF                  k(  r"t         jT                  jW                  |,      }n|tN        jX                  jF                  k(  s|tN        jZ                  jF                  k(  rt        | d-d      }nV|tN        j\                  jF                  k(  s|tN        j^                  jF                  k(  r~| t        | t`              r| jb                  }(|(|(j                  d.i       xs i })ni })te        |)j                  d/      |)j                  d0      1      }|)j                  d/      xs d}*|*}n|tN        jf                  jF                  k(  rJt        | th              r:||t-        d2j5                  ||            tk        | jl                  |||3      c S |tN        jn                  jF                  k(  rdd4l8m9}+ |+ju                  |5      c S d6|v s	d7|v s|d7k(  rtw        |tO        |      8      }n6|t         jx                  v sd9|v r |t         jz                  vrt}        | |      c S |t-        d|  d |       ||d%k(  rt        |      dkD  rCt         jT                  j                  d:|id;      },t         jT                  jW                  |,,      }| Pt        | t              r@t         jT                  j                  | <      }-t         jT                  jW                  |-,      }t        dJi d=|d|d|d|d>|d|d?|d@|dA|dB|d|d|dC|dDt        t        |      dE|dF|\  }.}/|.|/z   }0|0t        j                  || |||G      z  }0|0c S  t3        dIj5                  |            # t2        $ r8}&t        j                  d$j5                  t        |&                   Y d}&~&d}&~&ww xY w# t2        $ rL}&t        j                  dHj5                  |t        |&                   | t        |      dz
  k(  r|&Y d}&~&d}&~&ww xY w# t2        $ r}&|&d}&~&ww xY w)Ka  
    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   rr   zdall-e-2r:   )r   )rL   r   rM   r   r   r   z*selected model name for cost calculation: rb   r   r;   r<   rP   rQ   prompt_tokens_detailscached_tokens_response_msr   rM   r   r   r   1024-x-1024r   standardr   rY   z[Model is None and does not exist in passed completion_response. Passed completion_response=z, model=)rL   r   )rL   textr[   zXlitellm.cost_calculator.py::completion_cost() - Error inferring custom_llm_provider - {}rl   )rL   image_responsern   )rL   r   r   r   zTcompletion_response must be of type ImageResponse for bedrock image cost calculationr   )rL   r   rM   r   r   r   )r   rk   re   search_unitsrX   )r   rX   zusage object and custom_llm_provider must be provided for realtime stream cost calculation. Got cost_per_token_usage_object={}, custom_llm_provider={})resultscombined_usage_objectrM   r   )MCPCostCalculator)r   togethercomputertogether_ai)rU   	replicater   )datarU   )response_objrL   r=   r?   r>   rN   rO   rS   rU   rV   rT   )rL   response_objectrb   r   rM   zXlitellm.cost_calculator.py::completion_cost() - Error calculating cost for model={} - {}z?Unable to calculat cost for received potential model names - {}rE   )Gr   r   r   r}   r   r   append	enumerater   r   r   r   r   r   r   setattrry   r.   r   r   r   r   r   r8   r{   rx   r   r   _call_type_has_image_responser2   vertex_ai_image_cost_calculatorbedrock_image_cost_calculator	TypeErrorr*   RECRAFTvalue$litellm.llms.recraft.cost_calculatorr   default_image_cost_calculatorr/   r\   r]   utils_count_charactersrh   ri   rd   rc   r'   metar&   	arealtimer)   'handle_realtime_stream_cost_calculationr   call_mcp_tool6litellm.proxy._experimental.mcp_server.cost_calculatorr   calculate_mcp_tool_call_costr   replicate_modelsrz   r   get_formatted_promptr3   get_response_stringr   r   r(   r   get_cost_for_built_in_tools)1r   rL   promptr   r   r   rU   rM   r   r   r   r   r>   r?   r   r   r   r   r   r   r   r;   rN   r<   rO   rP   rQ   rV   cost_per_token_usage_objectrT   selected_modelpotential_model_namesidxr   _usager   r   r   r   recraft_image_cost_calculatormeta_objre   r   r   prompt_stringcompletion_stringr   r   _final_costs1                                                    rH   r   r   F  s	   lD$Y0CDT	 -->P1P!5#&E
a#w.E+//359#1536)7H 38
# <@9 3 3)!+
 "0 0!((/#$9:JCW$$@H '22I>!"5t<!"5t</33GR@ " %,,?"$M	!)Y7@W"+A  /##MMCI,@,@,BC
 !(!##Iy9!*!5!5!7!*.EEfM!8!d!d""$*, 
 %+JJ$BM(.

3F(J%28**5q3/ /5jj9RTU.V+/69"#:;rA"#:;06

;RTV0W-2G2K2K+Q3/ "))<na!PJ$+,?AQSW$XM$0.;.?.?13F3N$/+ '4&7&7{&S,001BBGKK"M  #0"3"34Er"J"N"N%z# *--.?DHH }(y  {N  zO  OW  X]  W^  _  8}q((5EH(UVq(5E(O(5E
(S%=$u  wJ  vK  KS  TY  SZ  [  '.	;B;S;S"'<82Aq 'DDYO*k9%&9=I#B&+/B$  -	9%&9=I#@&+%)/B0?	$  (r  -0D0D0L0L0R0RR  ="'+>  
  ="'$+0C!%,;   !1!1!7!77 I$5$5$;$;;(/(G(GV(G(T%!9!9!?!?? I$;$;$A$AA8?+Z95 !1!1!7!77 I$5$5$;$;;*6:+^< $7#;#;#/+3<<+K+QrL+-L.?)5)9)9.)I)5)9)9.)I/+ ),,^<A % -9))"5"5";";;
')KA 4;.6( u  |  | ; 3  C 3 ; ;.I,?+0	  )"9"9"?"?? -II^qIrr '%/$-*m;
 :9)=E W5559M7#5#55;<OQ[\\=$u  wJ  vK  KS  TY  SZ  [ 
 (3+{: 8}q((/(J(J",h!7< )K ) -4MM,K,K!. -L -) +6:+]< -4MM,M,M)< -N -) 180O0O!2 1P 1- # "/ '8 )<	
 &0 !, ,B +@ '8 +@ 1L -D "= ##3Y? 7X  )<!15( 24UU  3OO#(;97U,? #"a ;r MTT%
 	
 % &,,v}} #A n  $$nuus1v
 #34q88G 9  s   Ba J*_7^37_7a #_7<a ?A_7 a _7a G_7a 4_7a A_7(a +D*_7a a 3	_4<-_/)_7/_44_77	a Aaa aa 	aaar   c                     t        | t              r| j                         }n| }|j                  di       }|rd|v r|d   }|y t	        |d         S y )Nadditional_headersz$llm_provider-x-litellm-response-cost)r   r   r   r   float)r   _hidden_params_dictr  r   s       rH   $get_response_cost_from_hidden_paramsr    sl     -++668+,001ErJ26HH*+QR '(NOPPrJ   r   )r   
aembeddingr   acompletionatext_completionr   r   r   
moderationamoderationrh   ri   r]   r\   rd   rc   	cache_hitr  c                     	 d}||du rd}|S t        | t              r4|| j                  d<   t        | d      rt	        | j                        }||S t        | ||||||||	|
||      }|S # t        $ r}|d}~ww xY w)z7
    Returns
    - float or None: cost of response
    r:   NTr   r   )r   rL   rU   rM   r   r   r   r  r   r   r   r   )r   r   r   r   r  r   rx   )r   rL   rM   rU   r   r!  r   r   r  r   r   r   r   r   provider_response_costr   s                   rH   response_cost_calculatorr$    s    `" Y$%6M4 1 /95DS../@A?,<=-Q'66.* .955+$3#$7 /-%/M#5 /$7M  s#   A+ AA+ A+ +	A;4A66A;re   c                 "   t        j                  | |      \  }}}}	 t        j                  | dg t	        |            }	 t        j
                  | |      }|j                  | |||      S # t        $ r d}Y "w xY w# t        $ r}|d}~ww xY w)zI
    Returns
    - float or None: cost of response OR none if error.
    r^   N)rL   api_basepresent_version_paramsprovider)rL   rM   re   r   )ry   r{   r4   get_provider_rerank_configr*   r~   rx   calculate_rerank_cost)rL   rM   re   r   configr   r   s          rH   r   r   )  s     $+#;#;)<$ AAq&AA#%!"56	
	.5.D.D1D/J ++ 3%!	 , 
 	
  	J	  s;   "A> A- A> -A;8A> :A;;A> >	BB		Brk   c                     t        | ||      S )Nrj   )r   rj   s      rH   transcription_costr-  O  s     ")<x rJ   c                    |xs d}d|v rd|vr|j                  dd      n|}t        t        |j                  d            \  }}| d|  }	d}
|r3| j	                  | d      r| j                  | dd      }
| d| d|
 }	|r| d|	 n|	}t
        j                  j                   d|	 }t        j                  d| d|	        | d| j                  d      d	    }|r| d| n|}d}||	|||| |
g}|D ],  }||t        j                  v st        j                  |   } n |t        d
|       |d   |z  |z  |z  S )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   r   z-x-rZ   NrK   zLooking up cost for models: z, z,Model not found in cost map. Tried checking input_cost_per_pixel)replacemapintr|   
startswithr   MEDIUMr   r   r   ry   rz   rx   )rL   rM   r   r   r   r   size_strheightwidthbase_model_name&model_name_without_custom_llm_providermodel_name_with_qualitymodel_name_with_v2_qualitymodel_without_provider#model_with_quality_without_provider	cost_infomodels_to_check_models                     rH   r   r   W  s   2 )MH (?uH4 	e$  X^^E23MFE "
!E7+O<@*u//3F2Gq0IJ16"#1%r2
. ##1XJa0V/WX 	 +27)1_%&  )//55
6a7HI  
&'>&?r/ARS !)z5;;s+;B+?*@A187)1+,->T (
 !%I"+.,O "&G,>,>">**62I " :?:KL
 	
 +,v5=AArJ   rb   c                    t        j                  ||      \  }}}}t        j                  d||       	 t        j                  ||      }|sy|j                  d      }|j                  d      }|j                  d      }|j                  d      }d	}	d	}
|r| j                  |z  }	n|r| j                  |z  d
z  }	|r| j                  |z  }
|	|
fS |r| j                  |z  d
z  }
|	|
fS # t
        $ r d}Y w xY w)z+
    Calculate the cost of a batch job
    r^   zBCalculating batch cost per token. model=%s, custom_llm_provider=%sN)r:   r:   input_cost_per_token_batchesrB   output_cost_per_token_batchesrC   r:      )	ry   r{   r   r   r~   rx   r   r;   r<   )rb   rL   rM   r   r   rC  rB   rD  rC   total_prompt_costtotal_completion_costs              rH   r   r     sD    $+#;#;)<$ AAq L*1*@*@-@+

 #->>2P#Q %>>*@A$.NN3R$S!&NN+BC#!//2NN	#781< 	 % % 7 7:W W 333 
##'<=A 	 3333  
s   C* *C87C8c                   ,    e Zd Zedee   defd       Zy)BaseTokenUsageProcessorusage_objectsr@   c                 "   ddl m}m}m}  |       }| D ]w  }t	        |      D ]  }|j                  d      rt        t        ||            r+t        ||d      }t        ||d      }|Ht        |t        t        f      s_t        |t        t        f      svt        ||||z           t        |d      r|j                  rt        |d      r|j                  s |       |_        |j                  j                  D ]  }t        |j                  |      s|j                  d      r,t        t        |j                  |            rLt        |j                  |d      xs d}t        |j                  |d      xs d}|t        |t        t        f      st        |j                  |||z           t        |d      s|j                  st        |d      r|j                  s |       |_        t	        |j                        D ]  }|j                  d      rt        t        |j                  |            r5t        |j                  |d      }t        |j                  |d      }|ft        |j                  |||z           z |S )zs
        Combine multiple Usage objects into a single Usage object, checking model keys for nested values.
        r   )CompletionTokensDetailsPromptTokensDetailsWrapperr.   r   r   completion_tokens_details)litellm.types.utilsrL  rM  r.   dirr4  callabler   r   r3  r  r   r   r   model_fieldsrN  )	rJ  rL  rM  r.   combinedrb   attrcurrent_valnew_vals	            rH   combine_usage_objectsz-BaseTokenUsageProcessor.combine_usage_objects  sM   
	
 	
 7 #EE
s+HWUD=Q4R")(D!"<K%eT15G+&we=&{S%LA$g0EF # u565;V;V*AB#995O5QH2 "77DDD ; ;TB $ 4 (1L1Ld)S T $H$B$BD!LQPQ $ #*%*E*EtQ"O"TST".:gU|3T# ( > > $ +g 5 E& :;33  *EF#==9P9RH6   ? ?@D??3/ ? ?F9 '.$>>a' #*%*I*I4QR"S".# ( B B $ +g 5 Ae #B rJ   N)__name__
__module____qualname__staticmethodr   r.   rW  rE   rJ   rH   rI  rI    s*    NT%[ NU N NrJ   rI  c                   d    e Zd Zededee   fd       Zededefd       Zededede	fd       Z
y)RealtimeAPITokenUsageProcessorr   r@   c                     t        t        t           | D cg c]  }|d   dk(  s| c}      }g }|D ];  }t        j                  |d   j                  di             }|j                  |       = |S c c}w )z<
        Collect usage from realtime stream results
        r   zresponse.doneresponserb   )r   r   r"   r   r   r   r   )r   resultresponse_done_eventsrJ  rS   s        rH   *collect_usage_from_realtime_stream_resultszIRealtimeAPITokenUsageProcessor.collect_usage_from_realtime_stream_results4  s     NR78")OVF^-NVON
 &(*F'SS:&**7B7 
   . +  Ps
   A3
A3
c                 Z    t         j                  |       }t         j                  |      }|S )zH
        Collect and combine usage from realtime stream results
        )r]  rb  rW  )r   collected_usage_objectsr   s      rH   6collect_and_combine_usage_from_realtime_stream_resultszURealtimeAPITokenUsageProcessor.collect_and_combine_usage_from_realtime_stream_resultsI  s8     +UU 	 
 !? T T#!
 %$rJ   rb   c                     t        | |      S )Nrb   r   )r)   rg  s     rH   create_logging_realtime_objectz=RealtimeAPITokenUsageProcessor.create_logging_realtime_objectZ  s     2
 	
rJ   N)rX  rY  rZ  r[  r!   r   r.   rb  re  r)   rh  rE   rJ   rH   r]  r]  3  sx    )	e ( %)%	% %  

7
	+
 
rJ   r]  r   r   c                 6   d}g }| D ]?  }|d   dk(  st        t        |      d   j                  dd      }|j                  |       A |j                  |       d}d}|D ]"  }		 |	t	        |	||      \  }
}||
z  }||z  } n ||z   }|S # t
        $ r Y 7w xY w)z
    Handles the cost calculation for realtime stream responses.

    Pick the 'response.done' events. Calculate total cost across all 'response.done' events.

    Args:
        results: A list of OpenAIRealtimeStreamBaseObject objects
    Nr   zsession.createdsessionrL   r:   ra   )r   r#   r   r   r   rx   )r   r   rM   r   received_modelr  r`  rB   rC   
model_name_input_cost_per_token_output_cost_per_token
total_costs                rH   r  r  d  s     N&>..!"CVLc'4   "((8    !34+
		!<R +$7=9!#9 	 55!77 , &(==J  		s   $B'B	BB)r   r   r:   NN)rK   r   r   r:   NNNNr   r   NNNNNr   r:   )r:   r   )NNNN)NNNrK   NNNN)NNrY   r   N){r   	functoolsr   typingr   r   r   r   r   r	   r
   r   httpxr   pydanticr   ry   litellm._loggingr   litellm.constantsr   r   @litellm.litellm_core_utils.llm_cost_calc.tool_call_cost_trackingr   .litellm.litellm_core_utils.llm_cost_calc.utilsr   r   r   r   'litellm.llms.anthropic.cost_calculationr   r   #litellm.llms.azure.cost_calculationr   %litellm.llms.bedrock.cost_calculationr   *litellm.llms.bedrock.image.cost_calculatorr   r   'litellm.llms.databricks.cost_calculatorr   %litellm.llms.deepseek.cost_calculatorr   )litellm.llms.fireworks_ai.cost_calculatorr   #litellm.llms.gemini.cost_calculatorr   $litellm.llms.openai.cost_calculationr   r   r   'litellm.llms.perplexity.cost_calculatorr   (litellm.llms.together_ai.cost_calculatorr   &litellm.llms.vertex_ai.cost_calculatorr   r   r   r   r   7litellm.llms.vertex_ai.image_generation.cost_calculatorr   litellm.responses.utilsr   litellm.types.llms.openair   r   r    r!   r"   r#   r$   r%   litellm.types.rerankr&   r'   rO  r(   r)   r*   r+   r,   r-   r.   litellm.utilsr/   r0   r1   r2   r3   r4   r5   r6   r7   r8   *litellm.litellm_core_utils.litellm_loggingr9   LitellmLoggingObjectr  rI   r   r3  r   r   boolr   r   r   r   r   r   r   r   r  r$  r   r-  r   r   rI  r]  r  rE   rJ   rH   <module>r     s     R R R     "  X Y S U <	 	 	 C        (+48.2 uo
 $L1 %UO eE5L!"0 (+)-'++/12-.48.2'+$(7;"./21MPMPMP MP uo	MP
 "#MP  }MP $C=MP "*#MP &c]MP $L1MP %UOMP"  }#MP& 5/'MP* ""34+MP.  /MP0 (-1MP2 5%<3MP`	A$ 	A*3 *4 * *.C=!# c], !%%))-%)?C=?!#? ? TN	?
 "#? c]? c]?D -./c /d / //  e_ F()@C8 "%,0!48.2&*%) $KO(,%):>1zC=z 	z z ()z 3-z c]z }z  $L1!z" %UO#z$ d^%z& TN'z( )z* %--G$H+z, !-z. c]/z0 ""671z2 3zzy)e_n !% $%)KO(,%):>UO"* 
	O O "#O  	!OD EOF ~GOH IOJ TNKOL MON %--G$HOOP !QOR c]SOT ""67UOV WOj 15##!## ,-# 5%<	#L%-c]>C
5%< *.!'&*SBSB!#SB c]SB }	SB
 3-SB d^SB SBr *.040404 "#04 5%<	04fP Pf.
%< .
b+%+ + + 	+
 +rJ   