Function Calling (Tool Use)

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.
{
  "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.

Related terms

  • AI Agents AI agents are LLM-powered systems that can autonomously plan, use tools, and take multi-step actions to complete complex goals. Unlike a simple chatbot, an agent can browse the web, write and execute code, call APIs, and adapt its approach based on intermediate results.
  • LLM (Large Language Model) A Large Language Model is a neural network trained on massive text corpora to predict and generate text. LLMs like GPT-4, Claude, and Gemini exhibit broad language understanding, reasoning, and generation capabilities that emerge from scale.
  • Grounding Grounding connects an LLM's outputs to verifiable, real-world information sources. A grounded model response is tied to specific, citable facts rather than relying purely on parametric knowledge, reducing hallucination and increasing trustworthiness.