how to use Function calling with Voice Agents Inte...
# ask-ai
n
how to use Function calling with Voice Agents Integrate LLM-powered function calling into your agent. A*gents 1.0 available* This documentation is for v0.x of the LiveKit Agents framework. For the latest documentation, see Tool definition and use. Function calling (also known as "tool calling" or "tool use") is a powerful LLM capability that allows AI models to interact with external functions and tools. This allows your agent to retrieve additional context before generating a response or take real-world actions. For example, when a user says "What's the weather like in New York?", the LLM can intelligently detect the need to call your weather function before it responds. As another example, you could use function calling to trigger an action in the frontend, such as presenting a map to the user or querying their current location. Both
PipelineVoiceAgent
and
MultimodalAgent
have built-in support for function calling, making it easy to create rich voice-powered applications. Usage
Copy code
import aiohttp
from typing import Annotated

from livekit.agents import llm
from livekit.agents.pipeline import VoicePipelineAgent
from livekit.agents.multimodal import MultimodalAgent

# first define a class that inherits from llm.FunctionContext
class AssistantFnc(llm.FunctionContext):
    # the 
llm.ai_callable decorator marks this function as a tool available to the LLM
    # by default, it'll use the docstring as the function's description
    @llm.ai_callable()
    async def get_weather(
        self,
        # by using the Annotated type, arg description and type are available to the LLM
        location: Annotated[
            str, llm.TypeInfo(description="The location to get the weather for")
        ],
    ):
        """Called when the user asks about the weather. This function will return the weather for the given location."""
        logger.info(f"getting weather for {location}")
        url = f"https://wttr.in/{location}?format=%C+%t"
        async with aiohttp.ClientSession() as session:
            async with session.get(url) as response:
                if response.status == 200:
                    weather_data = await response.text()
                    # response from the function call is returned to the LLM
                    # as a tool response. The LLM's response will include this data
                    return f"The weather in {location} is {weather_data}."
                else:
                    raise f"Failed to get weather data, status code: {response.status}"

fnc_ctx = AssistantFnc()

# pass the function context to the agent
pipeline_agent = VoicePipelineAgent(
    ...
    fnc_ctx=fnc_ctx,
)

multimodal_agent = MultimodalAgent(
    ...
    fnc_ctx=fnc_ctx,
)
Forwarding to the frontend Function calls can be forwarded to a frontend application using RPC. This is useful when the data needed to fulfill the function call is only available at the frontend. It can also be used to trigger actions or UI updates in a structured way. For instance, here's a function that accesses the user's live location from their web browser:
t
Thanks for reaching out, Mohamed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
If you want to use function calling with Voice Agents in LiveKit, you can easily integrate LLM-powered function calling into agents like
PipelineVoiceAgent
and
MultimodalAgent
. This lets the AI fetch extra info or trigger actions before responding to you. Just a heads-up: these agents have been deprecated in Agents 1.0, and now we’re using the unified
AgentSession
interface instead. Here’s a quick guide for v0.x: 1. Define your functions using
llm.FunctionContext
and the
@llm.ai_callable()
decorator. These functions act like handy tools the LLM can use. 2. Pass that function context to your agent when you initialize it. 3. If needed, you can also forward function calls to the frontend via RPC for any frontend-specific data or actions. With Agents 1.0, it’s best to switch over to the
AgentSession
model. Instead of
@llm.ai_callable
, use
@function_tool
and define your tools right inside the agent class. This way, the tools become instantly available to the LLM—no separate function context needed. If you want to dive deeper or see some examples, check out the updated Tool definition and usage docs in your LiveKit setup. We’re here to help you get it running smoothly! Sources: Function calling with Voice Agents | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | AI voice agents | LiveKit Docs | Agents 0.x migration guide | LiveKit Docs | tool_calling/call_function_tool.py
n
(venv) PS C:\Users\ASUS\Desktop\Nancy AI\meeting-agent-livekit\livekitAzure-simli> python agent_worker.py dev --avatar-url http://localhost:8089/launch Traceback (most recent call last): File "C:\Users\ASUS\Desktop\Nancy AI\meeting-agent-livekit\livekitAzure-simli\agent_worker.py", line 130, in <module> class MediaFetcherFnc(llm.FunctionContext): ^^^^^^^^^^^^^^^^^^^ AttributeError: module 'livekit.agents.llm' has no attribute 'FunctionContext' (venv) PS C:\Users\ASUS\Desktop\Nancy AI\meeting-agent-livekit\livekitAzure-simli>
t
Hey there! The error you're seeing happens because
FunctionContext
was removed in LiveKit Agents 1.0. In the earlier versions (0.x),
FunctionContext
was how we defined callable functions for the LLM, but starting from 1.0, there's a new way to do this. To get things working smoothly, you’ll want to update your code to use the new tool definition model in Agents 1.0. Here’s a quick rundown: 1. Swap out
FunctionContext
for the
@function_tool
decorator. 2. Define your tools right inside your agent class instead of using a separate context object. 3. Arguments now come straight from function signatures and docstrings, so there’s no need for
Annotated
types anymore. Here’s a quick example to show you how it looks:
Copy code
from livekit.agents.llm import function_tool
from livekit.agents.voice import Agent

class MyAgent(Agent):
    @function_tool
    async def get_weather(self, location: str):
        """Retrieve weather information for a given location."""
        # Your implementation here
        return f"The weather in {location} is sunny."
Just update your code along these lines, and you should be all set! If you hit any snags or want me to walk you through it, just give me a shout—I’m here to help! Sources: Agents 0.x migration guide | LiveKit Docs | livekit-agents/livekit/agents/voice/agent_activity.py