i am initializing agent with some custom data i am...
# ask-ai
k
i am initializing agent with some custom data i am putting in init method
Copy code
python
class AssistantAgent(Agent):
    """
    AI Assistant Agent that uses assistant-specific configuration.
    Supports dynamic TTS providers, custom prompts, and advanced call handling.
    """

    async def __init__(
        self,
        *,
        assistant_config: dict):
             self.assistant: Dict[str, Any] = assistant_config
             .......
@function_tool() async def transfer_call(context: RunContext, reason: str = "Customer requested transfer") -> str: ......... @function_tool() async def end_call_on_trigger(context: RunContext, reason: str = "", msg: str = "") ........... how can i access assistant in function tools?
t
Thanks for reaching out, Saurav. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! To grab the
assistant
instance variable inside your function tools, you can simply use the
context.userdata
property that comes with
RunContext
. Here’s a quick example to make it clearer:
Copy code
from livekit.agents import Agent, RunContext, function_tool

class AssistantAgent(Agent):
    def __init__(self, *, assistant_config: dict):
        super().__init__()
        self.userdata = {"assistant": assistant_config}

    @function_tool()
    async def transfer_call(self, context: RunContext, reason: str = "Customer requested transfer") -> str:
        assistant_data = context.userdata.get("assistant")
        return f"Transferring call using assistant: {assistant_data}"
So, what’s happening here? • The
assistant
variable gets stored in
self.userdata
when the class is initialized. • Then, inside your function tool, you can easily access it with
context.userdata.get("assistant")
. This way, your function tools can flexibly tap into the
assistant
configuration whenever they need to. Hope that helps!
k
so i have to pass self.userdata = {"assistant": assistant_config} before super()._init_() or after it?
t
Hey! It’s best to assign
self.userdata = {"assistant": assistant_config}
after you call
super().__init__()
. This way, the parent class finishes setting up before you tweak or add your own instance data like
userdata
. Here’s how you can do it:
Copy code
class AssistantAgent(Agent):
    def __init__(self, *, assistant_config: dict):
        super().__init__()  # Initialize the parent class
        self.userdata = {"assistant": assistant_config}  # Set userdata after
If anything’s unclear or you want to dive deeper, just give me a shout!