how to access the agent's self attributes from a f...
# ask-ai
b
how to access the agent's self attributes from a function set in @llm.function_tool.
t
Thanks for reaching out, RikuKomiya. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm glad you're exploring how to access an agent's self attributes within a function defined with
@llm.function_tool
. You can simply use the
self
keyword inside the function to reference any attributes you set up in the agent's
__init__
method. Here's a little example to illustrate:
Copy code
class Assistant(Agent):
    def __init__(self):
        self.volume = 50
        super().__init__(instructions=f"Starting volume is {self.volume}.")

    @function_tool()
    async def set_volume(self, volume: int):
        self.volume = volume  # Access and update self attribute
Also, if you're thinking about session-wide or shared state, the
RunContext
parameter comes in really handy because it gives you access to
userdata
. This is great for keeping data persistent, especially when agents hand off tasks. Here's an example for that:
Copy code
from livekit.agents import function_tool, RunContext

class ReservationAgent(Agent):
    @function_tool()
    async def update_reservation_time(self, time: str, context: RunContext):
        context.userdata.reservation_time = time
        return f"Reservation set for {time}"
So, just remember: use
self
when you're dealing with agent-specific state, and
RunContext
when you want to manage shared or session-wide state. I hope this helps clarify things for you!