Ë
    uPêjd  ã            
       óD  — U d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
mZ erddlmZ  eddd	 e
d
dd¬«      ¬«      Zeed<    G d„ de«      Zd dededz  dedz  fd„Zdddedeedf   fd„Zdddedeeef   dz  fd„Zdddee   dedz  fd„Zdddee   dedz  fd„Zy)!ae  The counterfactual a complexity router's savings are measured against.

The router's own tier ladder names the model the traffic would have run on without a
router: a deployment has to pick one model that can carry the hardest request it will
see, so the baseline is the priciest model in the hardest configured tier. A cheap
tier is a choice the router made, not a ceiling it was bounded by.

Candidates are ranked once against a fixed reference request, not against each request
that runs. Ranking per request means reading the request, and every input shape it can
take; a per-router default must not carry that surface.

Baselines are always provider-qualified, because they travel to the spend writer as a
bare string with no provider beside them; an operator who writes ``deepseek-r1`` meaning
Azure would otherwise be priced against whoever else owns that name.
é    )ÚIterable)ÚTYPE_CHECKINGÚFinalÚ
NamedTuple)Úverbose_router_logger)ÚPromptTokensDetailsWrapperÚUsage)ÚRouteri N  iè  iR  i8J  )Úcached_tokensÚcache_creation_tokensÚtext_tokens)Úprompt_tokensÚcompletion_tokensÚtotal_tokensÚprompt_tokens_detailsÚ_REFERENCE_REQUESTc                   ó0   — e Zd ZU dZeed<   dZedz  ed<   y)ÚBaselinea|  The counterfactual deployment: what it is called, and which deployment it was.

    ``model`` is what the operator would recognise, and the string the spend writer
    prices the counterfactual under. ``deployment_id`` only ranks: a deployment can be
    charged something other than its model's public rate, and
    `Router.get_deployment_model_info` is what merges the two.
    ÚmodelNÚdeployment_id)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   © ó    úm/home/oem/CyberBrain26-unified/.venv/lib/python3.12/site-packages/litellm/router_strategy/savings_baseline.pyr   r   #   s   … ñð ƒJØ $€M�3˜‘:Ô$r   r   Nr   Úcustom_llm_providerÚreturnc                 óø   — ddl }ddlm}  || |«      }|�|› d| j                  |› d�«      › �S 	 |j	                  | |¬«      \  }}}}|› d|› �S # t
        $ r!}t        j                  d| |«       Y d}~yd}~ww xY w)a$  ``provider/model``, or ``None`` when the pair names no known provider.

    A deployment may name its vendor in the model prefix or in a separate
    ``custom_llm_provider``, and the bare name alone is not enough to price: it can
    resolve to a different vendor's rates, or to nothing at all.

    A github_copilot or chatgpt candidate is qualified by string alone: resolving either
    provider runs its OAuth device flow, and for a declared pair the resolver's answer is
    the declaration itself, so asking it buys nothing but the block.
    r   N)Ú declared_authenticating_providerú/)r   r    z2savings baseline: cannot resolve candidate %s (%s))ÚlitellmÚ1litellm.litellm_core_utils.get_llm_provider_logicr#   ÚremoveprefixÚget_llm_providerÚ	Exceptionr   Údebug)	r   r    r%   r#   ÚdeclaredÚresolvedÚproviderÚ_Úes	            r   Úcanonical_modelr0   0   s¡   € ó Ýbá6°uÐ>QÓR€HØÐØ�˜1˜U×/Ñ/°8°*¸A°Ó?Ð@ÐAÐAðØ#*×#;Ñ#;À%Ð]pÐ#;Ó#qÑ ˆ�(˜A˜qð ˆZ�q˜˜
Ð#Ð#øô ò Ü×#Ñ#Ð$XÐZ_ÐabÔcÜûðús   °A Á	A9ÁA4Á4A9Úrouterr
   Ú
group_name.c                 óÌ   ‡ ‡‡— ‰ j                   j                  |«      }|st        |«      x}rt        |«      fS dS dt        dt        dz  fˆ fd„Št        ˆˆfd„|D «       «      S )a7  The candidates a tier entry actually calls, each with its own pricing key.

    `litellm_params.model` is not always a model: on Azure it is the deployment name,
    absent from the cost map, and `model_info.base_model` names the real one. Wildcard
    and aliased deployments behave the same, and router.py resolves pricing through
    that same base_model chain. A name matching no deployment is a tier pointing
    straight at a provider model rather than at a configured group, and prices under
    its own name because there is no deployment to override it.
    r   Úindexr!   Nc                 óâ  •— ‰j                   |    }|j                  d«      }t        |t        «      sy |j                  d«      }t        |t        «      r|j                  d«      nd }|xs$ |j                  d«      xs |j                  d«      }|rt	        ||j                  d«      «      nd }|€y t        |t        «      r|j                  d«      nd }t        ||rt        |«      «      S d «      S )NÚlitellm_paramsÚ
model_infoÚ
base_modelr   r    Úid)Ú
model_listÚgetÚ
isinstanceÚdictr0   r   r   )	r4   Ú
deploymentÚparamsÚinfoÚbaser   Ú	qualifiedr   r1   s	           €r   Ú	candidatez_models_in.<locals>.candidateW   sÏ   ø€ Ø"×-Ñ-¨eÑ4ˆ
Ø"Ÿ™Ð'7Ó8ˆÜ˜&¤$Ô'ØØ —n‘n \Ó2ˆÜ0:¸4ÄÔ0F�d—h‘h˜|Ô,ÈDˆØÒN˜vŸz™z¨,Ó7ÒN¸6¿:¹:ÀgÓ;NˆÙX]œ?¨5°&·*±*Ð=RÓ2SÔTÐcgˆ	ØÐØÜ1;¸DÄ$Ô1G˜tŸx™x¨œ~ÈTˆÜ˜	¹¤3 }Ó#5ÓQÐQÈDÓQÐQr   c              3   ó8   •K  — | ]  } ‰|«      xŠ€Œ‰–— Œ y ­w©Nr   )Ú.0r4   ÚcrC   s     €€r   ú	<genexpr>z_models_in.<locals>.<genexpr>e   s    øè ø€ ÒO�u±¸5Ó1AÐ,A¨AÑ+N”ÑOùs   ƒ“)Ú model_name_to_deployment_indicesr;   r0   r   ÚintÚtuple)r1   r2   ÚindicesrB   rG   rC   s   `   @@r   Ú
_models_inrM   I   sn   ú€ ð ×<Ñ<×@Ñ@ÀÓL€GÙÜ7FÀzÓ7RÐ*R¨)Ð*R”˜Ó#Ð%Ð[ÐY[Ð[ðRœð R¤¨D¡õ Rô ÔO ÔOÓOÐOr   rC   c                 ó¶  — ddl m} |j                  j                  d«      \  }}}	 | j	                  |j
                  xs d|j                  «      }|€y ||xs |j                  t        ||¬«      \  }}||z   }
|
dk  r!t        j                  d	|j                  «       y|
|fS # t        $ r+}	t        j                  d|j                  |	«       Y d}	~	yd}	~	ww xY w)
a0  ``(cost_of_the_reference_request, candidate)``, or ``None`` when unpriceable.

    "Most expensive" is a property of a request, not of a rate: a deployment dearer per
    output token can be cheaper per cached token, so comparing a chosen pair of rates
    orders cache-heavy traffic backwards. Costing one reference request through the same
    engine the savings use leaves cache rates, tiered tables and every other billing
    dimension to that engine. A candidate that prices to nothing there cannot stand in
    for what the traffic would have cost.
    r   )Úgeneric_cost_per_tokenr$   Ú N)r   Úusager    r7   z2savings baseline: no pricing for candidate %s (%s)g        z0savings baseline: candidate %s prices to nothing)
Ú.litellm.litellm_core_utils.llm_cost_calc.utilsrO   r   Ú	partitionÚget_deployment_model_infor   r   r)   r   r*   )r1   rC   rO   r-   r.   Ú
model_namer@   Úprompt_costÚcompletion_costr/   Úcosts              r   Ú_pricedrY   h   sâ   € õ Và'Ÿo™o×7Ñ7¸Ó<Ñ€Hˆa�ðØ×6Ñ6°y×7NÑ7NÒ7TÐRTÐV_×VeÑVeÓfˆØˆ<ØÙ'=ØÒ/ 	§¡Ü$Ø (Øô	(
Ñ$ˆ�_ð  Ñ/€DØˆs‚{Ü×#Ñ#Ð$VÐXa×XgÑXgÔhØØ�)ÐÐøô ò Ü×#Ñ#Ð$XÐZc×ZiÑZiÐklÔmÜûðús   §,B$ Á!B$ Â$	CÂ-!CÃCÚ
candidatesc                 ó|   ‡ ‡— t        ˆˆ fd„|D «       «      }|st        j                  d«       yt        |«      d   S )zEThe candidate that would have cost the most on the reference request.c              3   ó@   •K  — | ]  }t        ‰|«      xŠ€Œ‰–— Œ y ­wrE   )rY   )rF   rC   Úrr1   s     €€r   rH   z"_most_expensive.<locals>.<genexpr>‹   s#   øè ø€ Òi 	ÄÈÐQZÓA[Ð<[¸AÑ;hœ!Ñiùs   ƒ—zBsavings baseline: no priceable candidates; savings driver disabledNé   )rK   r   r*   Úmax)r1   rZ   Úpricedr]   s   `  @r   Ú_most_expensivera   ‰   s6   ù€ äÔi¨ZÔiÓi€FÙÜ×#Ñ#Ð$hÔiØÜˆv‹;�q‰>Ðr   Úgroup_namesc                 óˆ   ‡ — 	 t        ‰ ˆ fd„|D «       «      S # t        $ r }t        j                  d|«       Y d}~yd}~ww xY w)a…  The derived baseline for a router whose hardest tier offers ``group_names``.

    Holds no cache of its own; each pricing pass walks the pool, so the caller is
    expected to bound how often it runs. The complexity router caches the result with a
    TTL, which keeps a deployment added or removed at runtime able to change the
    baseline while keeping this walk off the per-request hot path.

    Never raises. This is read on the routing path to decorate a request that is about
    to be served, and a dashboard's counterfactual is not worth failing a live request
    over; an unresolvable baseline zeroes the savings driver instead.
    c              3   óD   •K  — | ]  }t        ‰|«      D ]  }|–— Œ Œ y ­wrE   )rM   )rF   ÚnamerG   r1   s      €r   rH   z#resolve_baseline.<locals>.<genexpr>Ÿ   s&   øè ø€ Ò'd¨dÌ:ÐV\Ð^bÓKcÒ'dÀa¬Ð'd¨Ñ'dùs   ƒ z@savings baseline: could not resolve, savings will read zero (%s)N)ra   r)   r   Úwarning)r1   rb   r/   s   `  r   Úresolve_baselinerg   ’   s@   ø€ ðÜ˜vÓ'd°kÔ'dÓeÐeøÜò Ü×%Ñ%Ð&hÐjkÔlÜûðús   ƒ ˜	A¡<¼ArE   )r   Úcollections.abcr   Útypingr   r   r   Úlitellm._loggingr   Úlitellm.types.utilsr   r	   Úlitellm.routerr
   r   r   r   r   r0   rK   rM   ÚfloatrY   ra   rg   r   r   r   ú<module>rn      s  ðòõ  %ß 3Ñ 3å 2ß AáÝ%ñ "ØØØÙ4À6ÐafÐtuÔvô	Ð �Eó ô
%ˆzô 
%ñ$˜3ð $°S¸4±Zð $È3ÐQUÉ:ó $ð2P�xð P¨Sð P°U¸8ÀS¸=Ñ5Ió Pð>�Hð ¨ð °e¸EÀ8¸OÑ6LÈtÑ6Só ðB˜Hð °(¸8Ñ2Dð ÈÐTXÉó ð˜Xð °H¸S±Mð ÀhÐQUÁoô r   