Loading...

How to Use Function Calling to Connect LLMs to Live Data

A large language model's knowledge is static; it's frozen at the time of its training. It doesn't know today's weather, the current price of a stock, or the status of your latest order. Function calling is the mechanism that bridges this gap, transforming a conversational AI into a dynamic agent that can interact with the real world through APIs.

Image Description

This guide will walk you through the concept and a practical example of function calling. We'll cover the developer workflow for defining functions, letting the model choose when to use them, and executing the code to get live, real-world information into your AI application.

What Is Function Calling?

Function calling (or tool use) is a capability where an LLM, when faced with a user prompt it can't answer from its internal knowledge, can request to call an external function that you have defined. The model doesn't execute the function itself. Instead, it generates a structured JSON object containing the name of the function it wants to call and the arguments to pass to it.

Your application code then receives this JSON, executes the actual function (e.g., calls a weather API), and sends the result back to the LLM. The model then uses this new information to formulate its final, data-informed answer to the user.

The Core Workflow in Three Steps

The entire process follows a predictable loop. Understanding these three steps is key to building any application that uses LLM tools.

  • Define Your Tools: In your initial API call to the LLM, you provide a list of available functions. You describe what each function does, what parameters it accepts, and what it returns, typically using a format like JSON Schema. For a weather tool, you'd define a function named `get_current_weather` that accepts a `location` parameter.
  • Model Decides and Generates JSON: The user sends a prompt, like “What's the weather like in London?” The LLM analyzes the prompt, sees that `get_current_weather` can answer it, and instead of responding with text, it outputs a special message requesting a function call, like: `{ "name": "get_current_weather", "arguments": { "location": "London" } }`.
  • Execute and Respond: Your code parses this JSON, calls your actual `get_current_weather("London")` function, which fetches the data from a weather API. You then make a second call to the LLM, feeding it the result (e.g., “The weather in London is 15°C and cloudy”). The LLM uses this information to generate a natural language response for the user, such as, “The current weather in London is 15°C and cloudy.”

A Simple Python Example

Let's imagine a simplified example using Python. First, you'd define your function and a schema that describes it to the LLM.

# 1. Define the actual function
def get_current_weather(location):
# In a real app, this would call a weather API
if "london" in location.lower():
return {"temperature": "15", "condition": "cloudy"}
return {"temperature": "unknown"}

# 2. Define the schema for the LLM
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA"
}
},
"required": ["location"]
}
}
}
]

When a user asks about the weather in London, the model would return a request to call `get_current_weather`. Your code would execute it, get the weather data, and pass that data back to the model in a follow-up call to generate the final human-readable answer.

Best Practices for Defining Functions

The model's ability to correctly use your tools depends entirely on how well you describe them. Follow these best practices for reliable results:

  • Use clear, descriptive names: `get_weather` is good, `gw` is bad.
  • Write detailed descriptions: The `description` field is crucial. Explain what the function does, its limitations, and when it should be used. For example, “Fetches the current weather, not the forecast.”
  • Be specific about parameters: Clearly describe what each parameter is and provide examples. This helps the model extract the right information from the user's prompt.

Frequently Asked Questions (FAQ)

Does the LLM have to call a function?

No. You can configure the model to call a function when it deems necessary, force it to always call a specific function, or let it decide. If the user's question can be answered from its internal knowledge, it will typically respond directly without requesting a function call.

Can I define multiple functions?

Yes, you can provide a list of many different tools. The model will analyze the user's prompt and select the most appropriate function (or sequence of functions) to achieve the goal.

Is this secure?

The security is in your hands. The LLM only generates a request; your code is responsible for execution. You should never execute code directly from the model without validation. Treat the arguments provided by the LLM as untrusted user input and sanitize them accordingly before passing them to any real API or database.

Key Takeaways

  • Function calling allows an LLM to access live, external data through APIs you provide.
  • The process is a loop: you define tools, the model requests a tool call with arguments, and your code executes the tool and returns the result.
  • The model does not execute code; it only generates a structured JSON request.
  • Clear and descriptive function names and descriptions are critical for the model to understand when and how to use your tools.
  • You are responsible for the safe execution of any function calls requested by the model.

Related Reading

  • The ReAct Framework: How AI Agents Combine Reasoning and Action
  • How to Build a Simple AI Agent to Automate Your Digital Tasks
  • What Is RAG? The AI Workflow That Connects LLMs to Your Data

Tagsberulearning