o
    ưiQ                     @   st   d Z ddlmZmZ ddlmZ ddlmZ erddlmZ de	dedee
e
f fd	d
Zddddde
fddZdS )z
Helper util for handling XAI-specific cost calculation
- Uses the generic cost calculator which already handles tiered pricing correctly
- Handles XAI-specific reasoning token billing (billed as part of completion tokens)
    )TYPE_CHECKINGTuple)Usage)generic_cost_per_token)	ModelInfomodelusagereturnc                 C   sx   t t|ddpd}d}t|dr|jrt t|jddpd}|| }t|j||j|jdd}t| |dd\}}||fS )	a  
    Calculates the cost per token for a given XAI model, prompt tokens, and completion tokens.
    Uses the generic cost calculator for all pricing logic, with XAI-specific reasoning token handling.

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

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd
    completion_tokensr   completion_tokens_detailsreasoning_tokensN)prompt_tokensr
   total_tokensprompt_tokens_detailsr   Zxai)r   r   Zcustom_llm_provider)	intgetattrhasattrr   r   r   r   r   r   )r   r   r
   r   Ztotal_completion_tokensZmodified_usageZprompt_costZcompletion_cost r   W/home/app/Keep/.python/lib/python3.10/site-packages/litellm/llms/xai/cost_calculator.pycost_per_token   s$   
r   r   
model_infor   c                 C   sl   d}d}t | dr!| jdur!t | jdr!| jjdur!t| jj}nt | dr0| jdur0t| j}|| }|S )aI  
    Calculate the cost of web search requests for X.AI models.
    
    X.AI Live Search costs $25 per 1,000 sources used.
    Each source costs $0.025.
    
    The number of sources is stored in prompt_tokens_details.web_search_requests
    by the transformation layer to be compatible with the existing detection system.
    g?r   r   Nweb_search_requestsnum_sources_used)r   r   r   r   r   )r   r   Zcost_per_sourcer   Z
total_costr   r   r   cost_per_web_search_request6   s   


r   N)__doc__typingr   r   Zlitellm.types.utilsr   Z.litellm.litellm_core_utils.llm_cost_calc.utilsr   r   strfloatr   r   r   r   r   r   <module>   s    &