fix(vertex-live): charge the fixed cost margin once per Live session

This commit is contained in:
mateo-berri 2026-09-12 16:33:31 -07:00
parent 83594427fc
commit 710d4ae2a3
2 changed files with 37 additions and 4 deletions

View file

@ -314,14 +314,24 @@ class VertexAILivePassthroughLoggingHandler(BasePassthroughLoggingHandler):
model: str,
logging_obj: LiteLLMLoggingObj,
) -> float | None:
"""Price each turn on its own tokens and grounding, so two grounded turns pay the query fee twice."""
"""Price each turn on its own tokens and grounding, so two grounded turns pay the query fee twice.
The fixed cost margin is a flat per-request fee, so the session's single spend row carries it once
rather than once per turn.
"""
turn_costs: Final = tuple(self._turn_cost(turn, model, logging_obj) for turn in _turns(websocket_messages))
priced: Final = tuple(turn_cost for turn_cost in turn_costs if turn_cost is not None)
if not priced or len(priced) != len(turn_costs):
return None
breakdowns: Final = tuple(breakdown for _, breakdown in priced)
first: Final = breakdowns[0]
total_cost: Final = sum(cost for cost, _ in priced)
fixed_margin: Final = first.get("margin_fixed_amount") or 0.0
duplicated_fixed_margin: Final = fixed_margin * (len(priced) - 1)
total_cost: Final = sum(cost for cost, _ in priced) - duplicated_fixed_margin
summed_margin_total: Final = _summed(breakdowns, "margin_total_amount")
margin_total_amount: Final = (
None if summed_margin_total is None else summed_margin_total - duplicated_fixed_margin
)
logging_obj.set_cost_breakdown(
input_cost=_summed(breakdowns, "input_cost") or 0.0,
output_cost=_summed(breakdowns, "output_cost") or 0.0,
@ -331,8 +341,8 @@ class VertexAILivePassthroughLoggingHandler(BasePassthroughLoggingHandler):
discount_percent=first.get("discount_percent"),
discount_amount=_summed(breakdowns, "discount_amount"),
margin_percent=first.get("margin_percent"),
margin_fixed_amount=_summed(breakdowns, "margin_fixed_amount"),
margin_total_amount=_summed(breakdowns, "margin_total_amount"),
margin_fixed_amount=first.get("margin_fixed_amount"),
margin_total_amount=margin_total_amount,
cache_read_cost=_summed(breakdowns, "cache_read_cost"),
cache_creation_cost=_summed(breakdowns, "cache_creation_cost"),
reasoning_cost=_summed(breakdowns, "reasoning_cost"),

View file

@ -13,6 +13,7 @@ from typing import Dict, List, Any, Optional
import pytest
import httpx
import litellm
from typing_extensions import NotRequired, ReadOnly, TypedDict
# Add the parent directory to the system path
@ -533,6 +534,28 @@ class TestVertexAILivePassthroughLoggingHandler:
assert two_breakdown["total_cost"] == pytest.approx(two_cost)
assert two_breakdown["tool_usage_cost"] == pytest.approx(2 * one_breakdown["tool_usage_cost"])
def test_the_fixed_cost_margin_is_charged_once_per_session(self, handler):
"""A fixed cost margin is a flat per-request fee, and a Live session is one spend row.
Pricing each turn on its own applied the fixed margin per turn, so a two-turn session paid it
twice. The session now carries the fixed margin once no matter how many turns it billed.
"""
head, turn = self._live_messages(self.AUDIO_SESSION[:1])
grounding = self._grounding_frame({"webSearchQueries": ["q"]})
messages = [head, grounding, turn, grounding, turn]
plain_cost, _ = self._billed_session(handler, messages)
fixed_amount = 0.01
with patch.object(litellm, "cost_margin_config", {"vertex_ai": {"fixed_amount": fixed_amount}}):
margined_cost, breakdown = self._billed_session(handler, messages)
assert margined_cost - plain_cost == pytest.approx(
fixed_amount
), "a two-turn session must add the fixed margin once, not once per billed turn"
assert breakdown["margin_fixed_amount"] == pytest.approx(fixed_amount)
assert breakdown["margin_total_amount"] == pytest.approx(fixed_amount)
def test_reporting_tool_use_tokens_does_not_move_the_bill(self, handler, mock_logging_obj):
"""Deliberate boundary: these tokens are reported here, and priced nowhere.