gentle-dream-14103
09/17/2025, 1:45 PMtall-airline-16756
09/17/2025, 1:45 PMtall-airline-16756
09/17/2025, 1:46 PMfrom 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