When to use self.session.userdata vs context.userd...
# ask-ai
g
When to use self.session.userdata vs context.userdata
t
Thanks for reaching out, Aumesh. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
self.session.userdata
and
context.userdata
both point to the same user data object, but choosing which to use really depends on where you are in your code: • Use
self.session.userdata
when you're inside an agent's methods, like
on_enter()
. This is a great way to access or update user data directly, especially within class methods of agents. • Use
context.userdata
when you're working inside function tools that receive a
RunContext
parameter. This lets your tools independently read and update the user's state smoothly. This little distinction helps keep user data management clear and effective, depending on whether you're dealing with the agent's internal state or the tool's execution context. I hope this makes things a bit clearer for you! Sources: agents/src/voice/run_context.ts | examples/drive-thru/drivethru_agent.py | flows/declarative_flow.py | Building Multi-Agent Architectures with LiveKit Agents