What is the preferred way to have function calls f...
# ask-ai
c
What is the preferred way to have function calls for agents? Inside the prompt?
t
Thanks for reaching out, Bryce. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! Just a quick tip: the best way to enable function calls for agents isn’t by stuffing them inside the prompt. Instead, LiveKit offers some neat, dedicated ways to handle this: • Using
@function_tool
decorator:
You can define functions right inside an
Agent
class and simply decorate them with
@function_tool
. These functions then become automatically available for the LLM to call when it makes sense. • Using
llm.FunctionContext
with `@llm.ai_callable`:
Another way is to create a class that inherits from
llm.FunctionContext
, and mark its methods with
@llm.ai_callable()
. This helps you set up well-structured functions with clear, typed, and described parameters. Using these methods gives you more reliability, better type safety, and smoother integration compared to trying to squeeze function logic or triggers directly into the prompt. Hope this helps! Sources: examples/drive-thru/test_agent.py | Function calling with Voice Agents | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs
c
How does the agent know which one to use then?
t
Think of the agent like a helpful guide that picks the right function based on what you’re asking and the details of each function it knows about. The LLM looks at your request and figures out which function fits best by checking out the function’s name, parameters, and descriptions. For instance, when you use the
@function_tool
or
@llm.ai_callable
decorators, giving each function clear names and descriptions really helps the LLM get what each one does. That way, it can choose the right function to match what you want. Sometimes, more than one function might seem like a good fit. In those cases, the LLM uses its understanding of your input to pick the best one. So, taking a little extra time to define and describe your functions well makes everything run smoother and more accurately.
c
how can you add a description?
t
Hey there! If you want to add a description to functions in LiveKit, here are a couple of easy ways to do it: • Using the function's docstring: The description will be picked up automatically from the function's docstring. Here's a quick example:
Copy code
@function_tool
async def get_weather(self, location: str) -> str:
    """
    Called when the user asks about the weather.

    Args:
        location: The location to get the weather for
    """
    return f"The weather in {location} is sunny today."
• Specifying the description directly: You can also pass a
description
parameter right in the
@function_tool
decorator. Like this:
Copy code
@function_tool(description="Retrieves weather information for a specified location")
async def get_weather(self, location: str) -> str:
    return f"The weather in {location} is sunny today."
And if you want even more control, you can use
raw_schema
to manually define the function schema — including the name, description, and parameters. Adding clear, detailed descriptions really helps the LLM understand what your function does and pick it at the right time based on what the user needs. Sources: Agents 0.x migration guide | LiveKit Docs | Agents v0.x migration guide | LiveKit Docs | Tool definition and use | LiveKit Docs | Tool definition and use | LiveKit Docs | livekit-plugins/livekit-plugins-google/livekit/plugins/google/utils.py