Function Calling and Tool Use: How AIs Execute Real Actions
Discover how modern AI models go beyond chat to execute real-world actions via function calling and tool use, with practical code examples and best practices.

Is your company ready for AI? Download our free checklist →
Download checklistIntroduction
Imagine asking an AI to book a flight, send an email, or update a database, and it actually does it. That's the power of function calling and tool use—a paradigm shift from passive conversation to active task execution. In this post, we'll explore how AI models like GPT-4, Claude, and open-source alternatives can call external tools, APIs, and functions to perform real actions. We'll cover the underlying mechanisms, practical implementation, and key considerations for developers.
What is Function Calling?
Function calling (also known as tool use) allows an LLM to output structured data that specifies which function to call and with what parameters, rather than just generating text. The model doesn't execute the function; it returns a JSON object describing the desired invocation. The application then runs the function and feeds the result back to the model.
For example, a user says: "What's the weather in Tokyo?" The model might output:
{
"function": "get_weather",
"parameters": {
"location": "Tokyo",
"units": "celsius"
}
}
Your code intercepts this, calls your weather API, and returns the result to the model, which then formulates a natural language response.
How It Works: The Loop
- User query -> LLM
- LLM decides whether to call a function. If yes, it outputs a special JSON block.
- Application parses the JSON, calls the appropriate function with provided parameters.
- Function result (typically JSON) is sent back to the LLM as a tool response.
- LLM uses the result to generate a final answer in natural language.
This loop can be chained: the model can call multiple tools in sequence based on previous results.
Want a personalized diagnostic? Complete our free checklist →
Download checklistPractical Example with OpenAI
OpenAI's API supports function calling natively. Here's a minimal example using Python:
import openai
# Define the tool you want the model to use
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": "City name"},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}
},
"required": ["location"]
}
}
}
]
response = openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in London?"}],
tools=tools,
tool_choice="auto"
)
# Extract the tool call
message = response.choices[0].message
tool_calls = message.tool_calls
if tool_calls:
function_name = tool_calls[0].function.name
arguments = tool_calls[0].function.arguments
print(f"Function to call: {function_name}, Arguments: {arguments}")
# Now call your actual weather API with these arguments
# Then send the result back as a tool response
The model returns a special tool_calls field instead of content. Your application must handle this and subsequently call the model again with the function result.
Tool Use in Other Models
Anthropic's Claude, Google's Gemini, and open-source models like Llama 3 also support tool use. For example, Claude uses a similar structure but calls them "tools". Here's a snippet for Claude:
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-opus-4",
max_tokens=1024,
tools=[
{
"name": "get_weather",
"description": "Get weather",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
},
"required": ["location"]
}
}
],
messages=[{"role": "user", "content": "Weather in Paris?"}]
)
if response.stop_reason == "tool_use":
tool_block = response.content[-1]
print(tool_block.name, tool_block.input)
Best Practices
- Clear function descriptions: The model relies on your
descriptionfields to decide when to call a function. Be explicit about what the function does and when to use it. - Robust parameter handling: Validate parameters client-side; the model can hallucinate.
- Error handling: If the function fails, return a clear error message so the model can apologize or ask for clarification.
- Limit tool access: Only expose functions that are safe for the AI to invoke, especially if they modify data or cost money.
- Use
tool_choicewisely:autolets the model decide;requiredforces a tool call;nonedisables tools.
Real-World Applications
- Customer support: AI can check order status, initiate refunds, or escalate tickets.
- Data analysis: Connect to databases to query and visualize data.
- Automation: Trigger CI/CD pipelines, send Slack messages, or create calendar events.
- Research: Scrape web pages, search the internet, or query APIs like Wikipedia.
Security Considerations
Because function calling gives the AI power to act, security is paramount:
- Never expose destructive functions like
delete_database. - Add human-in-the-loop for sensitive actions (e.g., financial transactions).
- Sanitize all inputs before executing functions to prevent injection.
- Rate-limit and monitor API calls to avoid abuse.
Conclusion
Function calling and tool use transform AI from a chatbot into a capable agent. By defining tools and managing the conversational loop, you can build assistants that truly get things done. As models improve, the line between conversation and action will blur further—making now the perfect time to experiment.
For more details, check OpenAI's function calling documentation and Anthropic's tool use guide.
Ready for the next step? Evaluate your company with our free checklist →
Download checklistRelated posts
- AI & ML◈
Invisible AI Integration: How It's Quietly Reshaping Our Daily Lives
Invisible AI Integration: How It's Quietly Reshaping Our Daily Lives
Sep 30, 2026
- Backend▣
Ada Lovelace: The Victorian Visionary Who Wrote the First Algorithm in 1843
Ada Lovelace: The Victorian Visionary Who Wrote the First Algorithm in 1843
Sep 29, 2026
- AI & ML◈
Apple Unveils 2026 AI Developer Tools: A New Era for On-Device Intelligence
Apple Unveils 2026 AI Developer Tools: A New Era for On-Device Intelligence
Sep 28, 2026