how to send time to first token and time for first...
# ask-ai
w
how to send time to first token and time for first byte to langfuse?
t
Thanks for reaching out, Sunil. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you want to send Time to First Token (TTFT) and Time to First Byte (TTFB) metrics to LangFuse, you can easily do that using LiveKit's OpenTelemetry integration. Let me walk you through the setup: 1. First, configure a tracer provider using the
set_tracer_provider
function. 2. Next, make sure to set these environment variables:
LANGFUSE_PUBLIC_KEY
,
LANGFUSE_SECRET_KEY
, and
LANGFUSE_HOST
. 3. Then, use the
setup_langfuse()
function in your entrypoint to kick off telemetry collection. This sets up the OpenTelemetry exporter to send data right to LangFuse. Here’s a quick example for you:
Copy code
import os
from livekit.agents.telemetry import set_tracer_provider

def setup_langfuse(host=None, public_key=None, secret_key=None):
    from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import BatchSpanProcessor

    public_key = public_key or os.getenv("LANGFUSE_PUBLIC_KEY")
    secret_key = secret_key or os.getenv("LANGFUSE_SECRET_KEY")
    host = host or os.getenv("LANGFUSE_HOST")

    if not public_key or not secret_key or not host:
        raise ValueError("LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_HOST must be set")

    # Configuration continues...
Just so you know, metrics like TTFT (captured during LLM inference) and TTFB (captured during TTS generation) are automatically picked up and sent over to LangFuse. If you want to see a full example, check out the LiveKit Agents GitHub repository. If you have any questions or need a hand with anything else, just give me a shout! Sources: Logs, metrics, and telemetry | LiveKit Docs | Capturing metrics | LiveKit Docs | livekit-agents/livekit/agents/metrics/base.py | livekit/livekit_metrics.pb.go | Agent Testing & Evaluation | livekit/livekit_composite | DeepWiki