How can I be able to handle state from inside the ...
# ask-ai
e
How can I be able to handle state from inside the agent constructor?
Copy code
class 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."
        )
t
Thanks for reaching out, Brian. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
It sounds like you want to access per-session state (like
session.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 Agents
e
Oh so I can't do it from the init it has to be the on_enter
t
Yes—access it in
on_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
.
e
Is it possible to call a function tool from another function tool. And if so how do we pass the context available in all function tools?
t
It sounds like you want one tool to invoke another and share the same context. Yes, you can do that. • Tools are regular functions, so you can call one tool from another directly. • Include the special
context
(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