From 6177bcc25be656ca328a19264a5cc63235874d2e Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 05:34:15 -0700 Subject: [PATCH] fix(azure_ai): bill Foundry Cohere Embed V5 text and image tokens at their own rates --- litellm/llms/cohere/embed/transformation.py | 9 +++++++ ...est_azure_ai_passthrough_transformation.py | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/litellm/llms/cohere/embed/transformation.py b/litellm/llms/cohere/embed/transformation.py index 1477e45997b..4eb251d8432 100644 --- a/litellm/llms/cohere/embed/transformation.py +++ b/litellm/llms/cohere/embed/transformation.py @@ -57,6 +57,7 @@ class _CohereBilledUnits(BaseModel): model_config = ConfigDict(frozen=True, hide_input_in_errors=True) input_tokens: int | None = None + text_tokens: int | None = None image_tokens: int | None = None images: int | None = None @@ -116,6 +117,14 @@ def _billed_usage(billed_units: _CohereBilledUnits, image_billing: CohereImageTo image_count=billed_units.images, ), ) + case CohereImageTokenBilling.FOLDED_INTO_INPUT_TOKENS if billed_units.text_tokens is not None: + return _prompt_usage( + billed_units.text_tokens + (billed_units.image_tokens or 0), + PromptTokensDetailsWrapper( + text_tokens=billed_units.text_tokens, + image_tokens=billed_units.image_tokens or None, + ), + ) case CohereImageTokenBilling.FOLDED_INTO_INPUT_TOKENS: return _prompt_usage(billed_units.input_tokens or 0, None) diff --git a/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py b/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py index 2d8ea27edb7..59572792905 100644 --- a/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py +++ b/tests/unit/llms/azure_ai/passthrough/test_azure_ai_passthrough_transformation.py @@ -604,6 +604,32 @@ def test_native_cohere_embed_relay_prices_an_image_at_the_input_tokens_foundry_a +@pytest.mark.parametrize("model", ["Cohere-Embed-V5-Pro", "Cohere-Embed-V5-Fast"]) +@pytest.mark.parametrize( + ("input_tokens", "text_tokens", "image_tokens"), + [(1017, 6, 258), (24, 0, 6)], + ids=["text_and_image", "image_only"], +) +def test_native_cohere_embed_relay_prices_itemized_text_and_image_tokens_at_their_own_rates( + model: str, input_tokens: int, text_tokens: int, image_tokens: int +) -> None: + body: Final = { + **COHERE_EMBED_BODY, + "meta": { + "api_version": {"version": "2"}, + "billed_units": {"input_tokens": input_tokens, "image_tokens": image_tokens, "text_tokens": text_tokens}, + }, + } + result, logging_obj = _relay_logging_result(AzureAIPassthroughConfig(), model, "providers/cohere/v2/embed", body) + model_info: Final = litellm.get_model_info(f"azure_ai/{model}") + + assert isinstance(result, EmbeddingResponse) + assert result.usage.prompt_tokens == text_tokens + image_tokens + assert logging_obj.response_cost_calculator(result=result) == pytest.approx( + text_tokens * model_info["input_cost_per_token"] + image_tokens * model_info["input_cost_per_image_token"] + ) + + def test_native_cohere_embed_relay_rows_keep_their_embedding_type() -> None: result, _ = _cohere_embed_relay_result({**COHERE_EMBED_BODY, "embeddings": {"float": TWO_VECTORS, "int8": [[1, 2]]}})