brainy-painter-59862
05/15/2025, 1:17 PMtall-airline-16756
05/15/2025, 1:17 PMtall-airline-16756
05/15/2025, 1:18 PM# Import the metrics module
from livekit.agents import metrics
# Subscribe to metrics collection events
@agent.on("metrics_collected")
def _on_metrics_collected(mtrcs: metrics.AgentMetrics):
# Use the helper function to format and log metrics based on type
metrics.log_metrics(mtrcs)
For more comprehensive tracking, you can use the UsageCollector class to aggregate metrics over a session:
# Create a usage collector
usage_collector = metrics.UsageCollector()
# Add metrics to the collector as they are received
@agent.on("metrics_collected")
def _on_metrics_collected(mtrcs: metrics.AgentMetrics):
# Pass metrics to the collector for aggregation
usage_collector.collect(mtrcs)
# Log aggregated summary at the end of the session
async def log_usage():
summary = usage_collector.get_summary()
<http://logger.info|logger.info>(f"Usage: {summary}")
# Add the summary logging as a shutdown callback
ctx.add_shutdown_callback(log_usage)
These steps will help you log detailed metrics about your agent's performance, including LLM responses and usage. Let me know if you need further assistance! Sources: Capturing metrics | LiveKit Docs | Capturing metrics | LiveKit Docs