can i access agent custom properties in @session.o...
# ask-ai
w
can i access agent custom properties in @session.on("function_tools_executed") def _on_function_tools_executed(ev: FunctionToolsExecutedEvent):
t
Thanks for reaching out, Test. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hi there! I wanted to share a helpful tip regarding the
@session.on("function_tools_executed")
event. It doesn’t directly give you access to agent custom properties, but no worries—you can use
session.userdata
to store and retrieve those custom properties while your tools are running, and then easily access them in the event handler. Here’s a simple example to guide you:
Copy code
from livekit.agents import Agent, RunContext, function_tool

class WeatherAgent(Agent):
    def __init__(self):
        super().__init__()
        self.session.userdata["last_action"] = None

    @function_tool()
    async def get_weather(self, context: RunContext, location: str):
        context.userdata["last_action"] = f"weather lookup for {location}"
        return {"weather": "sunny", "temp": 75}

@session.on("function_tools_executed")
def on_tools_executed(ev):
    last_action = session.userdata.get("last_action")
    print(f"Last agent action: {last_action}")
    if "weather" in last_action:
        ev.cancel_tool_reply()
Using this approach lets you smoothly coordinate your custom properties across different tools and event handlers. If you have any questions or want to explore this further, feel free to ask—I’m here to help! Sources: livekit-agents/livekit/agents/telemetry/trace_types.py | Tool definition and use | LiveKit Docs | Events and error handling | LiveKit Docs