how do I calculate and log latency when using a sp...
# ask-ai
v
how do I calculate and log latency when using a speech to speech realtime model? (gpt-realtime) help with a python code sample
t
Thanks for reaching out, Ruchir. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To help you calculate and log latency for a speech-to-speech real-time model (like gpt-realtime) in Python, here’s a friendly guide you can follow: 1. Key Metrics to Track: • Time to First Token (TTFT): This measures the time from when you create the request until the first token is generated. • Total Duration: This is the total time from request creation to when the process finishes. • End-to-End Latency: This includes all delays from transcription, model processing, to text-to-speech conversion. 2. Python Code Sample:
Copy code
import time
from livekit.agents.metrics import RealtimeModelMetrics

# Here’s an example of how you can track latency
start_time = time.time()

# Simulate request creation and processing
# You’ll want to replace this with your actual request handling logic
response = model.generate(input_data)

# Now, calculate the latency metrics
end_time = time.time()
ttft = response.first_token_timestamp - start_time
total_duration = end_time - start_time

# Finally, log the metrics
print(f"Time to First Token (TTFT): {ttft:.3f}s")
print(f"Total Duration: {total_duration:.3f}s")
3. Advanced Tracking: If you’d like to capture more detailed insights like token counts and generation speed, the
RealtimeModelMetrics
class is a great tool to use for automatic logging. If you have any questions or want help with a more detailed implementation, please don’t hesitate to reach out—I’m here to help! Sources: livekit-plugins/livekit-plugins-ultravox/livekit/plugins/ultravox/realtime/... | Logs, metrics, and telemetry | LiveKit Docs | Operational Best Practices | livekit/livekit_composite | DeepWiki
v
how do I use
RealtimeModelMetrics
- to track latency in agent responding? and also the EOU delay (all of this for realtime mode)
t
I'm glad you're interested in tracking latency in agent responding and EOU (End-of-Utterance) delay in realtime mode! Here's a friendly guide to help you through it: 1. Tracking Agent Response Latency: • Time to First Token (TTFT): This measures the time from when the request is created to when the first token is generated. • Total Duration: This tracks the time from request creation until the response is fully completed. You can easily access these metrics using the
RealtimeModelMetrics
class:
Copy code
from livekit.agents.metrics import RealtimeModelMetrics

@agent.on("metrics_collected")
def on_metrics_collected(agent_metrics):
    if isinstance(agent_metrics, RealtimeModelMetrics):
        print(f"Agent response TTFT: {agent_metrics.ttft}s")
        print(f"Total response duration: {agent_metrics.duration}s")
2. Tracking EOU Delay: • End-of-Utterance Delay: This is the time between when the user finishes speaking and when the system decides to end the turn. • Transcription Delay: This measures how long it takes to get the transcript after the user stops speaking. For these, you can use the
EOUMetrics
class:
Copy code
from livekit.agents.metrics import EOUMetrics

@agent.on("metrics_collected")
def on_metrics_collected(agent_metrics):
    if isinstance(agent_metrics, EOUMetrics):
        print(f"EOU delay: {agent_metrics.end_of_utterance_delay}s")
        print(f"Transcription delay: {agent_metrics.transcription_delay}s")
3. Combining Metrics for Total Latency: To get a complete picture, you can combine these metrics like this:
Copy code
total_latency = eou.end_of_utterance_delay + llm.ttft + tts.ttfb
If you'd like, I’m here to help with any further questions or if you want a more detailed implementation. Just let me know! Sources: examples/src/comprehensive_test.ts | agent-examples/agent_examples/pipeline_agent.py | Logs, metrics, and telemetry | LiveKit Docs