wide-dress-57902
08/12/2025, 8:12 PMtall-airline-16756
08/12/2025, 8:12 PMtall-airline-16756
08/12/2025, 8:12 PMsession.on("user_state_changed", callback)
helps you keep track of when someone starts or stops speaking.
• Agent state changes: For example, session.on("agent_state_changed", callback)
lets you monitor changes like when an agent is initializing or speaking.
• Transcription events: For example, session.on("user_input_transcribed", callback)
is great for handling finalized speech transcriptions from users.
• Conversation events: For example, session.on("conversation_item_added", callback)
helps you catch new messages as they come into the conversation.
Using this method is key to creating interactive and responsive apps with LiveKit. If you have any questions or need a hand setting it up, just let me know! Sources: Turn detection and interruptions | LiveKit Docs | basics/repeater.py | tests/test_agent_session.pywide-dress-57902
08/12/2025, 8:19 PMtall-airline-16756
08/12/2025, 8:19 PMfrom livekit.agents import AgentSession, MetricsCollectedEvent
from livekit.agents.metrics import STTMetrics
2. Implement the Event Handler: Next, create a function that listens for the metrics_collected
event and handles the `STTMetrics`: @session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
if isinstance(ev.metrics, STTMetrics):
print(f"STT Metrics - Audio Duration: {ev.metrics.audio_duration}s")
print(f"Total Duration: {ev.metrics.duration}s")
3. Log or Process Metrics: You can also use the handy log_metrics
function for nicely formatted logging: from livekit.agents.metrics import log_metrics
@session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
log_metrics(ev.metrics)
4. Aggregate Metrics (Optional): If you want to gather metrics over the whole session, the UsageCollector
is your friend: from livekit.agents.metrics import UsageCollector
usage_collector = UsageCollector()
@session.on("metrics_collected")
def on_stt_metrics_collected(ev: MetricsCollectedEvent):
usage_collector.collect(ev.metrics)
async def log_usage():
summary = usage_collector.get_summary()
print(f"Usage Summary: {summary}")
session.add_shutdown_callback(log_usage)
This way, you can easily handle and work with metrics like STTMetrics. If anything’s unclear or you’d like to see more examples, just let me know—I’m here to help!wide-dress-57902
08/12/2025, 8:21 PMtall-airline-16756
08/12/2025, 8:22 PMAgentSession
sends out whenever it gathers any metrics. It can include info from different parts like LLM, TTS, STT, and VAD. Think of it as a way to log and gather overall usage stats throughout the session.
• STT Metrics Event: This one’s all about Speech-to-Text specifically. It gives you details such as: Basically, it zooms in on how well the speech recognition and audio processing are performing.
◦ `audio_duration`: How long the audio input is, in seconds.
◦ `duration`: How much time it took to process the audio (this is 0 if it’s streaming STT).
◦ `streamed`: Whether the STT is running in streaming mode.
Here are the key differences to keep in mind:
• Scope: The metrics_collected_event
covers a wide range of metrics, while the STT metrics events are all about speech-to-text.
• Content: metrics_collected_event
pulls in data from multiple components, whereas STT metrics focus just on transcription performance.
• Usage: Use metrics_collected_event
when you want general metrics, and turn to STT metrics events when you’re specifically monitoring speech-to-text functionality.
If anything’s unclear or you want to dive deeper, just let me know—I’m happy to help! Sources: Logs, metrics, and telemetry | LiveKit Docs | metrics/send-metrics-to-3p/send_metrics_to_3p.py | livekit-agents/livekit/agents/voice/agent_activity.pywide-dress-57902
08/12/2025, 8:23 PMtall-airline-16756
08/12/2025, 8:23 PM