Overview
LiveKit Agents instruments each session with OpenTelemetry traces: the same spans that power Agent insights in LiveKit Cloud. Set a tracer provider to export these spans to any OpenTelemetry-compatible backend.
The following example sends spans to Langfuse , an open-source LLM observability platform. The same approach works for any backend that accepts traces over the OpenTelemetry Protocol (OTLP). To learn more, see Other backends.
Set environment variables
Create an API key pair in your Langfuse project settings, then add the following to your agent's .env.local file:
LANGFUSE_PUBLIC_KEY: The public key for your Langfuse project.LANGFUSE_SECRET_KEY: The secret key for your Langfuse project.LANGFUSE_BASE_URL: The URL for your Langfuse instance, such ashttps://cloud.langfuse.com(EU) orhttps://us.cloud.langfuse.com(United States).
The example script reads these variables to build the OTLP endpoint and authentication headers that the exporter sends to Langfuse. To export to a different backend, set those values directly instead. See Other backends.
Trace a complete agent
Both examples send the x-langfuse-ingestion-version header to opt into Langfuse's realtime ingestion. Without it, spans can take up to 10 minutes to appear.
Call setup_langfuse before the session starts so the agent's spans route to Langfuse. Pass metadata to set attributes on every span. For example, set langfuse.session.id to the room name to group all of a session's spans together in Langfuse:
import base64import osfrom dotenv import load_dotenvfrom opentelemetry.sdk.trace import TracerProviderfrom opentelemetry.util.types import AttributeValuefrom livekit.agents import (Agent,AgentServer,AgentSession,JobContext,cli,inference,)from livekit.agents.telemetry import set_tracer_providerload_dotenv(".env.local")def setup_langfuse(metadata: dict[str, AttributeValue] | None = None) -> TracerProvider:from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporterfrom opentelemetry.sdk.trace.export import BatchSpanProcessorpublic_key = os.environ.get("LANGFUSE_PUBLIC_KEY")secret_key = os.environ.get("LANGFUSE_SECRET_KEY")base_url = os.environ.get("LANGFUSE_BASE_URL")if not public_key or not secret_key or not base_url:raise ValueError("LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_BASE_URL must be set")langfuse_auth = base64.b64encode(f"{public_key}:{secret_key}".encode()).decode()os.environ["OTEL_EXPORTER_OTLP_ENDPOINT"] = f"{base_url.rstrip('/')}/api/public/otel"os.environ["OTEL_EXPORTER_OTLP_HEADERS"] = (f"Authorization=Basic {langfuse_auth},x-langfuse-ingestion-version=4")trace_provider = TracerProvider()trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))set_tracer_provider(trace_provider, metadata=metadata)return trace_providerclass Assistant(Agent):def __init__(self) -> None:super().__init__(instructions="You are a helpful voice AI assistant.",llm=inference.LLM(model="openai/gpt-5.2-chat-latest"),)server = AgentServer()@server.rtc_session(agent_name="my-agent")async def entrypoint(ctx: JobContext):# Route spans to Langfuse before the session starts.trace_provider = setup_langfuse(metadata={"langfuse.session.id": ctx.room.name})# Flush any remaining spans before the process exits.async def flush_trace():trace_provider.force_flush()ctx.add_shutdown_callback(flush_trace)session = AgentSession(stt=inference.STT(model="deepgram/nova-3", language="multi"),tts=inference.TTS(model="inworld/inworld-tts-2"),preemptive_generation=True,)await session.start(agent=Assistant(), room=ctx.room)await ctx.connect()if __name__ == "__main__":cli.run_app(server)
For a larger example with fallback models and metrics logging, see the OpenTelemetry trace example on GitHub .
Install the OpenTelemetry SDK and an OTLP trace exporter alongside @livekit/agents:
pnpm add @opentelemetry/api @opentelemetry/sdk-trace-node @opentelemetry/exporter-trace-otlp-http
Call setupLangfuse before the session starts so the agent's spans route to Langfuse. Pass metadata to set attributes on every span. For example, set langfuse.session.id to the room name to group all of a session's spans together in Langfuse:
import {type JobContext,ServerOptions,cli,defineAgent,inference,telemetry,voice,} from '@livekit/agents';import { type Attributes } from '@opentelemetry/api';import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';import { BatchSpanProcessor, NodeTracerProvider } from '@opentelemetry/sdk-trace-node';import dotenv from 'dotenv';import { fileURLToPath } from 'node:url';dotenv.config({ path: '.env.local' });function setupLangfuse(metadata?: Attributes): NodeTracerProvider {const publicKey = process.env.LANGFUSE_PUBLIC_KEY;const secretKey = process.env.LANGFUSE_SECRET_KEY;const baseUrl = process.env.LANGFUSE_BASE_URL;if (!publicKey || !secretKey || !baseUrl) {throw new Error('LANGFUSE_PUBLIC_KEY, LANGFUSE_SECRET_KEY, and LANGFUSE_BASE_URL must be set');}const auth = Buffer.from(`${publicKey}:${secretKey}`).toString('base64');const traceExporter = new OTLPTraceExporter({url: `${baseUrl.replace(/\/$/, '')}/api/public/otel/v1/traces`,headers: { Authorization: `Basic ${auth}`, 'x-langfuse-ingestion-version': '4' },});// A provider takes its span processors at construction. Include a FanoutSpanProcessor and// hand its `add` method to setTracerProvider so the framework can attach the processor that// applies `metadata` to every span.const fanout = new telemetry.FanoutSpanProcessor();const traceProvider = new NodeTracerProvider({spanProcessors: [new BatchSpanProcessor(traceExporter), fanout],});traceProvider.register();telemetry.setTracerProvider(traceProvider, {metadata,registerSpanProcessor: (processor) => fanout.add(processor),});return traceProvider;}export default defineAgent({entry: async (ctx: JobContext) => {// Route spans to Langfuse before the session starts.const traceProvider = setupLangfuse({ 'langfuse.session.id': ctx.room.name });// Flush any remaining spans before the process exits.ctx.addShutdownCallback(async () => {await traceProvider.shutdown();});const session = new voice.AgentSession({stt: new inference.STT({ model: 'deepgram/nova-3', language: 'multi' }),llm: new inference.LLM({ model: 'openai/gpt-5.2-chat-latest' }),tts: new inference.TTS({model: 'inworld/inworld-tts-2',voice: 'Ashley',}),});await session.start({agent: voice.Agent.create({ instructions: 'You are a helpful voice AI assistant.' }),room: ctx.room,});await ctx.connect();},});cli.runApp(new ServerOptions({ agent: fileURLToPath(import.meta.url), agentName: 'my-agent' }));
registerSpanProcessor also keeps Agent insights in LiveKit Cloud working: with LiveKit Cloud tracing enabled, the framework registers its own exporter on your provider, so spans reach both Langfuse and LiveKit Cloud. Without it, the framework turns off Cloud tracing and logs a warning.
For a larger example with fallback models and metrics logging, see the OpenTelemetry trace example on GitHub .
Other backends
The preceding pattern works for any backend that accepts OpenTelemetry traces over OTLP. To export elsewhere, point the exporter at the OTLP endpoint for that backend and set the authentication it requires:
OTEL_EXPORTER_OTLP_ENDPOINT: The OTLP HTTP endpoint for the backend.OTEL_EXPORTER_OTLP_HEADERS: Any authentication headers the backend requires, such as an API key.
The rest of the agent stays the same: build a tracer provider, add a batch span processor with an OTLP exporter, and pass the provider to set_tracer_provider (Python) or telemetry.setTracerProvider (Node.js) before the session starts.
In Node.js you can also pass url and headers to the exporter instead of setting environment variables, as the preceding example does. The url option is used as-is, while OTEL_EXPORTER_OTLP_ENDPOINT has /v1/traces appended to it.