kind-iron-94532
08/10/2025, 4:59 PMpython
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?tall-airline-16756
08/10/2025, 4:59 PMtall-airline-16756
08/10/2025, 5:00 PMassistant
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:
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!kind-iron-94532
08/10/2025, 5:03 PMtall-airline-16756
08/10/2025, 5:03 PMself.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:
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!