feat(cost): bill metadata.completion_window requests at tier prices

This commit is contained in:
shrey kharbanda 2026-09-24 04:57:26 +00:00
parent 607ab8e1a6
commit 4756fd6964

View file

@ -964,6 +964,27 @@ def _extract_service_tier(source: object) -> str | None:
return None
def _completion_window_value(metadata: object) -> str | None:
"""Return ``metadata["completion_window"]`` only when it names a billable tier
("flex" or "balanced"). "asap" is the provider default and bills at standard
pricing, so it returns None."""
if not isinstance(metadata, dict):
return None
window: Final = cast(dict[str, object], metadata).get("completion_window")
if isinstance(window, str) and window in (ServiceTier.FLEX.value, ServiceTier.BALANCED.value):
return window
return None
def _service_tier_from_completion_window(optional_params: dict[str, object]) -> str | None:
"""Read ``metadata.completion_window`` from ``extra_body`` or a top-level ``metadata``
param (the two shapes callers use to pick a provider completion window directly)."""
extra_body: Final = optional_params.get("extra_body")
return _completion_window_value(
cast(dict[str, object], extra_body).get("metadata") if isinstance(extra_body, dict) else None
) or _completion_window_value(optional_params.get("metadata"))
def get_usage_object(
completion_response: object,
) -> Usage | None:
@ -1376,6 +1397,8 @@ def completion_cost(
# Extract service_tier from optional_params if not provided directly
if service_tier is None and optional_params is not None:
service_tier = optional_params.get("service_tier")
if service_tier is None:
service_tier = _service_tier_from_completion_window(cast(dict[str, object], optional_params))
service_tier = _normalize_service_tier(service_tier)