Function Calling Fundamentals
From the course AI Function Calling & Tool Use
What Is Function Calling?
Function calling lets AI models request the execution of external functions, bridging the gap between language understanding and real-world actions.
The Function Calling Loop
- Define — Describe available functions with JSON Schema
- Generate — The model decides which function(s) to call and with what arguments
- Execute — Your code runs the actual function
- Return — Send the result back to the model
- Respond — The model incorporates the result into its response
Schema Design
Good schemas are the foundation of reliable function calling:
{
"name": "search_products",
"description": "Search the product catalog by name, category, or price range. Returns matching products with details.",
"parameters": {
"type": "object",
"properties": {
"query": { "type": "string", "description": "Search term or product name" },
"category": { "type": "string", "enum": ["electronics", "clothing", "books", "home"] },
"min_price": { "type": "number", "description": "Minimum price in USD" },
"max_price": { "type": "number", "description": "Maximum price in USD" },
"in_stock": { "type": "boolean", "default": true }
},
"required": ["query"]
}
}
Description Best Practices
- Be specific about what the function does and returns
- Include example values in parameter descriptions
- Document edge cases and limitations
- Use enums to constrain values wherever possible
Common Pitfall: Writing Vague Function Descriptions
A vague description like "Search function" or "Gets data" leaves the AI guessing when to call a tool. Write descriptions that specify the exact trigger, the expected inputs, and what the function returns:
{
"name": "search_products",
"description": "Search the product catalog by keyword, category, or price range. Use this when the user is looking for specific products or browsing a category. Returns up to 20 matching products with name, price, and stock status.",
...
}
Best Practice: Test Tool Calling with Adversarial Prompts
Verify your schema handles edge cases by testing with unusual inputs designed to trigger incorrect behavior:
const tests = [
"Show me everything", // Should search, not dump all products
"I want a laptop under $500", // Should call with max_price: 500
"What is the cheapest thing?", // Should search + sort, not guess
];
for (const prompt of tests) {
const result = await callModelWithTools(prompt, tools);
console.log(prompt, "→", JSON.stringify(result.tool_calls));
}
Named Product Reference: Anthropic Claude Tool Use
Claude's tool use is less prone to hallucinating tool calls compared to other providers — it prefers asking for clarification over calling a tool with guessed parameters. For high-stakes use cases (payments, bookings, sends), Claude's conservative tool selection behavior reduces error rates.
Key Takeaways
- •Function calling bridges AI language understanding with real-world actions
- •The loop: define → generate → execute → return → respond
- •Good schema design with clear descriptions improves call accuracy
- •Use enums and required fields to constrain model behavior
Progress is kept in this browser only.