fix(cost): price a realtime transcription clip at the transcription model the session set

A realtime conversation session opens with session.created naming the realtime model and no
transcription model, and the client sets the transcription model later in session.update. The
cost code took the first session frame and fell back to its session model, so a clip was priced
at the realtime model. That model has no per-second rate, so a gpt-transcribe clip cost $0, and a
token-billed clip used the realtime model's audio rate instead of the transcription model's.

Now the newest session frame that names a transcription model sets the price, and the first
session model is only the fallback when no frame names one, as before
This commit is contained in:
mateo-berri 2026-10-31 03:18:40 -07:00
parent 0e15ec1f22
commit 0cdc1ccab7
2 changed files with 58 additions and 16 deletions

View file

@ -3781,25 +3781,37 @@ def handle_realtime_transcription_cost_calculation(
return total_cost
_TRANSCRIPTION_SESSION_EVENT_TYPES: Final = frozenset(
{"transcription_session.created", "transcription_session.updated", "session.created", "session.updated"}
)
def _str_field(mapping: Mapping[str, object] | None, key: str) -> str | None:
value: Final = None if mapping is None else mapping.get(key)
return value if isinstance(value, str) and value else None
def _configured_transcription_model(session: Mapping[str, object]) -> str | None:
audio_input: Final = as_str_mapping((as_str_mapping(session.get("audio")) or {}).get("input"))
nested: Final = _str_field(as_str_mapping((audio_input or {}).get("transcription")), "model")
return nested or _str_field(as_str_mapping(session.get("input_audio_transcription")), "model")
def _get_transcription_model_name_from_results(
results: OpenAIRealtimeStreamList,
) -> str | None:
"""Resolve the ASR model from a transcription_session.* / session.* event."""
for result in results:
if result.get("type") in (
"transcription_session.created",
"transcription_session.updated",
"session.created",
"session.updated",
):
session = cast(dict, result).get("session", {}) or {}
transcription = ((session.get("audio", {}) or {}).get("input", {}) or {}).get(
"transcription", {}
) or session.get("input_audio_transcription", {})
model = (transcription or {}).get("model") or session.get("model")
if model:
return model
return None
sessions: Final = tuple(
session
for result in results
if result.get("type") in _TRANSCRIPTION_SESSION_EVENT_TYPES
and (session := as_str_mapping(result.get("session"))) is not None
)
configured: Final = next(
(model for session in reversed(sessions) if (model := _configured_transcription_model(session))), None
)
return configured or next(
(session_model for session in sessions if (session_model := _str_field(session, "model"))), None
)
_TranscriptionRateKey: TypeAlias = Literal[

View file

@ -1451,6 +1451,36 @@ def test_transcription_usage_cost_returns_zero_for_unknown_type():
assert _transcription_usage_cost({}, {}) == 0.0
def test_realtime_transcription_prices_a_clip_at_the_latest_configured_transcription_model(monkeypatch):
monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")
monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url=""))
from litellm.cost_calculator import handle_realtime_transcription_cost_calculation
realtime_model = "gpt-realtime-2.1-mini"
first_asr, latest_asr = "gpt-4o-transcribe", "gpt-transcribe"
first_rate = litellm.model_cost[first_asr]["input_cost_per_second"]
latest_rate = litellm.model_cost[latest_asr]["input_cost_per_second"]
assert first_rate != latest_rate and latest_rate > 0
def session_frame(frame_type: str, asr_model: str | None) -> dict[str, object]:
transcription = None if asr_model is None else {"model": asr_model}
return {"type": frame_type, "session": {"model": realtime_model, "audio": {"input": {"transcription": transcription}}}}
results: OpenAIRealtimeStreamList = [
session_frame("session.created", None),
session_frame("session.updated", first_asr),
session_frame("session.updated", latest_asr),
{"type": "conversation.item.input_audio_transcription.completed", "usage": {"type": "duration", "seconds": 5}},
]
cost = handle_realtime_transcription_cost_calculation(
results=results, custom_llm_provider="openai", litellm_model_name=realtime_model
)
assert cost == pytest.approx(5 * latest_rate, rel=1e-9)
def test_get_transcription_model_falls_back_to_session_model(monkeypatch):
"""session.model is used when transcription-specific model fields are absent."""
monkeypatch.setenv("LITELLM_LOCAL_MODEL_COST_MAP", "True")