I am using the livekit's python agent. How to expo...
# ask-ai
c
I am using the livekit's python agent. How to expose?
Copy code
total_latency = eou.end_of_utterance_delay + llm.ttft + tts.ttfb
?
t
Thanks for reaching out, Rashmi. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To expose the
total_latency
variable in LiveKit's Python agent, you can compute it by capturing individual metrics and processing them. Here's how you can do it: • Subscribe to the
metrics_collected
event on the
AgentSession
to capture metrics like
eou.end_of_utterance_delay
,
llm.ttft
, and
tts.ttfb
. • Calculate
total_latency
as the sum of these metrics. • Log or expose the computed
total_latency
using the
log_metrics
helper function or by sending it to an external system. Here is an example of how to calculate and log the total latency:
Copy code
class YourAgent(Agent):
    def __init__(self):
        super().__init__()
        self.eou_delay = 0
        self.llm_ttft = 0
        self.tts_ttfb = 0

    async def on_metrics_collected(self, metrics):
        if isinstance(metrics, LLMMetrics):
            self.llm_ttft = metrics.ttft
        elif isinstance(metrics, TTSMetrics):
            self.tts_ttfb = metrics.ttfb
        elif isinstance(metrics, EOUMetrics):
            self.eou_delay = metrics.end_of_utterance_delay

        total_latency = self.eou_delay + self.llm_ttft + self.tts_ttfb
        <http://logger.info|logger.info>(f"Total latency: {total_latency}")
You can also send the metrics to a third-party server for further analysis. Let me know if you need help implementing this or have additional questions!
c
I just want to log the metric for each turn of the agent
t
To log the
total_latency
metric for each turn of the agent, you can tweak the
on_metrics_collected
method to calculate and log the metric whenever new metrics come in. Here’s a friendly example to get you started:
Copy code
class YourAgent(Agent):
    def __init__(self):
        super().__init__()
        self.eou_delay = 0
        self.llm_ttft = 0
        self.tts_ttfb = 0

    async def on_metrics_collected(self, metrics):
        if isinstance(metrics, LLMMetrics):
            self.llm_ttft = metrics.ttft
        elif isinstance(metrics, TTSMetrics):
            self.tts_ttfb = metrics.ttfb
        elif isinstance(metrics, EOUMetrics):
            self.eou_delay = metrics.end_of_utterance_delay

        # Calculate total latency
        total_latency = self.eou_delay + self.llm_ttft + self.tts_ttfb

        # Log the total latency for this turn
        <http://logger.info|logger.info>(f"Total latency for this turn: {total_latency}")
This way, you’ll have the
total_latency
logged for every turn your agent takes. If you want to dive deeper or need a hand with anything else, just let me know!
c
thanks. But how is it ensuring that its being logged at the end of Agent's turn?
t
To make sure the
total_latency
metric gets logged right at the end of the agent's turn, you can tap into the
metrics_collected
event to grab and log those metrics. Here’s a friendly walkthrough on how to do it: • Subscribe to the
metrics_collected
event to catch metrics like
eou.end_of_utterance_delay
,
llm.ttft
, and
tts.ttfb
. • Calculate the
total_latency
by adding them up:
total_latency = eou.end_of_utterance_delay + llm.ttft + tts.ttfb
. • Log this total latency when the agent’s state switches to show it’s done with its turn. For example, use the
agent_state_changed
event to know when the agent stops speaking. Here’s a simple example to get you started:
Copy code
@session.on("metrics_collected")
def on_metrics_collected(ev):
    # Grab each metric
    eou_delay = ev.metrics.get("end_of_utterance_delay", 0)
    llm_ttft = ev.metrics.get("ttft", 0)
    tts_ttfb = ev.metrics.get("ttfb", 0)

    # Add them up for total latency
    total_latency = eou_delay + llm_ttft + tts_ttfb

    # Log it out
    <http://logger.info|logger.info>(f"Total latency: {total_latency}")

@session.on("agent_state_changed")
def on_agent_state_changed(ev):
    if ev.new_state == "speaking":
        <http://logger.info|logger.info>("Agent has completed its turn.")
This way, you’ll have the
total_latency
nicely logged right when the agent wraps up its turn. If anything’s unclear or you want to dive deeper, just let me know—I’m here to help! Sources: Capturing metrics | LiveKit Docs | Logs, metrics, and telemetry | LiveKit Docs