Lesson 3.1: What is Function Calling
Duration: 45 minutes
Learning Objectives
By the end of this lesson, you will be able to:
- Explain what function calling is and why it exists
- Understand the difference between text generation and tool use
- Identify use cases where function calling is essential
- Describe the function calling flow from request to response
The Limitation of Pure Text Generation
AI language models are trained on text data with a knowledge cutoff date. When you ask a model about current weather, stock prices, or today's news, it cannot provide accurate answers because:
- Static Knowledge: Training data has a cutoff date
- No External Access: Models cannot browse the internet or query databases
- No Actions: Models cannot send emails, create files, or modify systems
Function calling solves these limitations by allowing the model to request execution of predefined functions that you control.
What is Function Calling
Function calling (also called tool use) is a capability that allows AI models to:
- Recognize when a user request requires external data or actions
- Select the appropriate function from a list you provide
- Generate the correct arguments for that function
- Receive the function's result
- Incorporate that result into the final response
The model does not execute functions directly. Instead, it tells your application which function to call and with what arguments. Your code executes the function and returns the result to the model.
The Function Calling Flow
Here is how function calling works step by step:
1. User: "What's the weather in Tokyo?"
2. Your App → AI Model:
- User message: "What's the weather in Tokyo?"
- Available tools: [get_weather, search_web, ...]
3. AI Model → Your App:
- Tool call: get_weather({ location: "Tokyo" })
4. Your App:
- Executes get_weather("Tokyo")
- Gets result: { temp: 22, condition: "sunny" }
5. Your App → AI Model:
- Tool result: { temp: 22, condition: "sunny" }
6. AI Model → Your App:
- Final response: "The weather in Tokyo is sunny with a temperature of 22°C."
7. Your App → User:
- Display the response
The model decides when to use a tool and generates the arguments. Your application handles the actual execution.
Text Generation vs Function Calling
| Aspect | Text Generation | Function Calling |
|---|---|---|
| Output | Free-form text | Structured tool calls |
| Data | Training data only | Real-time external data |
| Actions | Cannot perform | Can trigger actions |
| Accuracy | May hallucinate facts | Uses actual data |
| Control | Limited | You control tool behavior |
Real-World Use Cases
Function calling enables powerful applications:
1. Data Retrieval
- Check current weather
- Look up stock prices
- Query databases
- Search internal documents
2. System Integration
- Create calendar events
- Send notifications
- Update CRM records
- Generate reports
3. Calculations
- Perform complex math
- Convert currencies
- Calculate shipping costs
- Process financial data
4. External APIs
- Book reservations
- Process payments
- Translate text with specific services
- Fetch social media data
A Simple Example
Here is what function calling looks like in code with OpenAI:
import OpenAI from 'openai';
const openai = new OpenAI();
// Define a simple tool
const tools: OpenAI.ChatCompletionTool[] = [
{
type: 'function',
function: {
name: 'get_current_time',
description: 'Get the current time in a specific timezone',
parameters: {
type: 'object',
properties: {
timezone: {
type: 'string',
description: "The timezone, e.g., 'America/New_York'",
},
},
required: ['timezone'],
},
},
},
];
// Make a request with tools available
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What time is it in New York?' }],
tools: tools,
});
// Check if the model wants to call a function
const message = response.choices[0].message;
if (message.tool_calls) {
console.log('Tool call requested:');
console.log(message.tool_calls[0].function.name);
console.log(message.tool_calls[0].function.arguments);
}
When you run this, the model recognizes that answering the question requires knowing the current time and requests the get_current_time function with timezone: "America/New_York".
Key Terminology
Understanding these terms is essential:
- Tool: A function definition provided to the model
- Tool Call: The model's request to execute a specific tool
- Tool Result: The output returned after executing the tool
- Tool Choice: Control over whether the model must/can/cannot use tools
How Models Decide to Use Tools
AI models use tools based on:
- User Intent: Does the request require external information?
- Tool Availability: Is there a relevant tool in the provided list?
- Tool Description: Does the description match the user's need?
- Parameter Match: Can the model extract the required arguments?
Good tool descriptions are critical. The model relies on them to decide when to use each tool.
Parallel Tool Calls
Modern models can request multiple tool calls simultaneously:
User: "What's the weather in Tokyo and New York?"
Model Response:
- Tool call 1: get_weather({ location: "Tokyo" })
- Tool call 2: get_weather({ location: "New York" })
Your application should handle executing multiple tools and returning all results.
Key Takeaways
- Function calling bridges AI and external systems by letting models request function execution
- The model decides when to use tools based on user requests and tool descriptions
- Your code executes the functions and returns results to the model
- Tool descriptions are critical for the model to select the right function
- Parallel calls are supported for efficiency with multiple operations
Resources
| Resource | Type | Level |
|---|---|---|
| OpenAI Function Calling Guide | Documentation | Beginner |
| Anthropic Tool Use | Documentation | Beginner |
| Function Calling Best Practices | Documentation | Intermediate |
Next Lesson
In the next lesson, you will learn how to define tools with proper schemas that AI models can understand and use effectively.