
    i                         d Z ddl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edefd	Zded
edefdZ	 	 ddede	dee   dee   deeef   f
dZy)zx
Azure AI cost calculation helper.
Handles Azure AI Foundry Model Router flat cost and other Azure AI specific pricing.
    )OptionalTuple)verbose_logger)generic_cost_per_token)Usage)get_model_infomodelreturnc                 D    | j                         }d|v xs d|v xs |dk(  S )a<  
    Check if the model is Azure AI Foundry Model Router.

    Detects patterns like:
    - "azure-model-router"
    - "model-router"
    - "model_router/<actual-model>"
    - "model-router/<actual-model>"

    Args:
        model: The model name

    Returns:
        bool: True if this is a model router model
    zmodel-routermodel_routerzazure-model-router)lower)r	   model_lowers     v/Users/manta/Documents/Projects/TheRoad-I1/.venv/lib/python3.12/site-packages/litellm/llms/azure_ai/cost_calculator.py_is_azure_model_routerr      s7      ++-K+% 	/[(	/..    prompt_tokensc                 n    t        |       syt        dd      }|j                  dd      }|dkD  r||z  S y)a	  
    Calculate the flat cost for Azure AI Foundry Model Router.

    Args:
        model: The model name (should be a model router model)
        prompt_tokens: Number of prompt tokens

    Returns:
        float: The flat cost in USD, or 0.0 if not applicable
            r   azure_ai)r	   custom_llm_providerinput_cost_per_tokenr   )r   r   get)r	   r   
model_inforouter_flat_cost_per_tokens       r   &calculate_azure_model_router_flat_costr   &   sF     "%(  n*UJ!+0F!J!A%999r   Nusageresponse_time_msrequest_modelc           	         d}d}t        |       xs |duxr t        |      }	 t        | |d      \  }}|r^|r|n| }t        ||j                        }	|	dkD  r=t        j                  d|	d	d
|j                   d|	|j                  z  dd       ||	z  }||fS # t        $ r1}t        |       s t        j                  d|  d|        Y d}~d}~ww xY w)aJ  
    Calculate the cost per token for Azure AI models.

    For Azure AI Foundry Model Router:
    - Adds a flat cost of $0.14 per million input tokens (from model_prices_and_context_window.json)
    - Plus the cost of the actual model used (handled by generic_cost_per_token)

    Args:
        model: str, the model name without provider prefix (from response)
        usage: LiteLLM Usage block
        response_time_ms: Optional response time in milliseconds
        request_model: Optional[str], the original request model name (to detect router usage)

    Returns:
        Tuple[float, float] - prompt_cost_in_usd, completion_cost_in_usd

    Raises:
        ValueError: If the model is not found in the cost map and cost cannot be calculated
            (except for Model Router models where we return just the routing flat cost)
    r   Nr   )r	   r   r   zAzure AI Model Router: model 'z>' not in cost map, calculating routing flat cost only. Error: r   z"Azure AI Model Router flat cost: $z.6fz (u    tokens × $z.9fz/token))r   r   	Exceptionr   debugr   r   )
r	   r   r   r   prompt_costcompletion_costis_router_requesterouter_model_for_calcrouter_flat_costs
             r   cost_per_tokenr(   ?   s(   4 KO /u5 T!K&<]&K 
'= *(
$_$ 1>EA!5#6#6
 a  45Ec4J K''(5EH[H[5[\_4``gi ++K'';  	
 &e,,UG3qrsqtu	
 	
	
s   B 	C"'CC)r   N)__doc__typingr   r   litellm._loggingr   .litellm.litellm_core_utils.llm_cost_calc.utilsr   litellm.types.utilsr   litellm.utilsr   strboolr   intfloatr   r(    r   r   <module>r4      s   
 # + Q % (# $ 0# c e 8 ),#'	H(H(H( uoH( C=	H(
 5%<H(r   