when I develop a realtime agent, how to handle fun...
# ask-ai
h
when I develop a realtime agent, how to handle function tool automatically?
t
Thanks for reaching out, YUNYI. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
To handle function tools automatically when developing a realtime agent in LiveKit, you can define tools using the
@function_tool
decorator in your agent class. This allows the LLM to access these tools automatically. For example:
Copy code
from livekit.agents import function_tool, Agent, RunContext

class MyAgent(Agent):
    @function_tool()
    async def lookup_weather(self, context: RunContext, location: str) -> dict[str, Any]:
        """Look up weather information for a given location.
        
        Args:
            location: The location to look up weather information for.
        """
        return {"weather": "sunny", "temperature_f": 70}
You can also dynamically create or update tools for an agent. For instance: 1. *Create tools when the agent is created:*`agent = MyAgent( instructions="You are a helpful assistant...", tools=[ function_tool( _get_course_info, name="get_course_info", description="Get information about a course", ) ], )` 2. *Update tools after the agent is created:*`await agent.update_tools( agent.tools + [function_tool(_random_number, name="random_number", description="Get a random number")] )` The
update_tools()
method ensures that tools are updated even during an ongoing realtime session. Sources: Tool definition and use | LiveKit Docs | GitHub::livekit::agents | livekit-agents/livekit/agents/voice/agent.py