bulky-wall-61623
09/11/2025, 9:47 AMtall-airline-16756
09/11/2025, 9:47 AMtall-airline-16756
09/11/2025, 9:47 AM@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:
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:
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!