how can i log total latency at every time in my en...
# ask-ai
c
how can i log total latency at every time in my entrypoint
t
Thanks for reaching out, parag. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To log latency in a LiveKit application, you can use the
metrics
module and subscribe to the
metrics_collected
event. Here’s an example implementation:
Copy code
# 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 to format and log based on metrics type
    metrics.log_metrics(mtrcs)
You can calculate the total conversation latency using the formula:
Copy code
total_latency = eou.end_of_utterance_delay + llm.ttft + tts.ttfb
Where: - end_of_utterance_delay: Time between VAD detecting end of speech and LLM inference. - ttft: Time to first token (LLM generating the first token). - ttfb: Time to first byte (TTS generating the first byte of audio). Let me know if you need further assistance! Sources: Capturing metrics | LiveKit Docs | Capturing metrics | LiveKit Docs
c
will this work
Copy code
@session.on("metrics_collected")
    def _on_metrics_collected(ev: MetricsCollectedEvent):
        metrics = ev.metrics
        total_latency = (
            metrics.eou.end_of_utterance_delay + metrics.llm.ttft + metrics.tts.ttfb
        )
        <http://logger.info|logger.info>(f"current total latency - {total_latency}")
        metrics.log_metrics(metrics=ev.metrics, logger=logger)
t
Your implementation appears to align well with the general approach for handling the
metrics_collected
event in LiveKit. Here’s a breakdown:
Copy code
@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    metrics = ev.metrics
    total_latency = (
        metrics.eou.end_of_utterance_delay + metrics.llm.ttft + metrics.tts.ttfb
    )
    <http://logger.info|logger.info>(f"current total latency - {total_latency}")
    metrics.log_metrics(metrics=ev.metrics, logger=logger)
This should work as long as: - The
metrics
object contains the
eou
,
llm
, and
tts
attributes with the respective properties. - The
log_metrics
function is correctly implemented to handle the
metrics
object. If you encounter any issues, feel free to share the error or behavior, and I’ll assist further! Sources: Capturing metrics | LiveKit Docs | Capturing metrics | LiveKit Docs