creamy-tiger-14805
10/14/2025, 8:06 AMtall-airline-16756
10/14/2025, 8:06 AMtall-airline-16756
10/14/2025, 8:07 AMon_enter or on_exit, you can simply use instance attributes to access variables defined in the agent class. It’s pretty straightforward!
Now, when it comes to function tools (those defined with @function_tool), things work a bit differently. To keep everything running smoothly, you’ll want to pass variables or state through the RunContext parameter, specifically using context.userdata. This is the best practice for managing shared state across your session.
Here’s a quick example to illustrate:
@function_tool
async def add_task(self, task: str, context: RunContext):
context.userdata.tasks.append(task)
return f"Added task: {task}"
Doing it this way helps keep your shared session data consistent and easy to access. If you have any questions or want me to walk you through it further, just let me know—I’m here to help!
Sources: Tool definition and use | LiveKit Docs | context-readmes/USERDATA_GUIDE.md | examples/voice_agents/basic_agent.py | Building Multi-Agent Architectures with LiveKit Agentscreamy-tiger-14805
10/14/2025, 8:09 AMelse:
logger.info(f"SWITCHING TO ENGLISH AGENT")
return LivspaceInboundEnglishAgent(chat_ctx=self.chat_ctx, user_project_details=self.user_project_details)
what's the best way to pass user_project_details in this case?tall-airline-16756
10/14/2025, 8:11 AMuser_project_details when switching agents! A really effective way to handle this is by using a shared state object, like UserData, which stays with you across agent transitions. This helps keep all your important context intact without any hassle.
Here’s a friendly example to show you how you might set this up:
@dataclass
class UserData:
user_project_details: Optional[dict] = None
agents: dict[str, Agent] = field(default_factory=dict)
prev_agent: Optional[Agent] = None
When it’s time to switch agents, you simply update this shared state and pass it along like this:
class BaseAgent(Agent):
async def _transfer_to_agent(self, name: str, context: RunContext[UserData]):
userdata = context.userdata
current_agent = context.session.current_agent
next_agent = userdata.agents[name]
userdata.prev_agent = current_agent
return next_agent, f"Transferring to {name}."
This way, your user_project_details and any other important info stay accessible to the new agent smoothly. If you’d like me to walk you through it more or provide extra examples, just let me know—I’m here to help!
Sources: context-readmes/USERDATA_GUIDE.md | Agent dispatch | LiveKit Docs | examples/src/restaurant_agent.ts | Building Multi-Agent Architectures with LiveKit Agents | examples/voice_agents/multi_agent.pycreamy-tiger-14805
10/14/2025, 8:39 AMtall-airline-16756
10/14/2025, 8:40 AMUserData for passing user project details, here’s a simple guide to help you along the way:
1. Define the UserData Class: Start by creating a structured class to hold your project details and any other session-level data you need. For example:
from dataclasses import dataclass, field
from livekit.agents import JobContext
@dataclass
class UserData:
ctx: JobContext
user_name: str = ""
project_details: dict = field(default_factory=dict)
interaction_count: int = 0
1. Initialize and Pass UserData: In your entrypoint function, create an instance of UserData and pass it to the AgentSession. Here’s how you can do it:
async def entrypoint(ctx: JobContext):
await ctx.connect()
userdata = UserData(ctx=ctx, user_name="Alice", project_details={"project_id": "123", "role": "admin"})
session = AgentSession[UserData](
userdata=userdata,
llm=openai.LLM(),
stt=deepgram.STT(),
tts=elevenlabs.TTS(),
vad=silero.VAD.load()
)
await session.start(room=ctx.room, agent=MyAgent())
1. Access UserData: Whenever you need to access this data, you can use self.session.userdata inside your agents or context.userdata within function tools. For example:
# In Agent
class MyAgent(Agent):
async def on_enter(self):
userdata = self.session.userdata
await self.session.say(f"Welcome {userdata.user_name}")
# In Function Tool
@function_tool
async def get_project_info(self, context: RunContext[UserData]):
return f"Project: {context.userdata.project_details}"
This method helps keep your project details and session data handy throughout the agent’s lifecycle. If you have any questions or want me to walk you through any part, just let me know—I’m here to help!
Sources: examples/src/drive-thru/drivethru_agent.ts | Building Multi-Agent Architectures with LiveKit Agents | context-readmes/USERDATA_GUIDE.md