Skip to main content

Introduction

LiveKit is a powerful platform for building real-time video, audio, and data applications. With Maxim’s integration, you can monitor and trace your LiveKit voice agents, capturing detailed insights into conversation flows, function calls, and performance metrics in real-time. This integration allows you to:
  • Monitor voice agent conversations in real-time
  • Trace function tool calls and their performance
  • Debug and optimize your voice AI applications

Requirements

Environment Variables

Set up the following environment variables in your .env file:

Getting Started

Step 1: Obtain API Keys

Maxim API Key

  1. Sign up at Maxim Console
  2. Create a new project or select an existing one
  3. Navigate to API Keys section
  4. Generate a new API key and copy your MAXIM_API_KEY
  5. Go to Logs, create a new repository and copyMAXIM_LOG_REPO_ID

LiveKit Credentials

  1. Set up your LiveKit server or use LiveKit Cloud, and create a new project.
  2. Get your server URL, API key, and API secret from the LiveKit dashboard
  3. Configure the credentials in your environment variables

OpenAI API Key

  1. Go to OpenAI Platform & create an API Key OpenAI Platform
  2. Set the OPENAI_API_KEY environment variable

Step 2: Initialize Maxim Logger

instrument_livekit: This function integrates Maxim’s observability features with LiveKit Agents . It allows you to automatically capture and send trace data to the platform: logger = Maxim().logger() : This creates a Maxim logger instance that:
  • Connects to your Maxim project using the MAXIM_API_KEY and MAXIM_LOG_REPO_ID environment variables
  • Handles sending trace data to the Maxim platform
  • Provides the logging interface for capturing events, metrics, and traces
on_event : This is a callback function that gets triggered during trace lifecycle events:
  • event: A string indicating what happened ("maxim.trace.started" or "maxim.trace.ended")
  • data: A dictionary containing trace information:
    • trace_id: Unique identifier for the trace
    • trace: The actual trace object with metadata, timing, and other details
What it does:
  • When a new conversation or interaction starts → logs “Trace started”
  • When a conversation or interaction ends → logs “Trace ended”
  • Useful for debugging and monitoring trace lifecycle in real-time

Step 3: Create Your Voice Agent

This code - Creates a custom agent class:
  • Inherits from Agent: Base class for all LiveKit agents
  • instructions: System prompt that defines the agent’s personality and capabilities
  • Tells the agent: It’s a voice assistant that can use web search
Creates a callable tool:
  • @function_tool(): Decorator that registers this method as a tool the agent can call
  • async def: Asynchronous function (required for LiveKit agents)
  • Type hints: query: str -> str helps the AI understand input/output types
  • Docstring: Describes the function for the AI model
  • Current implementation: Placeholder that returns a formatted string
The main function that runs when the agent starts:
  • ctx: agents.JobContext: Contains information about the current job/session
Creates a unique room name:
  • First tries: Environment variable LIVEKIT_ROOM_NAME
  • Falls back to: Generated name like assistant-room-a1b2c3d4e5f6...
  • uuid.uuid4().hex: Creates a random hexadecimal string
Starts the agent and begins conversation:
  • session.start(): Connects the agent to the room
  • agent=Assistant(): Uses your custom Assistant class
  • ctx.connect(): Connects to the LiveKit infrastructure
  • generate_reply(): Makes the agent speak first with a greeting
Here’s a complete example that includes web search functionality using Tavily:

What Gets Traced

Agent Conversations

Transcript containing System Instructions, User and Assistant Messages are pushed to Maxim

Running Your Agent

  1. Working code is uploaded here - https://github.com/maximhq/maxim-cookbooks/blob/main/python/observability-online-eval/livekit/livekit-gemini.py
  2. Start your LiveKit server (if self-hosting) or ensure your LiveKit Cloud instance is running
  3. Run your agent:

Troubleshooting

Common Issues

Agent not connecting to LiveKit:
  • Verify your LiveKit credentials are correct
  • Check that your LiveKit server is accessible
Traces not appearing in Maxim:
  • Confirm your MAXIM_API_KEY and MAXIM_LOG_REPO_ID are set correctly
  • Check the Maxim console for any API errors

Resources

LiveKit Docs

Official LiveKit documentation