I am trying to trace some errors in my application...
# agents
e
I am trying to trace some errors in my application with Deepgram support for which they are asking for the request ID, I am reading through the documentation but unable to find something like an emitted event for Speech which could be simply logged where I could have the request ID for the STT and TTS nodes. Can you share any examples or some direction on how to achieve this @refined-appointment-81829
r
I think you can do something like this in your agent:
Copy code
# inside entrypoint(), alongside your existing metrics handler
from livekit.agents import metrics as lk_metrics
from livekit.agents._exceptions import APIStatusError
from livekit.agents.stt.stt import STTError
from livekit.agents.tts.tts import TTSError

@session.on("metrics_collected")
def _on_metrics_collected(ev: MetricsCollectedEvent):
    if isinstance(ev.metrics, lk_metrics.STTMetrics):
        logger.info(
            "STT metrics",
            extra={"provider": ev.metrics.label, "request_id": ev.metrics.request_id},
        )
    elif isinstance(ev.metrics, lk_metrics.TTSMetrics):
        logger.info(
            "TTS metrics",
            extra={
                "provider": ev.metrics.label,
                "request_id": ev.metrics.request_id,  # may be local (not provider) for many TTS plugins
                "segment_id": ev.metrics.segment_id,
            },
        )
    lk_metrics.log_metrics(ev.metrics)  # keep your existing summary logs
    usage_collector.collect(ev.metrics)

# Optional: also capture provider request IDs on errors
@session.on("error")
def _on_error(ev):
    err = ev.error
    # STT/TTS API errors can carry provider request_id (if plugin sets it)
    if isinstance(err, (STTError, TTSError)) and isinstance(err.error, APIStatusError):
        logger.error(
            "Speech API error",
            extra={
                "source": err.type,
                "provider": err.label,
                "status_code": err.error.status_code,
                "request_id": err.error.request_id,  # provider-supplied if available
            },
            exc_info=err.error,
        )
e
Thanks @refined-appointment-81829