diff --git a/patch_audio_request_cost.py b/patch_audio_request_cost.py index d8ddac7..8147685 100644 --- a/patch_audio_request_cost.py +++ b/patch_audio_request_cost.py @@ -144,6 +144,75 @@ ANCHOR_RETURN = """ region_name=region_name, except Exception as e: raise e""" +# The unit-price call. On speech/transcription LiteLLM prices by character, +# second or token and raises ValueError when the model declares none of those +# rates — which is exactly a per-request model. The declaration is then the +# whole bill, so price the base at 0 instead of letting the raise swallow the +# spend log write (issue #5: rows recorded spend 0.0 and the tail patch below +# never ran because the exception aborted the function first). +ANCHOR_COST = """ response_cost = completion_cost( + completion_response=response_object, + model=model, + call_type=call_type, + custom_llm_provider=custom_llm_provider, + optional_params=optional_params, + custom_pricing=custom_pricing, + base_model=base_model, + prompt=prompt, + standard_built_in_tools_params=standard_built_in_tools_params, + litellm_model_name=litellm_model_name, + router_model_id=router_model_id, + litellm_logging_obj=litellm_logging_obj, + service_tier=service_tier, + data_residency=data_residency, + vertex_location=vertex_location, + region_name=region_name, + )""" + +PATCHED_COST = """ try: + response_cost = completion_cost( + completion_response=response_object, + model=model, + call_type=call_type, + custom_llm_provider=custom_llm_provider, + optional_params=optional_params, + custom_pricing=custom_pricing, + base_model=base_model, + prompt=prompt, + standard_built_in_tools_params=standard_built_in_tools_params, + litellm_model_name=litellm_model_name, + router_model_id=router_model_id, + litellm_logging_obj=litellm_logging_obj, + service_tier=service_tier, + data_residency=data_residency, + vertex_location=vertex_location, + region_name=region_name, + ) + except Exception as _coop_unit_exc: + # CO-OP PATCH (audio per-request cost, 2026-11-17): a model billed + # per request declares no per-character/second/token rate, so + # LiteLLM's unit pricing raises. The per-request charge is the + # whole bill; let the tail patch below add it to a zero base. + _coop_declared_pr = ( + _coop_per_request_cost( + model=model, + litellm_model_name=litellm_model_name, + custom_pricing=custom_pricing, + litellm_logging_obj=litellm_logging_obj, + ) + if call_type in ("transcription", "atranscription", "speech", "aspeech") + else 0.0 + ) + if _coop_declared_pr > 0: + print( + f"[coop-audio] unit pricing unavailable ({type(_coop_unit_exc).__name__}); " + f"pricing at declared per-request={_coop_declared_pr!r}", + flush=True, + ) + response_cost = 0.0 + else: + raise""" + PATCHED_EARLY = """ if provider_response_cost is not None and not custom_pricing: # CO-OP PATCH (2026-11-16): a provider-reported cost is not the # whole bill on the audio paths. Speech returns here and never @@ -249,6 +318,12 @@ def main() -> int: patched = source.replace(ANCHOR_DEF, HELPER + ANCHOR_DEF, 1) patched = patched.replace(ANCHOR_RETURN, PATCHED_RETURN, 1) + if ANCHOR_COST in patched: + patched = patched.replace(ANCHOR_COST, PATCHED_COST, 1) + print("[coop-patch] unit-price call made per-request tolerant (speech/transcription)") + else: + print("[coop-patch] WARNING unit-price anchor not found; per-request models " + "will still raise and bill $0") if ANCHOR_EARLY in patched: patched = patched.replace(ANCHOR_EARLY, PATCHED_EARLY, 1) print("[coop-patch] early provider-cost return also patched (audio speech)")