Skip to main content

Function Tools

Function tools allow your AI agent to perform actions and interact with external services, extending its capabilities beyond simple conversation. By registering function tools, you enable your agent to execute custom logic, call APIs, access databases, and perform various tasks based on user requests.

Overview

Function tools are Python functions decorated with @function_tool that your agent can call during conversations. The LLM automatically decides when to use these tools based on the user's request and the tool's description.

External Tools

External tools are defined as standalone functions and passed into the agent's constructor via the tools parameter. This approach is useful for sharing common tools across multiple agents.

main.py
from videosdk.agents import Agent, function_tool

# External tool defined outside the class
@function_tool(description="Get weather information for a location")
def get_weather(location: str) -> str:
"""Get weather information for a specific location."""
# Weather logic here
return f"Weather in {location}: Sunny, 72°F"

class WeatherAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a weather assistant.",
tools=[get_weather] # Register the external tool
)

Internal Tools

Internal tools are defined as methods within your agent class and decorated with @function_tool. This approach is useful for logic that is specific to the agent and needs access to its internal state (self).

main.py
from videosdk.agents import Agent, function_tool

class FinanceAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a helpful financial assistant."
)
self.portfolio = {"AAPL": 10, "GOOG": 5}

@function_tool
def get_portfolio_value(self) -> dict:
"""Get the current value of the user's stock portfolio."""
# Access agent state via self
return {"total_value": 5000, "holdings": self.portfolio}

Async Function Tools

Function tools can be asynchronous, which is essential for making HTTP requests, performing I/O operations, or integrating with async VideoSDK features.

main.py
import aiohttp
from videosdk.agents import Agent, function_tool

class WeatherAgent(Agent):
def __init__(self):
super().__init__(
instructions="You are a weather assistant that can fetch real-time weather data."
)

@function_tool
async def get_weather_async(self, location: str) -> dict:
"""Fetch real-time weather data from an API."""
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.weather.com/{location}") as response:
data = await response.json()
return {
"location": location,
"temperature": data.get("temp"),
"condition": data.get("condition")
}
Sarvam AI LLM: When using Sarvam AI as the LLM option, function tool calls and MCP tools will not work. Consider using alternative LLM providers if you need function tool support.

Examples - Try Out Yourself

Got a Question? Ask us on discord