Introduction to Function Calling Beginner

Function calling (also called tool use) is the ability for AI models to request the execution of external functions during a conversation. Instead of just generating text, the model can say "I need to call this function with these arguments" — and your application executes it.

Why Function Calling Matters

Without function calling, AI models are limited to their training data. They cannot check current weather, query databases, send emails, or interact with any external system. Function calling bridges this gap.

🌐

Real-Time Data

Access live information: weather, stock prices, database records, API responses — anything your functions can retrieve.

Take Actions

Go beyond answering questions: send emails, create tickets, update records, deploy code, and control external systems.

🔧

Structured Output

Function arguments are always structured JSON, giving you reliable, parseable output from the model.

🚀

AI Agents

Function calling is the foundation for AI agents that can plan, execute multi-step tasks, and interact with the world.

The Function Calling Lifecycle

Lifecycle
# Step 1: You define available tools
tools = [
    {"name": "get_weather", "parameters": {"location": "string"}},
    {"name": "search_docs", "parameters": {"query": "string"}}
]

# Step 2: User asks a question
User: "What's the weather in Tokyo?"

# Step 3: Model decides to call a function
Model: tool_call(name="get_weather", arguments={"location": "Tokyo"})

# Step 4: Your code executes the function
result = get_weather("Tokyo")  → {"temp": "22C", "condition": "Sunny"}

# Step 5: You send the result back to the model
tool_result: {"temp": "22C", "condition": "Sunny"}

# Step 6: Model generates a natural language response
Model: "The weather in Tokyo is currently 22°C and sunny!"
Key Point: The model never executes functions directly. It only requests that your application execute them. You always maintain control over what actually runs, which is critical for security.

Function Calling vs. Prompt Engineering

Approach Reliability Structure Use Case
Prompt: "Return JSON with..." Moderate Often malformed Simple extractions
Function calling High Always valid JSON Actions, structured data

Terminology Across Providers

Concept OpenAI Anthropic Google
Feature name Function calling Tool use Function calling
Tool definition tools[].function tools[] tools[].function_declarations
Model requests call tool_calls tool_use content block function_call part
You return result tool message tool_result content block function_response part
Next Steps: In the following lessons, we will implement function calling with each provider, starting with OpenAI's approach, then Anthropic's, and finally Google's.