Skip to content

Tool Calling in LLM

Key idea:

Tool Calling (aka Function Calling) — the way an LLM invokes external functions via structured output (usually JSON). Client supplies a tool schema → LLM decides which to call with what args → client executes → result returns to LLM → LLM synthesises final answer. Standard in OpenAI, Anthropic, Gemini APIs. Needed for agents, on-demand RAG, database queries, external integrations.

Below: details, example, related terms, FAQ.

Check your site →

Details

  • Tool schema: name, description, JSON Schema for args
  • LLM returns structured output { "tool": "name", "args": {...} }
  • Client executes tool → returns result to LLM
  • Parallel tool calls: LLM can invoke multiple tools simultaneously (OpenAI 2023+)
  • Model Context Protocol (MCP) — Anthropic 2024 spec for tool standardisation

Example

# OpenAI tool calling
tools = [{
  'type': 'function',
  'function': {
    'name': 'get_weather',
    'description': 'Get current weather',
    'parameters': {
      'type': 'object',
      'properties': {'location': {'type': 'string'}},
      'required': ['location']
    }
  }
}]

response = openai.chat.completions.create(
  model='gpt-5',
  messages=[{'role':'user','content':'Weather in Moscow?'}],
  tools=tools
)
# response.choices[0].message.tool_calls → [{name, arguments}]

Related Terms

TL;DR

Tool calling, or function calling, in large language models (LLMs) refers to the ability of AI systems to execute specific functions or commands within a structured environment. This capability allows LLMs to interact with external APIs, databases, or internal functions, enhancing their ability to provide accurate, context-aware responses. For instance, an LLM can call a weather API to retrieve real-time data when asked about current weather conditions.

Understanding Function Calling in LLMs

Function calling in LLMs is a paradigm that enhances the interaction between AI models and external systems. This feature enables models to execute pre-defined functions based on user prompts, thereby extending their utility beyond static text generation. Function calling is particularly relevant in applications where real-time data retrieval, computation, or external interaction is essential. For example, consider an LLM integrated with a financial API. When a user asks about the current stock price of a specific company, the LLM can call a function that queries the API for the latest data, providing a precise and timely response.

In technical terms, function calling typically involves a structured interface where the LLM can access various functions. These functions can be defined in a programming language such as Python or JavaScript, and they must conform to specific input-output schemas. For instance, a function to retrieve stock prices might be defined as:

def get_stock_price(ticker: str) -> float:

This function accepts a stock ticker symbol as input and returns the current price as a floating-point number. The LLM interprets user queries to identify relevant functions and formats the input accordingly before executing the function.

Moreover, function calling can be integrated with context management systems that track user sessions and preferences. This integration allows LLMs to maintain context over multiple interactions, making them more responsive and personalized. For example, if a user frequently inquires about technology stocks, the LLM can prioritize function calls related to tech stock prices in future interactions.

Practical Example of Implementing Function Calling

To implement function calling in an LLM, developers typically use a framework that supports API integrations and function definitions. Below is a practical example using Python with the FastAPI framework to create a simple API that the LLM can call to retrieve weather information.

First, ensure you have FastAPI and an ASGI server like Uvicorn installed:

pip install fastapi uvicorn

Next, create a Python file, weather_api.py, with the following content:

from fastapi import FastAPI
import requests

app = FastAPI()

@app.get("/weather/{city}")
def get_weather(city: str):
    api_key = 'your_api_key'
    response = requests.get(f'http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}')
    return response.json()

This code snippet defines a simple API endpoint that retrieves weather data for a specified city using the OpenWeatherMap API. Replace your_api_key with a valid API key from OpenWeatherMap.

To run the API, execute the following command:

uvicorn weather_api:app --reload

Once the server is running, the LLM can be programmed to call this API when a user requests weather information. For example, if a user types, “What’s the weather like in New York?”, the LLM can extract the city name and make a GET request to the API endpoint:

GET /weather/New%20York

The response from the API will be a JSON object containing the current weather data, which the LLM can then format and present to the user. This example illustrates how function calling enhances the capabilities of LLMs by enabling them to access real-time data and deliver more relevant responses.

Learn more

Frequently Asked Questions

Is tool calling reliable?

2026 models (GPT-5, Claude Opus 4.7) — 95%+ accuracy on simple tools. Complex tools (nested schemas) — test them.

What is MCP?

Model Context Protocol from Anthropic (2024) — standard for exposing tools. Clients (Claude Desktop, Zed IDE) connect to MCP servers (file system, GitHub, Slack, etc).

Security?

LLM may call the wrong tool or with bad args. Check authorisation on the server, do not trust LLM output. Sandbox tool execution.

Try the live tool that powered this guide

Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.