I am experimenting with live avatars using Hedra. ...
# ask-ai
r
I am experimenting with live avatars using Hedra. Is there a way to measure latency associated with Hedra?
t
Thanks for reaching out, Ivan. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hedra doesn’t provide specific latency metrics directly, but you can definitely measure the latency of its key components using LiveKit’s Agents Metrics API. Here are some important metrics to keep an eye on: • Time To First Token (TTFT): This measures the delay between sending transcribed speech to the LLM and receiving the very first token of the response. • Time To First Byte (TTFB): This tracks the delay from when the LLM returns text to when the TTS service produces the first audio chunk. • Speech-to-Text First Token (STT TTFT): This captures the time from when audio input is received to the first transcription output. You can also calculate the overall end-to-end latency by measuring the time from when the user starts speaking to when the avatar responds. To get this up and running, you’ll want to enable the Agents Metrics API and integrate OpenTelemetry for distributed tracing. If you’d like, I’m here to help guide you through the setup process—just let me know! Sources: avatars/hedra/README.md | Logs, metrics, and telemetry | LiveKit Docs | Sources/LiveKit/Protos/livekit_models.pb.swift | livekit-plugins/livekit-plugins-hedra/livekit/plugins/hedra/py.typed | Troubleshooting & Common Issues | livekit/livekit_composite | DeepWiki
r
How to calculate overall end-to-end latency? do you have an example?
t
To calculate overall end-to-end latency, a great way is to measure the time from when the user finishes speaking to when the agent (or avatar) starts responding. Let me walk you through a simple approach: 1. Capture Speech End Timestamp: Note the time when the user finishes speaking (for example, when the end-of-utterance is detected). 2. Capture Agent Response Start: Note the time when the agent begins speaking (like when the first audio output plays). 3. Calculate Latency: Subtract the speech end timestamp from the response start timestamp to find the total latency. Here’s a friendly Python example you can try out:
Copy code
import time

class LatencyTracker:
    def __init__(self):
        self.speech_end_time = None
        self.response_start_time = None

    def mark_speech_end(self):
        self.speech_end_time = time.time()

    def mark_response_start(self):
        self.response_start_time = time.time()

    def calculate_latency(self):
        if self.speech_end_time and self.response_start_time:
            return (self.response_start_time - self.speech_end_time) * 1000  # in ms
        return None

tracker = LatencyTracker()
tracker.mark_speech_end()  # Call this when the user finishes speaking
tracker.mark_response_start()  # Call this when the agent starts responding
latency = tracker.calculate_latency()
print(f"End-to-end latency: {latency} ms")
You can easily integrate this into your application to keep track of latency for each interaction. If you’d like, I’m here to help with any part of the implementation—just let me know! Sources: Operational Guidance & Best Practices | livekit/livekit_composite | DeepWik... | livekit-agents/livekit/agents/metrics/utils.py | Troubleshooting & Common Issues | livekit/livekit_composite | DeepWiki | agents/src/metrics/utils.ts | utils/rtpstats.go
r
give me examples for "*on_agent_state_changed"*
t
The on_agent_state_changed event is a helpful trigger that fires whenever the agent's conversational state changes, like moving between "listening," "thinking," or "speaking." Let me walk you through some examples to make it clearer: Python Example
Copy code
@session.on("agent_state_changed")
def on_agent_state_change(event):
    print(f"Agent state changed from {event.old_state} to {event.new_state}")

    if event.new_state == "thinking":
        log_agent_activity("Agent started processing request")
    elif event.new_state == "speaking":
        start_audio_visualizer()
    elif event.new_state == "listening":
        reset_user_interface()
React Frontend Example
Copy code
const { state } = useAgent();

useEffect(() => {
  if (state === 'thinking') {
    setShowThinkingIndicator(true);
  } else if (state === 'speaking') {
    startSpeechAnimation();
  } else if (state === 'listening') {
    setAgentReady(true);
  }
}, [state]);
Agent States • "initializing": The agent is setting up components. • "listening": The agent is waiting for user input. • "thinking": The agent is processing user input. • "speaking": The agent is generating or playing audio output. • "idle": The agent is connected but not engaged. If you have any questions or want me to help you with more examples or implementation tips, just let me know! I’m here to support you. Sources: packages/react/src/hooks/useAgent.ts | Web and mobile frontends | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_session.py | Events and error handling | LiveKit docs | web/src/data/agent.ts