What is Function Calling?
Function Calling (also called Tool Use) allows language models to request execution of predefined functions with structured arguments. Instead of generating text responses, the model outputs structured function calls that your code can execute.
How It Works
- Define Functions: Describe available tools with names, descriptions, and parameter schemas
- User Query: Send user message with function definitions
- Model Response: Model returns function name + arguments (not the result!)
- Execute: Your code runs the function
- Return Result: Send result back to model for final response
Example Flow
User: "What's the weather in Tokyo?"
β
Model: {"function": "get_weather", "args": {"location": "Tokyo"}}
β
Your Code: calls real weather API β "25Β°C, sunny"
β
Model: "The weather in Tokyo is 25Β°C and sunny."
Defining Functions
functions = [{
"name": "get_weather",
"description": "Get current weather for a location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}]
Common Use Cases
| Use Case | Example Functions |
|---|---|
| Data Retrieval | search_database, get_user_info |
| Actions | send_email, create_ticket |
| Calculations | calculate_price, convert_currency |
| External APIs | get_weather, search_web |
Provider Comparison
| Provider | Feature Name | Multiple Calls |
|---|---|---|
| OpenAI | Function Calling / Tools | Yes (parallel) |
| Anthropic | Tool Use | Yes |
| Function Calling | Yes | |
| Open Source | Varies by model | Depends |
Best Practices
- Clear descriptions: Help model understand when to use each function
- Validate arguments: Never trust model output blindly
- Handle errors: Return useful error messages to model
- Limit scope: Only expose necessary functions
- Test edge cases: Model may call wrong function
Parallel Function Calling
Modern APIs support multiple simultaneous calls:
User: "What's the weather in Tokyo and Paris?"
Model: [
{"function": "get_weather", "args": {"location": "Tokyo"}},
{"function": "get_weather", "args": {"location": "Paris"}}
]
Related Concepts
- Agents - Autonomous tool-using systems
- Structured Output - Reliable data generation
- RAG - Knowledge retrieval