# Function Calling (Tool Use)

Canonical: https://aimastermindlab.com/glossary/function-calling

---
Function calling allows LLMs to output structured calls to external functions or APIs instead of free-text responses. The model selects the appropriate function, fills in parameters from the user's request, and returns a structured JSON payload that your code can execute.

## What is Function Calling?

Function calling (also called tool use) is a capability that allows an LLM to respond with a structured call to a predefined function rather than generating free-form text. This is the primary mechanism by which LLMs interact with external systems — databases, APIs, search engines, code interpreters.

## How It Works

1. You define a set of functions with JSON Schema descriptions (name, parameters, types, descriptions).
2. These definitions are included in the API call.
3. The model decides whether to call a function or respond directly.
4. If calling, it returns a JSON object with the function name and arguments.
5. Your application executes the function and feeds the result back to the model.
6. The model generates a final response based on the function's output.

```json
{
  "name": "get_weather",
  "arguments": {
    "location": "London",
    "unit": "celsius"
  }
}
```

## Parallel Function Calling

Modern models (GPT-4o, Claude 3.5) can call multiple functions simultaneously when tasks are independent, reducing latency compared to sequential calls.

## Structured Outputs

A related capability: constraining the model to always return valid JSON matching a specific schema, even when not calling functions. Useful for data extraction, classification, and any task requiring structured responses.

## Reliability

Function calling is significantly more reliable than instructing a model to "respond in JSON format" via prompt alone. The model is specifically trained to produce valid, correctly-typed function arguments.

## Example

A travel booking agent uses function calling with three tools: search_flights(), check_hotel_availability(), and book_reservation(). The LLM decides which tools to call in sequence based on the user's natural language request.
---

Source: https://aimastermindlab.com/glossary/function-calling — AI Mastermind Lab (https://aimastermindlab.com)
