Any guesses what would make the EOU randomly take ...
# agents
m
Any guesses what would make the EOU randomly take a SUPER long time to respond? For example, in the call in the image below, it was sometimes good, (~1 sec), other times it hit the timeout (~6 seconds), and other times it was super long (9 sec and 11 sec) the 6 seconds makes sense as it's hitting the
max_endpointing_delay
, but I can't figure out where that 9 and 11 is coming from Here's a link to the room on livekit dashboard
p
Hey where do you get the debug logs from as shown in the SS? i.e, corresponding to a speech_id
m
The metrics themselves come from the livekit metrics collected event, and then i have some code doing aggregation. Here's the code:
Copy code
# Track metrics for conversation latency calculation
    latest_eou_metrics = None
    latest_llm_metrics = None
    latest_tts_metrics = None

    # Track logged speech IDs to prevent duplicates
    logged_speech_ids = set()

    # Track all metrics for conversation-level averages
    all_eou_metrics = []
    all_llm_metrics = []
    all_tts_metrics = []

    @session.on("metrics_collected")
    def _on_metrics_collected(ev: MetricsCollectedEvent):
        nonlocal latest_eou_metrics, latest_llm_metrics, latest_tts_metrics

        # Track latest metrics for latency calculation
        if isinstance(ev.metrics, metrics.EOUMetrics):
            latest_eou_metrics = ev.metrics
            all_eou_metrics.append(ev.metrics)
        elif isinstance(ev.metrics, metrics.LLMMetrics):
            latest_llm_metrics = ev.metrics
            all_llm_metrics.append(ev.metrics)
        elif isinstance(ev.metrics, metrics.TTSMetrics):
            latest_tts_metrics = ev.metrics
            all_tts_metrics.append(ev.metrics)

            # Calculate total conversation latency when we have all components
            if latest_eou_metrics and latest_llm_metrics and latest_tts_metrics:
                # Check if they're all from the same speech turn
                if (latest_eou_metrics.speech_id == latest_llm_metrics.speech_id ==
                        latest_tts_metrics.speech_id):
                    speech_id = latest_tts_metrics.speech_id

                    # Only log once per speech_id to avoid duplicates
                    if speech_id not in logged_speech_ids:
                        logged_speech_ids.add(speech_id)

                        total_latency = (latest_eou_metrics.end_of_utterance_delay +
                                         latest_llm_metrics.ttft +
                                         latest_tts_metrics.ttfb)

                        # Create detailed breakdown
                        breakdown = (
                            f"EOU: {latest_eou_metrics.end_of_utterance_delay:.3f}s, "
                            f"LLM TTFT: {latest_llm_metrics.ttft:.3f}s, "
                            f"TTS TTFB: {latest_tts_metrics.ttfb:.3f}s"
                        )

                        logger.info(
                            f"{state.room_metadata.roleplay_id}: User stopped speaking → AI started responding: {total_latency:.3f}s for speech_id {speech_id} ({breakdown})")

        # Collect usage metrics for cost tracking
        usage_collector.collect(ev.metrics)

    def log_conversation_averages():
        """Calculate and log average metrics for the entire conversation."""
        if not all_eou_metrics and not all_llm_metrics and not all_tts_metrics:
            logger.info(
                f"{state.room_metadata.roleplay_id}: No metrics collected during conversation")
            return

        # Calculate EOU averages
        eou_count = len(all_eou_metrics)
        if eou_count > 0:
            avg_eou_delay = sum(
                m.end_of_utterance_delay for m in all_eou_metrics) / eou_count
            logger.info(
                f"{state.room_metadata.roleplay_id}: Average EOU delay across {eou_count} utterances: {avg_eou_delay:.3f}s")

        # Calculate LLM averages
        llm_count = len(all_llm_metrics)
        if llm_count > 0:
            avg_ttft = sum(m.ttft for m in all_llm_metrics) / llm_count
            avg_completion_tokens = sum(
                m.completion_tokens for m in all_llm_metrics) / llm_count
            avg_prompt_tokens = sum(
                m.prompt_tokens for m in all_llm_metrics) / llm_count
            logger.info(
                f"{state.room_metadata.roleplay_id}: Average LLM TTFT across {llm_count} generations: {avg_ttft:.3f}s")
            logger.info(
                f"{state.room_metadata.roleplay_id}: Average LLM tokens - Completion: {avg_completion_tokens:.1f}, Prompt: {avg_prompt_tokens:.1f}")

        # Calculate TTS averages
        tts_count = len(all_tts_metrics)
        if tts_count > 0:
            avg_ttfb = sum(m.ttfb for m in all_tts_metrics) / tts_count
            avg_duration = sum(
                m.audio_duration for m in all_tts_metrics) / tts_count
            avg_characters = sum(
                m.characters_count for m in all_tts_metrics) / tts_count
            logger.info(
                f"{state.room_metadata.roleplay_id}: Average TTS TTFB across {tts_count} generations: {avg_ttfb:.3f}s")
            logger.info(
                f"{state.room_metadata.roleplay_id}: Average TTS duration: {avg_duration:.3f}s, characters: {avg_characters:.1f}")

        # Calculate overall conversation latency averages
        if eou_count > 0 and llm_count > 0 and tts_count > 0:
            # Only include metrics from complete speech turns (where all three components exist)
            complete_turns = []
            for eou in all_eou_metrics:
                matching_llm = next(
                    (llm for llm in all_llm_metrics if llm.speech_id == eou.speech_id), None)
                matching_tts = next(
                    (tts for tts in all_tts_metrics if tts.speech_id == eou.speech_id), None)
                if matching_llm and matching_tts:
                    total_latency = eou.end_of_utterance_delay + \
                        matching_llm.ttft + matching_tts.ttfb
                    complete_turns.append(total_latency)

            if complete_turns:
                avg_total_latency = sum(complete_turns) / len(complete_turns)
                logger.info(
                    f"{state.room_metadata.roleplay_id}: Average total conversation latency across {len(complete_turns)} complete turns: {avg_total_latency:.3f}s")
p
Thanks I have also written a wrapper to get these calculated. From the SS it appeared you were tracking this via Langfuse or some other OTEL integration
m
I've just got logs that are going into Sentry using this:
Copy code
sentry_sdk.init(
    dsn="XXXXXXXXXXXXXXXXXXXXXXX",
    traces_sample_rate=1.0,
    release=os.getenv("NEXT_PUBLIC_GIT_SHA", "development"),
    _experiments={"enable_logs": True},
)
I know Livekit recently came out with enhanced OTEL, but I haven't looked into it yet
👍 1