how to set multi agent handoff with multiple pytho...
# ask-ai
i
how to set multi agent handoff with multiple python file explain detaily
t
Thanks for reaching out, Selahattin. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're interested in setting up multi-agent handoff with multiple Python files in LiveKit! Let's walk through the steps together to make this as smooth as possible: 1. Organize Your Project: • Start by creating a clear directory structure to keep your agents and shared data nicely separated. Here's an example layout:
Copy code
project/
├── 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:
Copy code
# 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:
Copy code
# 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:
Copy code
# 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:
Copy code
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