Skip to content

Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.

fix(python-sdk): compute AssemblyAI cost from audio duration (#1381)

Josh Kappler585c884d707f
diff --git a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
index 1a45fe1b..659fe5e7 100644
--- a/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
+++ b/sdk/python/src/openlit/instrumentation/assemblyai/utils.py
@@ -214,9 +214,12 @@ def common_audio_logic(
prompt = scope._response.audio_url
request_model = scope._kwargs.get("speech_model", "best")
- # Compute cost
+ # Compute cost. AssemblyAI bills per second of audio, not per character.
cost = get_audio_model_cost(
- request_model, pricing_info, prompt, scope._response.audio_duration
+ request_model,
+ pricing_info,
+ prompt=None,
+ duration=scope._response.audio_duration,
)
# Common Span Attributes
diff --git a/sdk/python/tests/test_assemblyai_cost.py b/sdk/python/tests/test_assemblyai_cost.py
new file mode 100644
index 00000000..36280f2a
--- /dev/null
+++ b/sdk/python/tests/test_assemblyai_cost.py
@@ -0,0 +1,103 @@
+# pylint: disable=missing-function-docstring, missing-class-docstring, too-few-public-methods, protected-access
+"""
+Unit tests for AssemblyAI transcription cost calculation.
+
+AssemblyAI pricing is per second of audio, so the cost recorded by
+``common_audio_logic`` must be derived from ``audio_duration`` and must not
+vary with the length of the ``audio_url`` string.
+"""
+
+import pytest
+
+from openlit import OpenlitConfig
+from openlit.instrumentation.assemblyai.utils import common_audio_logic
+from openlit.semcov import SemanticConvention
+
+
+PRICING = {"audio": {"best": 0.00010277777, "nano": 3.333333e-05}}
+
+
[email protected](autouse=True)
+def _reset_config():
+ """common_audio_logic reads global config normally populated by openlit.init()."""
+ OpenlitConfig.reset_to_defaults()
+
+
+class _Span:
+ def __init__(self):
+ self.attributes = {}
+
+ def set_attribute(self, key, value):
+ self.attributes[key] = value
+
+ def set_status(self, status):
+ pass
+
+ def add_event(self, *args, **kwargs):
+ pass
+
+
+class _Response:
+ def __init__(self, audio_url, audio_duration):
+ self.audio_url = audio_url
+ self.audio_duration = audio_duration
+ self.id = "transcript-id"
+ self.text = "transcribed text"
+
+
+def _run(audio_url, audio_duration, speech_model="best", capture_message_content=False):
+ scope = type("GenericScope", (), {})()
+ scope._span = _Span()
+ scope._start_time = 0.0
+ scope._end_time = 1.0
+ scope._tbt = 0.0
+ scope._ttft = 1.0
+ scope._server_address, scope._server_port = "api.assemblyai.com", 443
+ scope._kwargs = {"speech_model": speech_model}
+ scope._response = _Response(audio_url, audio_duration)
+ scope._response_model = speech_model
+
+ common_audio_logic(
+ scope,
+ PRICING,
+ "test-environment",
+ "test-application",
+ None,
+ capture_message_content,
+ True,
+ "1.0.0",
+ False,
+ )
+ return scope._span.attributes
+
+
+def _cost(*args, **kwargs):
+ return _run(*args, **kwargs)[SemanticConvention.GEN_AI_USAGE_COST]
+
+
+class TestAssemblyAICost:
+ def test_cost_derived_from_duration(self):
+ cost = _cost("https://cdn.assemblyai.com/upload/abc", 7200)
+ assert cost == pytest.approx(7200 * PRICING["audio"]["best"], rel=1e-9)
+
+ def test_cost_independent_of_audio_url_length(self):
+ short_url = _cost("https://a.co/x", 600)
+ long_url = _cost("https://cdn.example.com/upload/" + "q" * 200, 600)
+ assert short_url == long_url
+
+ def test_nano_model_rate(self):
+ cost = _cost("https://cdn.assemblyai.com/upload/abc", 600, speech_model="nano")
+ assert cost == pytest.approx(600 * PRICING["audio"]["nano"], rel=1e-9)
+
+ def test_unknown_model_returns_zero(self):
+ assert (
+ _cost("https://cdn.assemblyai.com/upload/abc", 600, speech_model="x") == 0
+ )
+
+ def test_audio_url_still_recorded_as_input_content(self):
+ audio_url = "https://cdn.assemblyai.com/upload/abc"
+ attributes = _run(audio_url, 600, capture_message_content=True)
+ assert audio_url in attributes[SemanticConvention.GEN_AI_INPUT_MESSAGES]
+ assert attributes[SemanticConvention.GEN_AI_USAGE_COST] == pytest.approx(
+ 600 * PRICING["audio"]["best"], rel=1e-9
+ )
diff --git a/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts b/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts
index 1b61c733..1d49c90a 100644
--- a/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts
+++ b/sdk/typescript/src/instrumentation/__tests__/assemblyai-wrapper.test.ts
@@ -172,6 +172,14 @@ describe('AssemblyAIWrapper', () => {
])
);
+ // Cost must use duration, not audio_url character length (Python parity).
+ expect(OpenLitHelper.getAudioModelCost).toHaveBeenCalledWith(
+ 'best',
+ {},
+ '',
+ 42
+ );
+
expect(metricParams).toEqual({
genAIEndpoint: 'assemblyai.transcripts.transcribe',
model: 'best',
@@ -298,6 +306,34 @@ describe('AssemblyAIWrapper', () => {
expect(metricParams.model).toBe('nano');
});
+ it('should compute cost from audio duration even when audio_url is present', () => {
+ (OpenlitConfig as any).pricingInfo = { audio: { best: 0.001 } };
+ (OpenlitConfig as any).disableEvents = true;
+ (OpenlitConfig as any).captureMessageContent = false;
+ const costSpy = jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.06);
+
+ AssemblyAIWrapper._commonAudioSetter({
+ args: [{ audio: 'https://cdn.example.com/upload/' + 'q'.repeat(200) }],
+ genAIEndpoint: 'assemblyai.transcripts.transcribe',
+ response: {
+ id: 'transcript-long-url',
+ text: 'done',
+ speech_model: 'best',
+ audio_url: 'https://cdn.example.com/upload/' + 'q'.repeat(200),
+ audio_duration: 60,
+ },
+ span,
+ isStream: false,
+ });
+
+ expect(costSpy).toHaveBeenCalledWith(
+ 'best',
+ { audio: { best: 0.001 } },
+ '',
+ 60
+ );
+ });
+
it('should pass audio duration to getAudioModelCost when audio_url is absent', () => {
(OpenlitConfig as any).pricingInfo = { audio: { best: 0.001 } };
(OpenlitConfig as any).disableEvents = true;
diff --git a/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts b/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts
index 16a3f5d6..a408cff5 100644
--- a/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts
+++ b/sdk/typescript/src/instrumentation/assemblyai/wrapper.ts
@@ -202,10 +202,15 @@ class AssemblyAIWrapper extends BaseWrapper {
span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);
const pricingInfo = OpenlitConfig.pricingInfo || {};
+ // AssemblyAI bills per second of audio (pricing.json best/nano rates).
+ // Do not pass audio_url as the prompt — getAudioModelCost treats a
+ // truthy prompt as character-length pricing (TTS-style), which made
+ // gen_ai.usage.cost track URL length instead of duration. Match Python
+ // assemblyai/utils.py: pass an empty prompt and the duration.
const cost = OpenLitHelper.getAudioModelCost(
requestModel,
pricingInfo,
- prompt,
+ '',
audioDuration
);