Currently when i use the log metrics like this us...
# ask-ai
f
Currently when i use the log metrics like this usage_collector = metrics.UsageCollector() @session.on("metrics_collected") def _on_metrics_collected(ev: MetricsCollectedEvent): metrics.log_metrics(ev.metrics) usage_collector.collect(ev.metrics) summary = usage_collector.get_summary() The issue is that stt shows duration of 0, even though it did work where I was speaking to the agent. UsageSummary(llm_prompt_tokens=20890, llm_prompt_cached_tokens=0, llm_input_audio_tokens=0, llm_completion_tokens=1080, llm_output_audio_tokens=0, tts_characters_count=4392, tts_audio_duration=255.5993333333332, stt_audio_duration=0) How do i fix this?
t
Thanks for reaching out, Mok. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Could you please help me by confirming a few details? This will really help us get to the bottom of the issue: • Are you using streaming STT or batch STT? • Which STT provider are you working with (for example, Deepgram, AWS, Gladia)? • Have you subscribed to STTMetrics and noticed any events during your session? Thanks so much for your help! Sources: plugins/deepgram/src/stt.ts | livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | Logs, metrics, and telemetry | LiveKit Docs | metrics/metrics_stt.py | Docs::Agents
f
I am streaming the STT. session = AgentSession( userdata={"ctx": ctx}, llm=google.LLM(model="gemini-2.0-flash", api_key=config.GOOGLE_API_KEY), stt=google.STT( model="latest_long", spoken_punctuation=False, credentials_file=config.GOOGLE_APPLICATION_CREDENTIALS ), tts=google.TTS( language="en-US", voice_name="en-US-Chirp3-HD-Despina", credentials_file=config.GOOGLE_APPLICATION_CREDENTIALS ), vad=ctx.proc.userdata.get("vad"), turn_detection=MultilingualModel(), preemptive_generation=True, ) How do i subscrive to STTMetrics? THere is indeed stt and tts happening though, since i was conversing with the agent via livekit
t
To subscribe to STTMetrics in your setup, you can listen for the
metrics_collected
event on the
AgentSession
. Here's a friendly example in Python to help you get started:
Copy code
from livekit.agents import AgentSession, MetricsCollectedEvent
from livekit.agents.metrics import STTMetrics

session = AgentSession()

@session.on("metrics_collected")
def on_metrics_collected(ev: MetricsCollectedEvent):
    if isinstance(ev.metrics, STTMetrics):
        print("STT Metrics:", ev.metrics)
        # streamed=True indicates streaming mode
        # audio_duration: duration of audio processed
        # duration: 0.0 for streaming
This way, you’ll be able to capture and log the STT metrics, including
audio_duration
. If you have any questions or need a hand with anything else, just let me know—I’m here to help! Sources: Docs::Agents | Logs, metrics, and telemetry | LiveKit Docs | Docs::Agents
f
over here, you can see that it detects the user speaking, and converts it to text, but then why is the stt metric not logging then??
t
STT metrics might not be logged even when speech is detected and converted to text for a few different reasons. Let’s walk through some common areas to check together: • Event Subscription: First, make sure you’re subscribed to the
metrics_collected
event. Without this subscription, the metrics won’t be captured. Here’s a quick example to guide you:
Copy code
@session.on("metrics_collected")
def on_metrics_collected(ev):
    print(ev.metrics)
• STT Initialization: It’s important to double-check that the STT component is properly initialized and integrated into your session. For instance, confirm that your STT provider (like Google) is set up with valid credentials. • Streaming Mode: If you’re using streaming STT, keep in mind that metrics are linked to
RECOGNITION_USAGE
events. If these events aren’t firing, the metrics won’t be logged. So, ensure your STT provider is emitting these events regularly. • Realtime API Limitation: When using Realtime APIs, STT metrics might not be visible because they’re handled internally. • Error Handling: Also, check if there are any errors during recognition that might be stopping metrics from being emitted. Things like connection issues or retries could be blocking metric generation. To help you move forward, please verify your subscription to the metrics_collected event and confirm that your STT provider is emitting the RECOGNITION_USAGE events. If you’d like, I’m here to assist with specific configurations or walk you through debugging steps—just let me know! Sources: Logs, metrics, and telemetry | LiveKit Docs | Docs::Agents | Capturing metrics | LiveKit Docs | livekit-agents/livekit/agents/metrics/base.py
f
I have already subscribed to the events. How do i check that STT component is properly initialized though? because STT is indeed working though, but how do i stream the RECOGNITION_USAGE?? the logs show that indeed it detect the user speaking though, how do i even configure that STT provider is emitting the RECOGNITION_USAGE events?
t
The emission of
RECOGNITION_USAGE
events is thoughtfully managed by the STT provider's plugin during streaming transcription. To help you make sure these events are coming through smoothly: • Use Streaming Transcription: It’s important to use the
stream()
method for transcription because the
recognize()
method (which processes batches) doesn’t emit these events. • Provider Support: Double-check that your STT provider supports
RECOGNITION_USAGE
events. Providers like Deepgram, AssemblyAI, and Gladia are great examples that emit these events during streaming. • Event Subscription: Be sure to subscribe to the
metrics_collected
event and handle the
RECOGNITION_USAGE
event type within your application logic. For instance, with the Deepgram plugin,
RECOGNITION_USAGE
events are sent out every 5 seconds by default, based on internal tracking of audio duration. If you’re using Google STT, it’s a good idea to verify that your credentials and model configurations are set up correctly, since any misconfiguration might stop these events from being emitted properly. If you’d like, I’m here to help you check your STT provider’s setup or troubleshoot any issues you’re facing. Just let me know! Sources: plugins/deepgram/src/stt.ts | main.py | livekit-plugins/livekit-plugins-elevenlabs/livekit/plugins/elevenlabs/stt.p...
f
Event Subscription: Be sure to subscribe to the
metrics_collected
event and handle the
RECOGNITION_USAGE
event type within your application logic According to the documentation, isnt the code just like this to handle all events? usage_collector = metrics.UsageCollector() @session.on("metrics_collected") def _on_metrics_collected(ev: MetricsCollectedEvent): metrics.log_metrics(ev.metrics) usage_collector.collect(ev.metrics) summary = usage_collector.get_summary() Where i collect the metrics??
t
The
metrics.UsageCollector
actually takes care of
RECOGNITION_USAGE
events indirectly, and I’d love to walk you through how it all works: • Event Conversion: When the STT provider emits a
RECOGNITION_USAGE
event, LiveKit’s system steps in to convert it into an
STTMetrics
event. This event includes details like
audio_duration
and other important data. • UsageCollector Integration: The
UsageCollector
is set up to listen for
STTMetrics
events rather than
RECOGNITION_USAGE
directly. Once it receives an
STTMetrics
event, it adds the
audio_duration
into its internal summary. Your code snippet looks spot on for collecting metrics! If you’re still seeing
stt_audio_duration
as 0, it might mean that
RECOGNITION_USAGE
events aren’t being emitted or converted into
STTMetrics
as expected. This could happen because: • The STT provider might not be emitting
RECOGNITION_USAGE
events—sometimes this is due to configuration or session-related issues. • There could be errors in the STT plugin that are preventing the conversion to
STTMetrics
. To help you move forward, I’d suggest double-checking that the STT provider is properly configured and is indeed emitting those
RECOGNITION_USAGE
events. Please feel free to reach out if you want me to help you verify this or if you need any further troubleshooting support—I’m here to help! Sources: livekit-plugins/livekit-plugins-aws/livekit/plugins/aws/experimental/realti... | agents/src/inference/stt.ts | Docs::Agents