how to get `STTMetrics`
# ask-ai
f
how to get
STTMetrics
t
Thanks for reaching out, jitendra. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
STTMetrics are generated automatically by the LiveKit Agents framework and are emitted as events, not retrieved via direct API calls. To collect STTMetrics, set up an event listener on your STT instance: • Python Example:
Copy code
from livekit.agents.metrics import STTMetrics

# Create your STT instance
stt = deepgram.STT()  # or another provider

def stt_metrics_wrapper(metrics: STTMetrics):
    asyncio.create_task(handle_stt_metrics(metrics))

stt.on("metrics_collected", stt_metrics_wrapper)

async def handle_stt_metrics(metrics: STTMetrics):
    print(f"STT Duration: {metrics.duration}")
    print(f"Audio Duration: {metrics.audio_duration}")
    print(f"Streamed: {metrics.streamed}")
• JavaScript Example:
Copy code
stt.on(SpeechEventType.METRICS_COLLECTED, (metrics) => {
    console.log('STT Metrics:', {
        requestId: metrics.requestId,
        duration: metrics.duration,
        audioDuration: metrics.audioDuration,
        streamed: metrics.streamed,
        timestamp: metrics.timestamp
    });
});
STTMetrics properties include: • `audioDuration`: Duration of audio input (seconds) • `duration`: Time to create transcript (0 for streaming) • `streamed`: Boolean for streaming mode • `requestId`: Unique request identifier • `timestamp`: When metrics were recorded • `label`: STT provider identifier • `error`: Any error (optional) Metrics are emitted after each
recognize()
call for non-streaming STT, or on
RECOGNITION_USAGE
events for streaming STT. Listen for the
metrics_collected
event to capture and process STTMetrics in your application. Sources: livekit-agents/livekit/agents/metrics/base.py | metrics/send-metrics-to-3p/send_metrics_to_3p.py | agents/src/metrics/base.ts | STTMetrics | LiveKit Agents