-
[BE] fix: discount audio input tokens in cost for OpenAI audio models (#7137)
发布于
2026-06-25 14:56:48 +00:00 - [BE] fix: discount audio input tokens in cost for OpenAI audio models
23 OpenAI models in model_prices_and_context_window.json publish
input_cost_per_audio_token at a rate distinct from input_cost_per_token
(gpt-4o-audio-preview, gpt-4o-realtime, gpt-audio families). For
gpt-4o-audio-preview the audio input rate is 16x the text input rate
($4e-5 vs $2.5e-6 per token). The Java backend had zero references to
this JSON field, so audio input tokens were silently billed at the
standard text input rate.Wires input_cost_per_audio_token through ModelCostData -> ModelPrice ->
CostService, and updates SpanCostCalculator.textGenerationCost to read
the audio token count from the OTel keys logged by the Python SDK
(original_usage.prompt_tokens_details.audio_tokens with a bare-key
fallback). When the configured audio rate is zero, behavior matches
the previous code path; otherwise the (prompt_tokens - audio_tokens)
remainder is billed at the standard input rate and the audio tokens at
the configured audio rate.Test calculateCostBillsAudioPromptTokensAtAudioRateForOpenAiAudioModels
mutation-verifies the new behavior: without the SpanCostCalculator
change it fails (expected 0.01575, actual 0.0045). All 80 existing
CostServiceTest and SpanCostCalculatorTest assertions remain green.Output audio tokens (output_cost_per_audio_token) are an intentional
follow-up so this PR stays focused on the prompt side.Co-Authored-By: Claude Opus 4.7 (1M context) noreply@anthropic.com
- [BE] test: cover audio-token branches with parameterized cases
Address review feedback from #7137:
- Move the audio-token narrative comment to a javadoc block on the test
method (nit). - Refactor to @ParameterizedTest covering all three branches in the
updated textGenerationCost:- gpt-4o-audio-preview with original_usage.prompt_tokens_details.audio_tokens
(Python SDK 1.6.0+ key) -> 0.01575 - gpt-4o-audio-preview with bare prompt_tokens_details.audio_tokens
(OTel fallback key) -> 0.01575 - gpt-4o-mini (no input_cost_per_audio_token configured) so
inputAudioRate == 0 and the audio key is ignored -> 0.00027
- gpt-4o-audio-preview with original_usage.prompt_tokens_details.audio_tokens
Mutation-tested: reverting SpanCostCalculator to main makes both audio-rate
cases fail (0.0045 vs 0.01575) while the no-audio case continues to pass,
confirming each branch is independently exercised.
Co-authored-by: Andres Cruz andresc@comet.com
下载附件