OpenAI Function Calling: How to Make AI Actually "Do" Things
Tired of the AI just talking safely? In this guide, we'll show you the simplest way to let an AI use your apps, check data, and perform real actions.
If you've played with ChatGPT, you know it's a great conversationalist. But if you ask it to "Change my password" or "Check my bank balance," it usually apologizes and says it can't.
Function calling is how we fix that. It's the "secret sauce" that lets the AI send orders to your code instead of just writing poems.
1. The Problem: AI is all talk
By default, models like GPT-4 are just "text predictors." They are guessing the next word. If you ask them for a data report, they might "hallucinate" (make up) the numbers because they can't actually see your database.
We need a way to say: "Hey, don't guess. If you need a report, ask me for it, and I'll go get the real data for you."
2. The Waiter Analogy
Imagine a restaurant.
- The User: You (the hungry customer).
- The AI: The Waiter.
- Your App: The Kitchen.
When you tell the Waiter (AI) "I want a Steak," the Waiter doesn't cook it. The Waiter takes your order, writes it down on a specific Order Form, and hands it to the Kitchen (Your App).
The Kitchen cooks the steak and gives it back to the Waiter, who then brings it to you. Function calling is just that Order Form.
3. What is an "Order Form" (Schema) really?
Every "Order Form" needs rules. If the kitchen needs to know how you want your steak cooked (Rare or Well Done), the form must have a spot for that.
In the programming world, we call this form a JSON Schema. It looks a bit like this:
{
"name": "get_weather",
"description": "Get current weather for a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string" },
"unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
},
"required": ["city"]
}
}By giving the AI these rules, it knows exactly how to "talk" to your code. If you want to build one of these rules without getting a headache, use our Simple Rule Builder.
4. The "Pinky Promise" (Strict Mode)
Sometimes, the AI gets creative. It might try to order a "Medium-Rare Chicken" even if your kitchen doesn't allow it.
OpenAI added something called Strict Mode. Think of it as a pinky promise. When you turn this on, the AI must follow your form exactly. No more guessing, no more weird extra fields. It either fills out the form perfectly, or it doesn't fill it out at all.
Note: To use this "Pinky Promise," you have to make sure every single blank on your form is marked as "Required."
5. Putting it all together (Sample Code)
Here is how a real app uses that "Order Form." Don't worry if you don't know all the code, just look at how the tools are passed to the AI:
import OpenAI from "openai";
const openai = new OpenAI();
// 1. Give the AI your "Order Form"
const tools = [{
type: "function",
function: {
name: "get_weather",
parameters: { ... } // <--- THE DATA RULES GO HERE
}
}];
// 2. Ask the AI a question
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "What's the weather in Tokyo?" }],
tools: tools,
});
// 3. The AI fills out the form!
console.log(response.choices[0].message.tool_calls[0].function.arguments);
// Output: {"city": "Tokyo"}6. Quick Tips for Beginners
1. Explain it like a toddler: When you describe a field in your form, be very clear. Instead of "Date," say "Today's date in Year-Month-Day format."
2. Use Enums (Choices): If you only have 3 colors, tell the AI! Giving it a list of choices (Enums) prevents it from making up its own colors.
3. Test your Output: Once the AI gives you an order, check it! Use a tool like our JSON Validator to make sure the AI didn't make a typo before you try to run your code.
Ready to try it yourself?
Jump into our visual builder and create your first "Order Form" for an AI agent. It's easier than it looks!