LiveKit Agents Integration via OpenTelemetry

LiveKit Agents is an open-source Python framework for building production-grade multimodal and voice AI agents. It provides a complete set of tools and abstractions for feeding realtime media through AI pipelines, supporting both high-performance STT-LLM-TTS voice pipelines and speech-to-speech models.

LiveKit Agents’ primary advantage is its built-in OpenTelemetry support for comprehensive observability, making it easy to monitor agent sessions, LLM calls, function tools, and TTS operations in real-time applications.

LiveKit Agents tracing

Getting started

To use the LiveKit Agents integration with Opik, you will need to have LiveKit Agents and the required OpenTelemetry packages installed:

$pip install "livekit-agents[openai,turn-detector,silero,deepgram]" opentelemetry-exporter-otlp-proto-http

Environment configuration

Configure your environment variables based on your Opik deployment:

If you are using Opik Cloud, you will need to set the following environment variables:

$export OTEL_EXPORTER_OTLP_ENDPOINT=https://www.comet.com/opik/api/v1/private/otel
>export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default'

To log the traces to a specific project, you can add the projectName parameter to the OTEL_EXPORTER_OTLP_HEADERS environment variable:

$export OTEL_EXPORTER_OTLP_HEADERS='Authorization=<your-api-key>,Comet-Workspace=default,projectName=<your-project-name>'

You can also update the Comet-Workspace parameter to a different value if you would like to log the data to a different workspace.

Using Opik with LiveKit Agents

LiveKit Agents includes built-in OpenTelemetry support. To enable telemetry, configure a tracer provider using set_tracer_provider in your entrypoint function:

1import logging
2
3from livekit.agents import (
4 Agent,
5 AgentSession,
6 JobContext,
7 RunContext,
8 WorkerOptions,
9 cli,
10 metrics,
11)
12from livekit.agents.llm import function_tool
13from livekit.agents.telemetry import set_tracer_provider
14from livekit.agents.voice import MetricsCollectedEvent
15from livekit.plugins import deepgram, openai, silero
16from livekit.plugins.turn_detector.multilingual import MultilingualModel
17from opentelemetry.util.types import AttributeValue
18
19logger = logging.getLogger("basic-agent")
20
21
22def setup_opik_tracing(metadata: dict[str, AttributeValue] | None = None):
23 """Set up Opik tracing for LiveKit Agents"""
24 from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
25 from opentelemetry.sdk.trace import TracerProvider
26 from opentelemetry.sdk.trace.export import BatchSpanProcessor
27
28 # Set up the tracer provider
29 trace_provider = TracerProvider()
30 trace_provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
31 set_tracer_provider(trace_provider, metadata=metadata)
32
33 return trace_provider
34
35
36@function_tool
37async def lookup_weather(context: RunContext, location: str) -> str:
38 """Called when the user asks for weather related information.
39
40 Args:
41 location: The location they are asking for
42 """
43
44 logger.info(f"Looking up weather for {location}")
45
46 return "sunny with a temperature of 70 degrees."
47
48
49class Kelly(Agent):
50 def __init__(self) -> None:
51 super().__init__(
52 instructions="Your name is Kelly.",
53 llm=openai.LLM(model="gpt-4o-mini"),
54 stt=deepgram.STT(model="nova-3", language="multi"),
55 tts=openai.TTS(voice="ash"),
56 turn_detection=MultilingualModel(),
57 tools=[lookup_weather],
58 )
59
60 async def on_enter(self):
61 logger.info("Kelly is entering the session")
62 self.session.generate_reply()
63
64 @function_tool
65 async def transfer_to_alloy(self) -> Agent:
66 """Transfer the call to Alloy."""
67 logger.info("Transferring the call to Alloy")
68 return Alloy()
69
70
71class Alloy(Agent):
72 def __init__(self) -> None:
73 super().__init__(
74 instructions="Your name is Alloy.",
75 llm=openai.realtime.RealtimeModel(voice="alloy"),
76 tools=[lookup_weather],
77 )
78
79 async def on_enter(self):
80 logger.info("Alloy is entering the session")
81 self.session.generate_reply()
82
83 @function_tool
84 async def transfer_to_kelly(self) -> Agent:
85 """Transfer the call to Kelly."""
86
87 logger.info("Transferring the call to Kelly")
88 return Kelly()
89
90
91async def entrypoint(ctx: JobContext):
92 # set up the langfuse tracer
93 trace_provider = setup_opik_tracing(
94 # metadata will be set as attributes on all spans created by the tracer
95 metadata={
96 "livekit.session.id": ctx.room.name,
97 }
98 )
99
100 # (optional) add a shutdown callback to flush the trace before process exit
101 async def flush_trace():
102 trace_provider.force_flush()
103
104 ctx.add_shutdown_callback(flush_trace)
105
106 session = AgentSession(vad=silero.VAD.load())
107
108 @session.on("metrics_collected")
109 def _on_metrics_collected(ev: MetricsCollectedEvent):
110 metrics.log_metrics(ev.metrics)
111
112 await session.start(agent=Kelly(), room=ctx.room)
113
114
115if __name__ == "__main__":
116 cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))

What gets traced

With this setup, your LiveKit agent will automatically trace:

  • Session events: Session start and end with metadata
  • Agent turns: Complete conversation turns with timing
  • LLM operations: Model calls, prompts, responses, and token usage
  • Function tools: Tool executions with inputs and outputs
  • TTS operations: Text-to-speech conversions with audio metadata
  • STT operations: Speech-to-text transcriptions
  • End-of-turn detection: Conversation flow events

Further improvements

If you have any questions or suggestions for improving the LiveKit Agents integration, please open an issue on our GitHub repository.