Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

ops: Add OTEL metric for STT latency

Clémence Lesnéa2b484c314e4
diff --git a/app/helpers/call_llm.py b/app/helpers/call_llm.py
index 862192d..0b9ff67 100644
--- a/app/helpers/call_llm.py
+++ b/app/helpers/call_llm.py
@@ -39,6 +39,7 @@ from app.helpers.monitoring import (
SpanAttributeEnum,
call_answer_latency,
call_cutoff_latency,
+ call_stt_complete_latency,
gauge_set,
tracer,
)
@@ -233,6 +234,17 @@ async def load_llm_chat( # noqa: PLR0913, PLR0915
if wait:
await last_chat
+ async def _compute_stt_metrics() -> None:
+ """
+ Report the recognition latency.
+ """
+ start = time.monotonic()
+ await stt_complete_gate.wait()
+ gauge_set(
+ metric=call_stt_complete_latency,
+ value=time.monotonic() - start,
+ )
+
async def _response_callback(_retry: bool = False) -> None:
"""
Triggered when the audio buffer needs to be processed.
@@ -243,6 +255,9 @@ async def load_llm_chat( # noqa: PLR0913, PLR0915
nonlocal answer_start
answer_start = time.monotonic()
+ # Report the STT metrics
+ stt_metrics_task = asyncio.create_task(_compute_stt_metrics())
+
# Wait the complete recognition for 50ms maximum
try:
await asyncio.wait_for(stt_complete_gate.wait(), timeout=0.05)
@@ -277,8 +292,11 @@ async def load_llm_chat( # noqa: PLR0913, PLR0915
)
)
- # Process the response
- await _commit_answer(wait=True)
+ # Process the response and wait for latency metrics
+ await asyncio.gather(
+ _commit_answer(wait=False),
+ stt_metrics_task,
+ )
# First call
if len(call.messages) <= 1:
diff --git a/app/helpers/monitoring.py b/app/helpers/monitoring.py
index b394e32..291ffa6 100644
--- a/app/helpers/monitoring.py
+++ b/app/helpers/monitoring.py
@@ -67,6 +67,8 @@ class SpanMeterEnum(str, Enum):
"""Audio frames in latency in seconds."""
CALL_FRAMES_OUT_LATENCY = "call.frames.out.latency"
"""Audio frames out latency in seconds."""
+ CALL_STT_COMPLETE_LATENCY = "call.stt.complete.latency"
+ """Speech-to-text missed complete latency."""
def counter(
self,
@@ -127,6 +129,7 @@ call_answer_latency = SpanMeterEnum.CALL_ANSWER_LATENCY.gauge("s")
call_cutoff_latency = SpanMeterEnum.CALL_CUTOFF_LATENCY.gauge("s")
call_frames_in_latency = SpanMeterEnum.CALL_FRAMES_IN_LATENCY.gauge("s")
call_frames_out_latency = SpanMeterEnum.CALL_FRAMES_OUT_LATENCY.gauge("s")
+call_stt_complete_latency = SpanMeterEnum.CALL_STT_COMPLETE_LATENCY.gauge("s")
def gauge_set(