๐ Function Calling
Function calling is a structured way for AI agents to invoke tools by outputting precise, machine-readable commands (usually in JSON format) instead of natural language descriptions. Instead of saying "I would search the web for...", the AI outputs {"function": "web_search", "query": "latest news"} โ which a system can actually execute.
Think of it as the difference between telling someone "please turn on the lights" versus flipping the actual light switch. Function calling is the AI flipping the switch.
๐ฏ Why This Mattersโ
Function calling is the backbone of modern AI tool usage. Understanding it helps you:
- Design prompts where AI can reliably trigger external actions
- Build systems where AI output is parseable by code, not just readable by humans
- Understand how OpenAI function calling, Claude tool use, and similar features work
- Create structured integrations between AI and your apps, databases, and APIs
- Extract structured parameters from natural language user requests
๐ The Concept in Detailโ
How Function Calling Worksโ
User Message โ AI Processes โ Function Call Output
"What's the weather Understands {"function": "get_weather",
in Tokyo?" intent & "parameters": {
extracts "city": "Tokyo",
parameters "unit": "celsius"
}}
JSON Function Signaturesโ
Functions are described to the AI using structured signatures:
{
"name": "get_weather",
"description": "Get the current weather for a given city",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "The city name, e.g., 'New York'"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Temperature unit"
}
},
"required": ["city"]
}
}
Parameter Extraction from Natural Languageโ
The AI's job is to map messy human language to clean function parameters:
| User Says | Extracted Function Call |
|---|---|
| "How hot is it in Paris?" | get_weather(city="Paris", unit="celsius") |
| "Send an email to John about the meeting" | send_email(to="John", subject="meeting") |
| "Find flights to London next Friday" | search_flights(destination="London", date="next Friday") |
| "Delete my last order" | cancel_order(order_id="last") |
๐ก Prompt Examplesโ
Teaching Function Calling via Promptsโ
You are an AI assistant with access to the following functions.
When the user's request requires a function call, respond ONLY
with the JSON function call โ no other text.
AVAILABLE FUNCTIONS:
1. search_products(query: string, category?: string, max_price?: number)
โ Searches the product catalog
2. get_order_status(order_id: string)
โ Returns the status of an order
3. create_support_ticket(issue: string, priority: "low"|"medium"|"high")
โ Creates a customer support ticket
RESPONSE FORMAT:
If a function call is needed:
```json
{"function": "function_name", "parameters": {...}}
If no function is needed, respond normally in plain text.
User: I'm looking for wireless headphones under $50
### Multi-Function Calling
```text
You are an AI agent that can call multiple functions to complete
a task. Available functions:
1. search_database(table: string, filters: object)
โ Queries a database table with filters
2. calculate(expression: string)
โ Evaluates a mathematical expression
3. format_report(title: string, data: array, chart_type?: string)
โ Creates a formatted report
4. send_notification(channel: string, message: string)
โ Sends a notification via email/slack/sms
When you need multiple function calls, output them as a JSON array:
```json
[
{"function": "func1", "parameters": {...}},
{"function": "func2", "parameters": {...}}
]
If a function's output is needed as input for another function, indicate this with:
{"function": "func2", "parameters": {"data": "$result_of_func1"}}
User: Find all orders over $1000 from last month, calculate the total revenue, and send a summary to the sales team on Slack.
---
## โ Bad Example
```text
You can search products, check orders, and create tickets.
Help the user with their request.
User: I need headphones under $50.
Why it fails: Functions aren't formally defined. The AI will respond in natural language ("I'd recommend searching for headphones...") instead of producing a structured, executable function call. No system can parse this output automatically.
โ Improved Exampleโ
You are a function-calling assistant. Respond ONLY with valid JSON
when a function call is needed.
FUNCTIONS:
search_products:
description: Search product catalog
parameters:
- query (string, required): Search terms
- category (string, optional): Product category
- max_price (number, optional): Maximum price in USD
- sort_by (string, optional): "price_asc"|"price_desc"|"rating"
returns: Array of products with name, price, and rating
get_product_details:
description: Get full details for a specific product
parameters:
- product_id (string, required): The product ID
returns: Full product details including specs and reviews
RULES:
- Output ONLY the JSON function call, nothing else
- If the user's request is ambiguous, pick the most likely function
- Include only parameters that can be extracted from the user message
- Use null for optional parameters not mentioned by the user
User: I need wireless headphones under $50, show me the best rated ones.
Expected output:
{
"function": "search_products",
"parameters": {
"query": "wireless headphones",
"max_price": 50,
"sort_by": "rating"
}
}
Why it works: Functions have formal definitions with typed parameters. Rules are clear about output format. The AI produces machine-parseable JSON that a system can directly execute.
๐งช Try It Yourselfโ
๐งช Try It Yourself
Edit the prompt and click Run to see the AI response.
๐๏ธ Practice Challengeโ
Design a function-calling prompt for a calendar management assistant with these functions:
create_event(title, date, time, duration, attendees[])find_free_slots(date, min_duration)reschedule_event(event_id, new_date, new_time)cancel_event(event_id, notify_attendees: boolean)
Write the full prompt with function signatures, rules, and test it with: "Move my 2pm meeting with Sarah to tomorrow at 3pm and let her know."
๐ Real-World Scenarioโ
Scenario: You're building a smart home assistant that controls lights, thermostat, music, and locks.
Function calling lets the AI translate natural language into precise commands:
- "It's too cold" โ
set_thermostat(temperature=72, unit="fahrenheit") - "Movie night" โ
[dim_lights(room="living", brightness=20), play_music(playlist="ambient"), close_blinds(room="living")] - "I'm leaving" โ
[lock_doors(all=true), set_thermostat(mode="away"), turn_off_lights(all=true)]
The key is defining clear function signatures so the AI can reliably map intent to action.
๐ค Interview Questionโ
Q: What is function calling in the context of AI agents, and how does it differ from regular prompting?
A: Function calling is a structured mechanism where the AI outputs machine-readable commands (typically JSON) instead of natural language text. It differs from regular prompting in three ways: First, the output is structured โ JSON with specific function names and typed parameters, making it parseable by code. Second, functions are formally defined with names, descriptions, parameter types, and required/optional flags, similar to API documentation. Third, parameter extraction happens automatically โ the AI maps natural language input to structured parameters. This enables reliable AI-to-system integration, which is the foundation of OpenAI's function calling API, Claude's tool use, and most production AI agent frameworks.
๐ Summaryโ
- Function calling produces structured, machine-readable tool invocations (usually JSON)
- Define functions with clear signatures: name, description, parameter types, and required fields
- The AI's job is to extract parameters from natural language and map them to function calls
- Always specify the output format (JSON only, no extra text)
- Support multi-function calls for complex tasks that need multiple actions
- Function calling is the standard pattern used by OpenAI, Claude, and modern agent frameworks
- It bridges the gap between human language and executable system actions