steep-balloon-41261
08/28/2025, 8:27 AMtall-airline-16756
08/28/2025, 8:27 AMtall-airline-16756
08/28/2025, 8:28 AMmetadata field when you dispatch the agent. Then, the agent can grab this info right in its entrypoint function using ctx.job.metadata. For example, things like the user’s name, age, or preferences in JSON format work great here.
2. Custom Instructions: Take that metadata and use it to tailor the agent’s instructions or preload context with ChatContext. This way, the agent feels personalized and ready to go from the get-go.
3. Participant Attributes: You can also set attributes on the client side that the agent reads once it joins the room—perfect for handling dynamic preferences.
4. Session Userdata: Lastly, session.userdata is handy for storing structured user or session info that sticks around across interactions.
If you’d like, I’m happy to walk you through detailed examples or help you implement any of these methods. Just let me know!
Sources: Agent dispatch | LiveKit Docs | External data and RAG | LiveKit Docs | flows/simple_flow.py | Workflows | LiveKit Docs | examples/voice_agents/multi_agent.pytall-airline-16756
08/28/2025, 8:31 AMsession.userdata in your agent, here’s a simple step-by-step guide:
1. Define a Data Class: Start by creating a dataclass to organize your session data. Something like this works great:
from dataclasses import dataclass
@dataclass
class MySessionInfo:
user_name: str | None = None
age: int | None = None
1. Initialize the AgentSession: When you create the session, pass in an instance of your data class:
session = AgentSession[MySessionInfo](userdata=MySessionInfo())
1. Access in the Agent: Inside your agent, just use self.session.userdata to grab the data. For example:
async def on_enter(self):
userdata: MySessionInfo = self.session.userdata
await self.session.generate_reply(
instructions=f"Greet {userdata.user_name} personally and offer your assistance."
)
1. Access in Function Tools: In your tools, you can use context.userdata when context: RunContext[YourClass]. Like this:
@function_tool()
async def record_name(self, context: RunContext[MySessionInfo], name: str):
context.userdata.user_name = name
return "Name recorded."
Just a quick tip: make sure userdata is initialized before you access it to avoid any runtime hiccups. If you want me to walk you through anything else or need more examples, just give me a shout!
Sources: complex-agents/ivr-agent/agent.py | Workflows | LiveKit Docs | examples/drive-thru/drivethru_agent.pytall-airline-16756
08/28/2025, 8:32 AMsession.userdata in a LiveKit agent using TypeScript:
1. Define a Type for Userdata: Start by creating a TypeScript interface to shape your session data:
interface MySessionInfo {
userName?: string;
age?: number;
}
1. Initialize the Session with Userdata: When you create the session, pass in the userdata object like this: Sources: Workflows | LiveKit Docs | Company directory phone assistant | LiveKit Docs | Building an Automated IVR Menu Caller | LiveKit Docstall-airline-16756
08/28/2025, 8:40 AM