Why AI needs tools
AI models are pattern matchers, not calculators. Ask "What is 15 x 7?" and the AI might guess correctly, or might not. But with function calling, the AI can decide to call a real multiply(15, 7) function and get a guaranteed correct answer.
The tool use loop
The LLM decides when to call a tool, your code executes it, and the result feeds back into the conversation.
Exactly! ChatGPT plugins, custom GPTs, Claude's tool use, and AI agents all use function calling under the hood. You're learning the foundation that powers all of these.
The 5-step function calling workflow
The AI decides which function to call, your code executes it, and the result goes back to the AI.
def add(a: float, b: float) -> dict:
"""Add two numbers."""
result = a + b
return {"operation": "addition", "result": result}
def multiply(a: float, b: float) -> dict:
"""Multiply two numbers."""
result = a * b
return {"operation": "multiplication", "result": result}
def divide(a: float, b: float) -> dict:
"""Divide two numbers."""
if b == 0:
return {"error": "Division by zero"}
result = a / b
return {"operation": "division", "result": result}Simple Python functions that the AI will learn to call. Note the error handling in divide(), you should always handle edge cases.
To tell the AI about your functions, you describe them using JSON Schema, a standard format for describing the shape of JSON data. Each tool schema has a name (matching your Python function), a description (so the AI knows when to use it), and parameters (with types and required fields).
Fill in the blanks: Complete the tool definition
Loading practice…
# Tell the AI about your tools using JSON schemas
calculator_tools = [
{
"type": "function",
"function": {
"name": "multiply",
"description": "Multiply two numbers",
"parameters": {
"type": "object",
"properties": {
"a": {"type": "number", "description": "First number"},
"b": {"type": "number", "description": "Second number"}
},
"required": ["a", "b"]
}
}
},
# ... similar schemas for add, divide
]Tool schemas tell the AI what functions exist, what they do, and what parameters they accept. The "description" field matters most because it helps the AI decide WHEN to use each tool.
Flashcards: Flashcards
Loading practice…
Quiz: Quiz
Loading practice…
You now understand why AI needs tools and how the 5-step function calling workflow operates. Next, we will build the agentic loop, the core pattern that lets AI agents call tools repeatedly until the task is complete.