How to Make AI Take Real-World Actions + Code (Function Calling Explained)
Learn how modern AI models like GPT-4 and Claude 4 use function calling to interact with the real world, and how you can do it in just a few lines of code.
Function calling is having its moment, and for good reason. This week, Claude 4 launched with breakthrough capabilities that can work for nearly 7 hours straight. OpenAI's GPT-4.1 focuses on enhanced function calling for coding. MCP adoption is exploding so fast it's projected to overtake OpenAPI by July 2025.
This isn't just another tech trend. Function calling is the bridge between AI that can only talk about solutions and AI that can actually implement them. It's like the difference between having a brilliant consultant who's trapped behind glass and o
ne who can actually roll up their sleeves and get to work.
Let's understand this game-changing capability and learn to harness it.
Why AI Can't Take Action
Imagine having the world's most knowledgeable librarian. They know every book, every author, every detail about any topic you could imagine. They can analyze complex problems, provide brilliant insights, and explain solutions in perfect detail.
But here's the catch: their hands are permanently glued to their sides.
This librarian can tell you exactly which book contains the solution to your problem. They can quote passages from memory and explain concepts beautifully. But they can't actually walk over and hand you the book. You have to do all the legwork yourself.
This is exactly where traditional AI applications have been stuck. Need current weather? "I don't have access to real-time data." Want to book a meeting? "I can suggest scheduling, but you'll need to do it yourself." Need to send an email? "I can draft it perfectly, but you'll have to copy-paste it."
It's like having a genius assistant whose hands are tied behind their back.
What Function Calling Solves
Function calling shatters this glass box completely. It gives AI the ability to reach outside its conversational bubble and actually take action in the real world.
Think of it like suddenly untying that genius assistant's hands. Now they can't just analyze your calendar – they can actually update it. They can't just explain the weather – they can check the current conditions. They can't just suggest sending an email – they can compose and send it.
But here's what makes function calling truly remarkable: the AI doesn't just blindly execute functions when told. It intelligently recognizes when a function is needed, chooses the right tool from its available options, extracts the correct parameters from casual conversation, and seamlessly weaves the results back into natural dialogue.
It's like giving someone a well-organized toolbox with clear labels, then watching them automatically pick exactly the right tool for each job without you having to explain every scenario.
Why Learn This Now
You might wonder: "With MCP becoming the standard, why learn function calling directly?" Here's why understanding these fundamentals is more crucial than ever:
MCP is built on function calling principles. The Model Context Protocol's "Tools" primitive is essentially function calling implemented as a standard. Claude 4's "extended thinking with tool use" that can work for 7 hours straight? That's sophisticated function calling. GPT-4.1's enhanced coding capabilities? Function calling optimized for development tasks.
The current AI arms race centers on this capability. When GitHub launches a coding agent that can automatically fix bugs, it's using function calling to interact with development tools. When companies report 6x growth in AI spending, they're investing in systems that can take action, not just provide advice.
Understanding function calling is like learning to drive – it doesn't matter if you're driving a Tesla, a Toyota, or whatever comes next, the fundamental skills transfer. Whether you build MCP servers, use OpenAI's APIs, or work with future standards, these principles remain constant.
Building Your First Function-Calling AI
Let's transform theory into practice. We'll build an AI that can access real-time information, demonstrating the core pattern you'll use in any function calling scenario.
Think of this as teaching your AI to use its first tool.
Setup and Basic Function
import os
import json
from openai import OpenAI
client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY")
)
def get_current_weather(location: str) -> str:
"""
Returns current weather for the given location.
In production, this would call a real weather API.
"""
return f"The weather in {location} is sunny with a high of 25°C."
This function is like the first tool in your AI's toolbox. Simple, but powerful. In a real application, this would connect to a weather API, but the AI doesn't care where the data comes from – it just needs to know how to ask for it.
Function Schema Definition
Now comes the crucial part: teaching your AI about its new capability. This is like writing a clear instruction manual for each tool in the toolbox:
weather_tool = {
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and country (e.g. 'London, UK')"
}
},
"required": ["location"]
}
}
}
This schema is like writing instructions for a new employee. You're not just saying "here's a hammer" – you're explaining when to use it, what it needs to work, and providing examples. Notice the location example in the description. These details matter enormously for reliable function calling.
Conversation Setup
messages = [
{
"role": "system",
"content": "You are a helpful assistant. When users ask about current weather, "
"use the get_current_weather function to get accurate data."
},
{
"role": "user",
"content": "What's the weather in London right now?"
}
]
The system message is like giving context to your assistant: "Here's your job, here are the tools available, and here's when to use them."
Making the API Call
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=[weather_tool],
tool_choice="auto" # Let AI decide when to use functions
)
assistant_message = response.choices[0].message
This is where the magic happens. Instead of the AI apologetically saying "I don't have access to real-time data," it recognizes it has a tool available and responds with:
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_current_weather",
"arguments": "{\"location\": \"London, UK\"}"
}
}
]
}
Look at what happened automatically:
It recognized "London" required current weather data
It chose the right function from its toolbox
It formatted the location parameter correctly (even expanding "London" to "London, UK")
It structured the call properly
This is like watching someone fluently use a new tool after just reading the manual once.
Executing Functions
if assistant_message.tool_calls:
messages.append(assistant_message)
for tool_call in assistant_message.tool_calls:
function_name = tool_call.function.name
function_args = json.loads(tool_call.function.arguments)
if function_name == "get_current_weather":
function_result = get_current_weather(**function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": function_result
})
# Get final answer
final_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print("Assistant:", final_response.choices[0].message.content)
The AI now takes the function result and creates a natural response like: "The weather in London, UK is currently sunny with a high of 25°C. Perfect day to be outside!"
What's beautiful is how seamlessly the AI transitions between reasoning, action, and communication. It's like watching a skilled craftsperson who can think about a problem, use the right tool, and explain the result all in one fluid motion.
Complete Implementation
Here's everything together – your first AI that can actually take action:
import os
import json
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
def get_current_weather(location: str) -> str:
return f"The weather in {location} is sunny with a high of 25°C."
weather_tool = {
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather for a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and country (e.g. 'London, UK')"
}
},
"required": ["location"]
}
}
}
messages = [
{"role": "system", "content": "You are a helpful assistant. Use available functions when needed."},
{"role": "user", "content": "What's the weather in Tokyo?"}
]
# Make initial request
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
tools=[weather_tool],
tool_choice="auto"
)
assistant_message = response.choices[0].message
# Handle function calls
if assistant_message.tool_calls:
messages.append(assistant_message)
for tool_call in assistant_message.tool_calls:
function_args = json.loads(tool_call.function.arguments)
function_result = get_current_weather(**function_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": function_result
})
final_response = client.chat.completions.create(
model="gpt-4o-mini",
messages=messages
)
print("Assistant:", final_response.choices[0].message.content)
Congratulations! You've just built an AI agent that can bridge the gap between conversation and action.
Real-World Applications
The patterns you've learned are powering some of the most exciting AI developments happening right now:
GitHub's new coding agent uses function calling to automatically fix bugs, run tests, and update documentation. It's like having a tireless developer who can actually make changes, not just suggest them.
Claude 4's breakthrough capabilities can maintain focus across 7-hour workflows by chaining function calls together. Imagine an AI project manager that can coordinate multiple tools and systems throughout an entire workday.
Enterprise AI transformations are happening because companies can now build AI that integrates with their existing CRM systems, databases, and workflows. It's the difference between having an AI advisor and having an AI employee.
Whether you're building customer service bots that can actually process refunds, research assistants that can query real databases, or automation tools that connect multiple platforms, the pattern is always the same: describe your tools clearly, let AI decide when to use them, execute safely, and integrate results naturally.
Advanced Patterns
Modern AI models are showcasing function calling patterns that feel almost magical:
Function chaining is like watching a master chef who can taste a dish, adjust seasoning, check the oven, and plate the result all in one smooth workflow. Claude 4 can maintain context across complex projects, calling functions as needed throughout multi-hour tasks.
Parallel execution is like having an octopus assistant – GPT-4.1 can call multiple functions simultaneously, dramatically speeding up coding tasks by running tests while updating documentation while checking dependencies.
Conditional logic creates AI that adapts like a smart salesperson. Check customer status first, then choose different approaches based on whether they're a new lead or VIP client.
Error recovery gives AI the resilience of an experienced troubleshooter. When one approach fails, try alternatives or ask clarifying questions instead of giving up.
These patterns transform AI from simple responders into intelligent agents that can navigate complex, real-world scenarios.
It's awesome I liked it thanks for writing this for us!
Great write up. Like many foundational technologies, that’s impressive how elegant and simple it is at its core, and how powerful it becomes at scale.