Back to series
· 12 min read

Function Calling and Tool Invocation: How Large Language Models Connect to External Tools and the Real World?


I. What Is Function Calling?

Function Calling, commonly referred to as function calling or tool calling, is a mechanism that enables large language models to request that external functions perform specific tasks.

Traditional large language models primarily respond to user requests by generating text. They can explain concepts, write code, and summarize information, but they cannot rely solely on their own parameters to directly perform real-time queries, read from or write to databases, send emails, or call business systems.

Function Calling provides the model with a structured interface for invoking external capabilities.

For example, a user asks:

Help me check today's weather in Beijing.

The model can determine that it needs to call a weather query tool and generate structured parameters:

{
  "location": "北京",
  "date": "2026-11-19"
}

Then the application uses these parameters to perform the actual weather query, returns the result to the model, and the model organizes it into a natural-language answer.

A core concept should be made clear:

Function Calling itself is usually not responsible for executing functions; instead, it enables the model to generate invocation intents and parameters, which the external application is responsible for executing.

From the perspective of responsibilities, it involves three parties:

Actor Core Responsibilities
Large language model Determine whether a tool needs to be called and generate invocation parameters
Application Parse invocation requests, execute functions, and manage the invocation flow
External tool Perform the actual operation and return the execution result

II. Overall Workflow of Function Calling

Function Calling does not mean the model directly accesses the operating system or external services; rather, it establishes a controlled invocation chain through the application.

The typical workflow is as follows:

flowchart TD
    A["User makes a request"] --> B["Application receives the request"]
    B --> C["Provide tool definitions to the model"]
    C --> D["Model analyzes user intent"]
    D --> E{"Is a tool call needed?"}
    E -->|"No"| F["Model generates an answer directly"]
    E -->|"Yes"| G["Model generates a tool invocation request"]
    G --> H["Application parses invocation parameters"]
    H --> I["Parameter validation and permission checks"]
    I --> J{"Is execution allowed?"}
    J -->|"No"| K["Reject or request additional authorization"]
    J -->|"Yes"| L["Execute external function"]
    L --> M["Obtain tool execution result"]
    M --> N["Return the result to the model"]
    N --> O["Model integrates the result"]
    O --> P["Return final answer"]

    style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style F fill:#E5F0E9,stroke:#83A995,color:#315D49
    style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style H fill:#EAF3EC,stroke:#83A995,color:#315D49
    style I fill:#EAF3EC,stroke:#83A995,color:#315D49
    style J fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style K fill:#F7EAE7,stroke:#C98D7C,color:#754438
    style L fill:#EAF3EC,stroke:#83A995,color:#315D49
    style M fill:#EAF3EC,stroke:#83A995,color:#315D49
    style N fill:#EAF3EC,stroke:#83A995,color:#315D49
    style O fill:#EAF3EC,stroke:#83A995,color:#315D49
    style P fill:#DCEFE5,stroke:#4F8B70,color:#24563F

The entire process can be divided into six stages:

  1. Tool definition: The application provides the model with the name, purpose, and parameter structure of callable tools.
  2. Intent recognition: The model determines whether the user request requires external capabilities.
  3. Parameter generation: The model generates structured invocation parameters based on the tool definitions.
  4. Tool execution: The application validates the invocation request and executes the corresponding function.
  5. Result return: The application submits the tool execution result to the model.
  6. Answer generation: The model generates the final answer based on the data returned by the tool.

There is a point here that is easy to confuse:

The model generating a tool invocation request does not mean that the tool has been successfully executed.

For example, if the model generates a request to call an email-sending function, this only shows that it has proposed the intent to send an email. Only when the application actually performs the email-sending operation and obtains the corresponding result can it confirm whether the email was sent successfully.

III. Core Components of Function Calling

The implementation of Function Calling generally revolves around tool definitions, parameter structures, call requests, and execution results.

1. Tool Definition

Tool definitions are used to tell the model:

  • What tools are available.
  • What task each tool can accomplish.
  • What parameters the tool needs to receive.
  • What the data types and constraints of the parameters are.

For example, define a weather query tool:

{
  "type": "function",
  "name": "get_weather",
  "description": "查询指定城市的天气信息",
  "parameters": {
    "type": "object",
    "properties": {
      "location": {
        "type": "string",
        "description": "需要查询天气的城市"
      },
      "unit": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"],
        "description": "温度单位"
      }
    },
    "required": ["location"],
    "additionalProperties": false
  },
  "strict": true
}

This contains several important fields:

Field Meaning
type Tool type; here it is function
name Tool name
description Tool function description
parameters Parameter definition
properties Parameter names and data structures
required Parameters that must be provided
enum Restricts the allowed values of a parameter
additionalProperties Whether undeclared additional parameters are allowed
strict Whether to enable strict structural constraints; specific support depends on the API

A tool definition is essentially a machine-readable interface contract.

The model generates call parameters according to this contract, and the application can validate and execute based on the same structure.

2. Function Name

The function name is used to identify a specific tool.

For example:

get_weather
search_database
calculate_tax
send_email
create_order

Function names should have clear business meaning.

The following are not recommended:

tool1
function_a
process_data

Because the names are too vague, they increase the difficulty for the model to choose a tool and are also inconvenient for developers to maintain.

3. Parameters: Parameter Definitions

Parameter definitions determine what data the model needs to provide.

For example, an order query tool:

{
  "type": "object",
  "properties": {
    "order_id": {
      "type": "string",
      "description": "订单编号"
    },
    "include_items": {
      "type": "boolean",
      "description": "是否包含订单商品明细"
    }
  },
  "required": ["order_id"],
  "additionalProperties": false
}

The user asks:

Query the item details for order ORD20261119001.

The model might generate:

{
  "order_id": "ORD20261119001",
  "include_items": true
}

Note that generating parameters does not mean the parameters are correct.

The model may generate nonexistent order numbers, incorrect data types, or even omit fields required by the business.

Therefore, the application cannot rely solely on the model's parameter-generation capability; it also needs server-side validation.

4. Tool Call: Tool Invocation Request

When the model decides to call a tool, it generates a structured invocation request.

API formats vary among model vendors, but they usually include:

  • Tool call ID.
  • Function name.
  • Function arguments.
  • Call type.

An abstract example is as follows:

{
  "id": "call_001",
  "type": "function",
  "function": {
    "name": "get_weather",
    "arguments": {
      "location": "北京",
      "unit": "celsius"
    }
  }
}

In actual APIs, arguments is sometimes returned as a JSON string rather than an already parsed JSON object.

During development, you must handle it according to the specific API protocol and must not assume that all vendors use exactly the same data structure.

5. Tool Result: Tool Execution Result

After a tool executes, the application needs to return the result to the model.

For example:

{
  "location": "北京",
  "temperature": 12,
  "condition": "晴",
  "humidity": 35
}

After receiving the result, the model can organize the structured data into natural language:

Beijing is sunny today, with a temperature of 12°C and air humidity of 35%.

Three distinct stages need to be distinguished:

  • Tool Call: The model issues an invocation request.
  • Tool Execution: The application performs the actual operation.
  • Tool Result: The tool returns the execution result.

These three stages must not be conflated.

IV. How Does the Model Decide Which Tool to Call?

One of the most important capabilities of Function Calling is enabling the model to determine, based on user intent, whether external tools are needed.

Suppose the application provides the model with three tools:

Tool Function
get_weather Query weather
search_flight Query flights
calculate Perform mathematical calculations

The user asks:

Help me look up tomorrow's flights from Beijing to Shanghai and estimate the flight time.

The model needs to identify:

  1. The user needs to query flight information.
  2. Flight queries require external real-time data.
  3. The flight time can be obtained from flight information, or a calculation tool can assist with processing it.
  4. search_flight should be called first.

This process can be abstracted as:

flowchart TD
    A["User inputs natural language"] --> B["Analyze task semantics"]
    B --> C["Extract task goal"]
    C --> D["Identify required external capabilities"]
    D --> E["Match available tools"]
    E --> F{"Is there a suitable tool?"}
    F -->|"No"| G["Answer directly or explain capability limits"]
    F -->|"Yes"| H["Select a tool"]
    H --> I["Extract invocation arguments"]
    I --> J["Generate a structured invocation request"]

    style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#F7F4E8,stroke:#C7B77A,color:#675A2D
    style G fill:#F7EAE7,stroke:#C98D7C,color:#754438
    style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style I fill:#EAF3EC,stroke:#83A995,color:#315D49
    style J fill:#DCEFE5,stroke:#4F8B70,color:#24563F

From the perspective of the model's internal mechanism, Function Calling is not a traditional function-execution module that is completely independent of the language model.

The model still makes predictions based on context; it is just that the output target is no longer limited to ordinary text but can also be structured content that complies with the tool-calling protocol.

The model needs to learn:

  • The correspondence between tool descriptions and task semantics.
  • The scenarios in which different tools apply.
  • The meaning of parameter fields.
  • The constraints between parameters.
  • When to call a tool and when to answer directly.

Therefore, the quality of a tool description directly affects how well the model selects tools.

V. How Does Function Calling Differ from Ordinary Text Generation?

The core difference is not whether a large language model is used, but the output target and the subsequent processing.

Comparison dimension Ordinary text generation Function Calling
Output form Natural language Structured call request
Primary goal Generate an answer Request external capabilities
Parameter structure Not fixed Organized according to the tool Schema
Subsequent processing Displayed to the user Parsed and executed by the application
Interaction with external systems Usually does not occur directly Can be accomplished through tools
Execution result Content generated by the model Returned by the external tool
Verifiability Requires additional verification Can be verified against the tool's execution result

For example, the user asks:

Calculate 125 × 36.

Ordinary text generation might answer directly:

125 × 36 = 4500.

Function Calling, by contrast, might produce:

{
  "name": "calculate",
  "arguments": {
    "expression": "125 * 36"
  }
}

The application performs the calculation and then returns the result to the model.

For simple calculations, the model can answer directly; for complex calculations, precise data queries, or tasks that require access to external systems, tool calling is usually more reliable.

VI. Invocation Modes of Function Calling

Different tasks require different levels of control over tool invocation. Common modes include automatic invocation, specifying a tool, and disallowing tool invocation.

1. Auto: Automatic selection

The model itself decides whether a tool needs to be called and which tool to choose.

User request
   ↓
Model analysis
   ↓
Decide whether a tool is needed
   ↓
Select a tool or answer directly

This is the most common mode and suits general-purpose AI assistants.

2. Required: A tool must be called

The model is required to initiate a tool call, but which specific tool to choose can still be decided by the model; the exact semantics are defined by the API.

It applies to:

  • Business processes that must obtain data through external systems.
  • Scenarios where the model is not allowed to answer directly from its internal knowledge.
  • Workflows that must call a retrieval tool.

Note that forcing a call does not mean a valid result is guaranteed. The tool may still fail, and the parameters still need to be validated.

3. Specified tool: Forcing a particular function

The application explicitly requires the model to call a designated tool.

For example, after a user enters the order query page, the system has already determined that the order query capability is needed.

In this case, the model can be required to call get_order instead of being allowed to freely choose among all tools.

4. None: Tool invocation disallowed

The model is prohibited from initiating tool calls and may only generate ordinary responses.

It applies to:

  • Plain text Q&A.
  • Scenarios where access to external systems is not permitted.
  • Situations where tool invocation permission is temporarily disabled.

Different APIs may name and behave differently for the tool selection parameter, so in actual development, refer to the documentation of the respective platform.

VII. Single Tool Call vs. Multiple Tool Calls

Real-world business scenarios often involve more than one tool call.

1. Single tool call

The user makes one clear request that requires only one tool call.

For example:

Query today's weather in Beijing.

Execution process:

flowchart TD
    A["User request"] --> B["Model selects the weather tool"]
    B --> C["Generate weather query parameters"]
    C --> D["Application calls the weather API"]
    D --> E["Return weather data"]
    E --> F["Model generates a natural language answer"]

    style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#DCEFE5,stroke:#4F8B70,color:#24563F

2. Multiple sequential calls

Some tasks require obtaining the result of one tool before deciding which tool to call next.

For example:

Query flights from Beijing to Shanghai, and query the transportation route from the corresponding airport to the city center.

The model may need to:

  1. Call the flight query tool.
  2. Obtain the departure airport and arrival airport.
  3. Call the route query tool.
  4. Generate a complete answer based on the results of both tools.
flowchart TD
    A["User submits a composite task"] --> B["Call the flight query tool"]
    B --> C["Retrieve flight and airport information"]
    C --> D["Model analyzes the next task"]
    D --> E["Call the route query tool"]
    E --> F["Retrieve transportation routes"]
    F --> G["Integrate results from multiple tools"]
    G --> H["Generate the final answer"]

    style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#EAF3EC,stroke:#83A995,color:#315D49
    style G fill:#EAF3EC,stroke:#83A995,color:#315D49
    style H fill:#DCEFE5,stroke:#4F8B70,color:#24563F

This approach has the following characteristics:

The parameters of a subsequent tool may depend on the result returned by the previous tool.

Therefore, tool calls must be continuously orchestrated by the application; all the steps cannot be treated as a single simple function execution.

3. Parallel Invocation of Multiple Tools

If multiple tools have no dependencies among them, they can be executed in parallel.

For example:

Query today's weather in Beijing, Shanghai, and Shenzhen separately.

The model can generate multiple weather query requests at the same time, and the application calls the weather services in parallel.

flowchart TD
    A["User submits weather queries for multiple cities"] --> B["Model generates multiple tool calls"]
    B --> C["Beijing weather query"]
    B --> D["Shanghai weather query"]
    B --> E["Shenzhen weather query"]
    C --> F["Aggregate execution results"]
    D --> F
    E --> F
    F --> G["Model generates a summary answer"]

    style A fill:#DCEFE5,stroke:#4F8B70,color:#24563F
    style B fill:#EAF3EC,stroke:#83A995,color:#315D49
    style C fill:#EAF3EC,stroke:#83A995,color:#315D49
    style D fill:#EAF3EC,stroke:#83A995,color:#315D49
    style E fill:#EAF3EC,stroke:#83A995,color:#315D49
    style F fill:#EAF3EC,stroke:#83A995,color:#315D49
    style G fill:#DCEFE5,stroke:#4F8B70,color:#24563F

Parallel invocation can reduce overall waiting time, but note the following:

  • There must be no unmet data dependencies between parallel tools.
  • The application needs to handle multiple call IDs correctly.
  • Partial success and partial failure must be taken into account.
  • The results of multiple tools need to be aggregated in a unified way.
  • Parallel execution does not mean the model has already obtained the final results of all tools.

VIII. Practical Code Implementation of Function Calling

The most important thing when understanding Function Calling is to distinguish between model invocation and function execution.

Below, we build a simplified example in Python.

1. Define the Business Functions

First, write the Python functions that actually perform the tasks:

def get_weather(location: str, unit: str = "celsius"):
    # Sample data; in a real project, you should call a weather service
    weather_data = {
        "北京": {
            "temperature": 12,
            "condition": "晴"
        },
        "上海": {
            "temperature": 18,
            "condition": "多云"
        }
    }

    result = weather_data.get(location)

    if result is None:
        return {
            "success": False,
            "error": "暂不支持该城市"
        }

    temperature = result["temperature"]

    if unit == "fahrenheit":
        temperature = temperature * 9 / 5 + 32

    return {
        "success": True,
        "location": location,
        "temperature": temperature,
        "unit": unit,
        "condition": result["condition"]
    }

Here, get_weather is the function that actually performs the weather lookup.

Function Calling does not automatically create or execute this Python function.

2. Provide tool definitions to the model

The application needs to send the tool's name, purpose, and parameter schema to the model.

The following uses a simplified tool definition format:

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "查询指定城市的天气",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "城市名称"
                    },
                    "unit": {
                        "type": "string",
                        "enum": [
                            "celsius",
                            "fahrenheit"
                        ]
                    }
                },
                "required": ["location"]
            }
        }
    }
]

This step only declares the tool capability to the model; it does not execute the weather lookup.

3. The model generates a tool call request

The application sends the user message and tool definitions to the model.

messages = [
    {
        "role": "user",
        "content": "查询北京今天的天气"
    }
]

The model may return a call request similar to the following:

{
  "name": "get_weather",
  "arguments": {
    "location": "北京",
    "unit": "celsius"
  }
}

Note: This is an example call request generated by the model, not the result of a function that has already been executed.

4. The application executes the tool

The application needs to find the corresponding local function based on the function name, then pass in the parameters generated by the model.

import json

function_map = {
    "get_weather": get_weather
}

def execute_tool(name: str, arguments):
    if name not in function_map:
        raise ValueError(f"未知工具:{name}")

    if isinstance(arguments, str):
        arguments = json.loads(arguments)

    if not isinstance(arguments, dict):
        raise ValueError("工具参数必须是 JSON 对象")

    allowed_keys = {"location", "unit"}

    if not set(arguments).issubset(allowed_keys):
        raise ValueError("存在未允许的参数")

    if "location" not in arguments:
        raise ValueError("缺少必需参数 location")

    return function_map[name](**arguments)

Here, function_map is the tool registry.

It maps the function names that the model can request to the actual functions that exist in the application.

This design is much safer than directly using eval() or exec() to execute code generated by the model.

5. Return the result to the model

After the tool executes successfully, the application converts the result to JSON and then submits it to the model.

For example:

{
  "success": true,
  "location": "北京",
  "temperature": 12,
  "unit": "celsius",
  "condition": "晴"
}

The model can use the tool results to compose the final answer.

The overall call flow is as follows: