Skip to main content
This tutorial will guide you through building a sophisticated multi-agent personal shopping assistant using LiveKit for real-time voice interactions and Maxim for observability. The system features three specialized agents—Triage, Sales, and Returns—that seamlessly hand off conversations while maintaining customer context and order history.

Prerequisites

  • Python 3.9+
  • LiveKit server credentials (URL, API key, secret)
  • OpenAI API key (for LLM and TTS)
  • Deepgram API key (for STT)
  • Maxim account (API key, log repo ID)

Project Setup

Configure your environment variables in .env:

Install Dependencies

Add Dependencies to requirements.txt

Set Up a Virtual Environment

Create a Project Directory and Navigate into It


Architecture Overview

The Personal Shopper system uses a multi-agent architecture with three specialized agents: Each agent can transfer conversations to other agents while preserving the full conversation context and customer identification.

Code Walkthrough: Key Components

Below, each section of the code is presented with a technical explanation.

1. Imports and Initialization

  • Imports all required libraries for real-time audio, multi-agent orchestration, and observability.
  • Loads environment variables and configures logging for debugging and traceability.
  • Initializes the Maxim logger.

2. Maxim Instrumentation

The instrument_livekit function integrates Maxim with LiveKit, automatically capturing all agent interactions, transfers, and tool calls for comprehensive observability.

3. UserData Class for Session State

  • Maintains session state across agent transfers including customer identity and current order.
  • The is_identified() method enables personalized interactions once the customer provides their name.
  • The summarize() method provides context to each agent about the current customer.

4. BaseAgent Class with Context Preservation

  • The on_enter() method is called when an agent takes control of the conversation.
  • Automatically injects customer context and conversation history from the previous agent.
  • The _transfer_to_agent() method enables seamless handoffs between specialized agents.

5. Triage Agent

  • Acts as the entry point for all customer interactions.
  • Identifies customers and routes them to Sales or Returns based on their needs.
  • Uses personalized messages when the customer has been identified.

6. Sales Agent

  • Handles the complete order lifecycle: start, add items, and complete.
  • Calculates order totals and persists orders to the customer database.
  • Validates that customers are identified before processing orders.

7. Returns Agent

  • Retrieves customer order history for context-aware return processing.
  • Processes returns with order ID, item name, and reason tracking.
  • Can transfer back to Triage or Sales as needed.

8. Entrypoint: Starting the Multi-Agent Session

  • Initializes all three agents and registers them for cross-agent transfers.
  • Starts the session with the Triage agent as the entry point.
  • The AgentSession manages state across the entire conversation.

Supporting Files

database.py - Customer Database

utils.py - Prompt Loader

Prompt Files

Create three YAML files for agent prompts: triage_prompt.yaml
sales_prompt.yaml
returns_prompt.yaml

How to Use

  1. Start the Agent: Run the script to launch the multi-agent system.
  2. Connect via LiveKit: Join the room using a LiveKit client or the console.
  3. Interact with Triage: The Triage agent will greet you and ask for your name.
  4. Get Routed: Based on your needs, you’ll be transferred to Sales or Returns.
  5. Complete Your Task: Make purchases or process returns with the specialized agents.
  6. Monitor in Maxim: All agent interactions, transfers, and tool calls are logged.

Run the Script

Observability with Maxim

Every agent interaction, transfer, and tool call is automatically logged in your Maxim dashboard:
  • Agent Transfers: See when and why customers are transferred between agents
  • Customer Identification: Track customer lookups and account creation
  • Order Processing: Monitor order creation, item additions, and completions
  • Return Processing: Audit return requests and processing
Use Maxim to debug conversation flows, analyze agent performance, and improve your multi-agent system.

Troubleshooting

  • Agent not responding
    • Check your OpenAI API key is set correctly
    • Verify Deepgram API key for speech-to-text
  • Transfers not working
    • Ensure all agents are registered in userdata.personas
    • Check that agent names match exactly (“triage”, “sales”, “returns”)
  • No Maxim traces
    • Ensure your MAXIM_API_KEY is set in .env
    • Verify instrument_livekit(maxim_logger) is called before agent creation
  • Order not saving
    • Check that database.py has write permissions for customers.json

Complete Code: personal_shopper.py

What gets logged to Maxim

  • Agent Transfers: See when and why customers are transferred between agents
  • Customer Identification: Track customer lookups and account creation
  • Order Processing: Monitor order creation, item additions, and completions
  • Return Processing: Audit return requests and processing

Resources

Cookbook Code

Python code for LiveKit Personal Shopper with Maxim

Livekit Cookbooks

LiveKit Python Agents Examples