important-rainbow-47551
11/03/2025, 7:39 PMtall-airline-16756
11/03/2025, 7:39 PMtall-airline-16756
11/03/2025, 7:41 PMproject/
├── main.py # Entry point with entrypoint function
├── agents/
│ ├── __init__.py # Agent imports and exports
│ ├── intro_agent.py # IntroAgent class
│ ├── story_agent.py # StoryAgent class
│ └── specialist_agent.py # SpecialistEditorAgent class
├── shared/
│ └── data_models.py # Shared data classes
└── requirements.txt
2. Define Shared Data Models:
It’s helpful to keep shared data structures in a separate module for easy access and maintenance. For example:
# shared/data_models.py
from dataclasses import dataclass, field
from typing import Optional
@dataclass
class StoryData:
characters: list = field(default_factory=list)
locations: list = field(default_factory=list)
theme: Optional[str] = None
3. Implement Agents in Separate Files:
Each agent gets its own module, which keeps things tidy and manageable. Here's how you might set up an IntroAgent:
# agents/intro_agent.py
from livekit.agents import Agent, RunContext
from livekit.agents.llm import function_tool
from shared.data_models import StoryData
from .story_agent import StoryAgent
class IntroAgent(Agent):
def __init__(self):
super().__init__(instructions="Your goal is to gather information...")
@function_tool
async def information_gathered(self, context: RunContext[StoryData], name: str, location: str):
context.userdata.name = name
context.userdata.location = location
return StoryAgent(name, location, chat_ctx=context.session._chat_ctx), "Let's start the story!"
4. Set Up the Main Entry Point:
This is where you coordinate your agents and sessions. Here’s an example of how your main.py might look:
# main.py
from livekit.agents import AgentSession, JobContext, cli, WorkerOptions
from agents.intro_agent import IntroAgent
from shared.data_models import StoryData
async def entrypoint(ctx: JobContext):
await ctx.connect()
session = AgentSession[StoryData](
vad=ctx.proc.userdata["vad"],
llm=openai.LLM(model="gpt-4o-mini"),
stt=deepgram.STT(model="nova-3"),
tts=openai.TTS(voice="ash"),
userdata=StoryData(),
)
await session.start(agent=IntroAgent(), room=ctx.room)
if __name__ == "__main__":
cli.run_app(WorkerOptions(entrypoint_fnc=entrypoint))
5. Preserve Context During Handoff:
To keep the conversation flowing naturally, make sure to pass the current chat context when handing off to a new agent. For example:
childrens_editor = SpecialistEditorAgent("children's books", chat_ctx=context.session._chat_ctx)
This little step helps ensure your agents transition seamlessly.
If anything feels unclear or if you’d like me to walk you through any part in more detail, just let me know—I’m here to help!
Sources: Workflows | LiveKit Docs | README.md | complex-agents/role-playing/agents/__init__.py | README.md | Building Multi-Agent Architectures with LiveKit Agents