numerous-whale-53652
09/08/2025, 2:42 PMPipelineVoiceAgent and MultimodalAgent have built-in support for function calling, making it easy to create rich voice-powered applications.
Usage
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:tall-airline-16756
09/08/2025, 2:42 PMtall-airline-16756
09/08/2025, 2:43 PMPipelineVoiceAgent 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.pynumerous-whale-53652
09/08/2025, 2:47 PMtall-airline-16756
09/08/2025, 2:48 PMFunctionContext 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:
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