could you show example with agents fremawork , how...
# ask-ai
g
could you show example with agents fremawork , how to calculate end to end latency. pls show full code example , that we could copy and run
t
Thanks for reaching out, Andrii. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'd love to help you calculate end-to-end latency using the LiveKit Agents framework! You can take advantage of the built-in metrics that track performance at different stages of the agent pipeline. Here’s a friendly Python example that shows how to gather individual latency components and compute the total end-to-end latency:
Copy code
from livekit.agents import JobContext, WorkerOptions, cli, Agent, metrics
from livekit.plugins import deepgram, openai, silero

class LatencyMonitoringAgent(Agent):
    def __init__(self):
        super().__init__(
            stt=deepgram.STT(),
            llm=openai.LLM(),
            tts=openai.TTS(),
            vad=silero.VAD(),
        )

        # Track metrics
        self.eou_delay = 0.0
        self.llm_ttft = 0.0
        self.tts_ttfb = 0.0

    async def on_eou_metrics_collected(self, mtrcs: metrics.EOUMetrics):
        self.eou_delay = mtrcs.end_of_utterance_delay
        self._calculate_total_latency()

    async def on_llm_metrics_collected(self, mtrcs: metrics.LLMMetrics):
        self.llm_ttft = mtrcs.ttft
        self._calculate_total_latency()

    async def on_tts_metrics_collected(self, mtrcs: metrics.TTSMetrics):
        self.tts_ttfb = mtrcs.ttfb
        self._calculate_total_latency()

    def _calculate_total_latency(self):
        total_latency = self.eou_delay + self.llm_ttft + self.tts_ttfb
        print(f"End-to-end latency: {total_latency:.4f} seconds")

async def entrypoint(ctx: JobContext):
    agent = LatencyMonitoringAgent()

    # Subscribe to metrics events
    ctx.agent.on("eou_metrics_collected", agent.on_eou_metrics_collected)
    ctx.agent.on("metrics_collected", lambda evt: (
        agent.on_llm_metrics_collected(evt.metrics) if isinstance(evt.metrics, metrics.LLMMetrics) else None
    ))
    ctx.agent.on("metrics_collected", lambda evt: (
        agent.on_tts_metrics_collected(evt.metrics) if isinstance(evt.metrics, metrics.TTSMetrics) else None
    ))

    # Start agent session
    await ctx.connect()

if __name__ == "__main__":
    cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
This snippet is designed to capture metrics like end-of-utterance delay, LLM time-to-first-token, and TTS time-to-first-byte, then neatly sum them up to give you the total latency. Feel free to copy and run this example to keep an eye on latency in your LiveKit Agent setup. If you have any questions or need a hand with this, I’m here to help! Sources: Capturing metrics | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs