From 5cc33ffc4a445cde3c400ac8d80a3ee934af888b Mon Sep 17 00:00:00 2001 From: Daniel Salazar Date: Wed, 25 Nov 2026 17:13:31 -0700 Subject: [PATCH] ai(alibaba): bill Omni prompt audio at the audio rate --- .../alibaba/chat/AlibabaProvider.test.ts | 141 ++++++++++++++++++ .../providers/alibaba/chat/AlibabaProvider.ts | 23 +++ .../ai/providers/alibaba/chat/models.ts | 10 +- src/docs/src/AI/chat.md | 2 + 4 files changed, 174 insertions(+), 2 deletions(-) diff --git a/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.test.ts b/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.test.ts index 653aedd6d..e9e600408 100644 --- a/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.test.ts +++ b/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.test.ts @@ -627,6 +627,147 @@ describe('AlibabaProvider.complete non-stream output', () => { }); }, ); + + it.each([ + ['qwen3-omni-flash', 381], + ['qwen3.5-omni-flash', 300], + ['qwen3.5-omni-plus', 1100], + ])('bills prompt audio on %s at its audio rate', async (modelId, audioRate) => { + const { provider } = makeProvider(); + createMock.mockResolvedValueOnce({ + choices: [ + { + message: { content: 'ok', role: 'assistant' }, + finish_reason: 'stop', + }, + ], + usage: { + prompt_tokens: 176, + completion_tokens: 96, + prompt_tokens_details: { + cached_tokens: 0, + audio_tokens: 114, + text_tokens: 62, + }, + }, + }); + + const result = await withTestActor(() => + provider.complete({ + model: modelId, + messages: [{ role: 'user', content: 'transcribe' }], + }), + ); + + const model = ALIBABA_MODELS.find((m) => m.id === modelId)!; + expect(model.costs.audio_tokens).toBe(audioRate); + const [usage, , , overrides] = recordSpy.mock.calls[0]!; + expect(usage).toEqual({ + prompt_tokens: 62, + audio_tokens: 114, + completion_tokens: 96, + cached_tokens: 0, + }); + expect(overrides).toEqual({ + prompt_tokens: 62 * Number(model.costs.prompt_tokens), + audio_tokens: 114 * audioRate, + completion_tokens: 96 * Number(model.costs.completion_tokens), + cached_tokens: 0, + }); + expect( + (result as { usageDetails?: unknown }).usageDetails, + ).toMatchObject({ inputTokens: 176, outputTokens: 96 }); + }); + + it('bills streamed prompt audio at the audio rate when the live catalog lists the model', async () => { + axiosGetMock.mockResolvedValueOnce({ + data: { + success: true, + output: { + total: 1, + page_no: 1, + models: [ + { + model: 'qwen3.5-omni-flash', + capabilities: ['TG', 'VU'], + inference_metadata: { + request_modality: [ + 'Text', + 'Image', + 'Audio', + 'Video', + ], + response_modality: ['Text', 'Audio'], + }, + }, + ], + }, + }, + }); + const { provider } = makeProvider(); + createMock.mockReturnValueOnce( + asAsyncIterable([ + { choices: [{ delta: { content: 'ok' } }] }, + { + choices: [], + usage: { + prompt_tokens: 176, + completion_tokens: 96, + prompt_tokens_details: { audio_tokens: 114 }, + }, + }, + ]), + ); + + const result = await withTestActor(() => + provider.complete({ + model: 'qwen3.5-omni-flash', + messages: [{ role: 'user', content: 'transcribe' }], + stream: true, + }), + ); + const harness = makeCapturingChatStream(); + await ( + result as { + init_chat_stream: (p: { chatStream: unknown }) => Promise; + } + ).init_chat_stream({ chatStream: harness.chatStream }); + + const [usage, , , overrides] = recordSpy.mock.calls[0]!; + expect(usage).toMatchObject({ prompt_tokens: 62, audio_tokens: 114 }); + expect(overrides).toMatchObject({ audio_tokens: 114 * 300 }); + }); + + it('keeps one input rate for prompt audio on qwen3.8-omni-flash', async () => { + const { provider } = makeProvider(); + createMock.mockResolvedValueOnce({ + choices: [ + { + message: { content: 'ok', role: 'assistant' }, + finish_reason: 'stop', + }, + ], + usage: { + prompt_tokens: 176, + completion_tokens: 96, + prompt_tokens_details: { cached_tokens: 0, audio_tokens: 114 }, + }, + }); + + await withTestActor(() => + provider.complete({ + model: 'qwen3.8-omni-flash', + messages: [{ role: 'user', content: 'transcribe' }], + }), + ); + + const [usage] = recordSpy.mock.calls[0]!; + expect(usage).toEqual({ + prompt_tokens: 176, + completion_tokens: 96, + cached_tokens: 0, + }); + }); }); // ── Streaming deltas ──────────────────────────────────────────────── diff --git a/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.ts b/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.ts index 3360f514b..4f2b8d763 100644 --- a/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.ts +++ b/src/backend/drivers/ai/providers/alibaba/chat/AlibabaProvider.ts @@ -26,7 +26,9 @@ import { sdkClientOptions } from '../../../interfaces/chat/utils/sdkClient.js'; import { type ChatProviderConfig, OpenAICompatProvider, + type UsageSource, } from '../../../protocols/openaiCompatible/OpenAICompatProvider.js'; +import * as OpenAIUtil from '../../../protocols/openaiCompatible/OpenAIUtil.js'; import { ALIBABA_MODELS } from './models.js'; export class AlibabaProvider extends OpenAICompatProvider { @@ -60,6 +62,27 @@ export class AlibabaProvider extends OpenAICompatProvider { return this.#catalog(); } + /** Prompt audio is billed at the model's audio rate where it has one. */ + protected override meteredUsage(source: UsageSource, model: IChatModel) { + const metered = super.meteredUsage(source, model); + if (model.costs.audio_tokens === undefined) return metered; + const { usage } = metered; + const reported = source.usage?.prompt_tokens_details?.audio_tokens; + const audioTokens = + typeof reported === 'number' && Number.isFinite(reported) + ? Math.min(Math.max(0, reported), usage.prompt_tokens) + : 0; + return { + ...metered, + usage: { + ...usage, + prompt_tokens: usage.prompt_tokens - audioTokens, + audio_tokens: audioTokens, + }, + usageDetails: OpenAIUtil.usageDetailsFromTrackedUsage(usage), + }; + } + #catalog = cachedRemoteCatalog({ name: 'Alibaba catalog', fallback: ALIBABA_MODELS, diff --git a/src/backend/drivers/ai/providers/alibaba/chat/models.ts b/src/backend/drivers/ai/providers/alibaba/chat/models.ts index 2fda60cd7..7fdb7a0a7 100644 --- a/src/backend/drivers/ai/providers/alibaba/chat/models.ts +++ b/src/backend/drivers/ai/providers/alibaba/chat/models.ts @@ -527,7 +527,8 @@ export const ALIBABA_MODELS: IChatModel[] = [ completion_tokens: 160, }, }, - // -- Omni models (text interface, audio costs excluded) --------- + // -- Omni models ------------------------------------------------ + // Rates per input modality: https://www.alibabacloud.com/help/en/model-studio/model-pricing { puterId: 'alibaba:qwen/qwen3-omni-flash', id: 'qwen3-omni-flash', @@ -550,6 +551,7 @@ export const ALIBABA_MODELS: IChatModel[] = [ costs: { tokens: 1_000_000, prompt_tokens: 43, + audio_tokens: 381, completion_tokens: 166, }, }, @@ -907,7 +909,8 @@ export const ALIBABA_MODELS: IChatModel[] = [ completion_tokens: 600, }, }, - // -- Omni additions (text interface, audio costs excluded) ---- + // -- Omni additions --------------------------------------------- + // Rates per input modality: https://www.alibabacloud.com/help/en/model-studio/model-pricing { puterId: 'alibaba:qwen/qwen3.5-omni-flash', id: 'qwen3.5-omni-flash', @@ -929,6 +932,7 @@ export const ALIBABA_MODELS: IChatModel[] = [ costs: { tokens: 1_000_000, prompt_tokens: 40, + audio_tokens: 300, completion_tokens: 220, }, }, @@ -953,6 +957,7 @@ export const ALIBABA_MODELS: IChatModel[] = [ costs: { tokens: 1_000_000, prompt_tokens: 140, + audio_tokens: 1100, completion_tokens: 830, }, }, @@ -974,6 +979,7 @@ export const ALIBABA_MODELS: IChatModel[] = [ input_cost_key: 'prompt_tokens', output_cost_key: 'completion_tokens', serviceSpeedVariants: { fast: 'qwen3.8-omni-flash-prime' }, + // One input rate for every modality, audio included. costs: { tokens: 1_000_000, prompt_tokens: 15, diff --git a/src/docs/src/AI/chat.md b/src/docs/src/AI/chat.md index c7108d4c5..a3c2e8395 100755 --- a/src/docs/src/AI/chat.md +++ b/src/docs/src/AI/chat.md @@ -626,6 +626,8 @@ Passing `service_tier: "auto"` selects fast service, with the same prices as `se Models with an equivalent Prime or Fast variant select that variant for fast service. Other models use standard service. +Qwen3-Omni Flash, Qwen3.5-Omni Flash, and Qwen3.5-Omni Plus charge audio input at the audio token rate, including the audio track of a video. + ### OpenRouter models Faster tiers come from the selected model's endpoint catalog. A tier can fall back upstream. Billing uses the endpoint that serves the request.