early-window-29567
11/05/2025, 5:25 PMclass CustomerServiceAgent(Agent):
def __init__(self):
super().__init__(instructions="You are a friendly customer service representative.")
async def on_enter(self) -> None:
userdata: MySessionInfo = self.session.userdata
await self.session.generate_reply(
instructions=f"Greet {userdata.user_name} personally and offer your assistance."
)tall-airline-16756
11/05/2025, 5:25 PMtall-airline-16756
11/05/2025, 5:26 PMsession.userdata) from inside your agent. In LiveKit, session state is attached to the AgentSession, not the agent constructor, so access it in lifecycle hooks like on_enter, not __init__.
To do this:
• Put your custom state in session.userdata when creating the session (recommended: a dataclass).
• Read it in on_enter (as you’re doing) or inside tools via the run context.
Example pattern:
1. Define and pass userdata to the session.
2. In on_enter, read self.session.userdata and act.
For more on session-scoped userdata and chat context, see the docs:
• Userdata and workflows: Workflows | LiveKit Docs
• Stateless agents and multi-agent patterns: Building Multi-Agent Architectures with LiveKit Agents
Would you like a short example showing how to define a MySessionInfo dataclass and attach it to the session?
Sources: Agent Session & Client Integration | livekit/livekit_composite | DeepWiki | Workflows | LiveKit Docs | Agents v0.x migration guide - Python | LiveKit docs | Agents v0.x migration guide - Node.js | LiveKit Docs | Building Multi-Agent Architectures with LiveKit Agentsearly-window-29567
11/05/2025, 5:34 PMtall-airline-16756
11/05/2025, 5:35 PMon_enter (or later), not in __init__.
• In __init__, the agent isn’t attached to a session yet, so self.session (and session.userdata) isn’t available.
• In on_enter, the session is attached, so you can safely read self.session.userdata and act on it.
If helpful, I can share a minimal snippet showing how to pass MySessionInfo into the session and read it in on_enter.early-window-29567
11/05/2025, 5:50 PMtall-airline-16756
11/05/2025, 5:52 PMcontext (RunContext) parameter in each tool’s signature. When chaining, pass the same context through so both tools share session, userdata, etc. (e.g., return other_tool(context, args...)).
• For shared logic, you can also factor a plain helper function that takes context and call it from multiple tools.
References:
• Tool signatures and context details: Tool definition and use | LiveKit docs
• RunContext in tools (migration notes): Agents v0.x migration guide - Python
• Multi-agent/tool patterns: Building Multi-Agent Architectures with LiveKit Agents
Want a quick Python snippet showing tool_a calling tool_b and sharing context.userdata?
Sources: Tool definition and use | LiveKit docs | Agents v0.x migration guide - Python | LiveKit docs | Building Multi-Agent Architectures with LiveKit Agents