Function Calling is a Pro feature. Upgrade to access.
Basic Usage
Define tools and let the model decide when to call them:Python
Tool Definition Schema
Handling Tool Responses
After receiving a tool call, execute the function and send results back:Python
Parallel Function Calls
Models can request multiple function calls simultaneously:Python
Controlling Tool Usage
Usetool_choice to control function calling behavior:
| Value | Behavior |
|---|---|
"auto" | Model decides whether to call functions (default) |
"none" | Never call functions |
"required" | Must call at least one function |
{"type": "function", "function": {"name": "..."}} | Force specific function |
Python
Best Practices
- Clear Descriptions: Write detailed function and parameter descriptions
- Validate Arguments: Always validate parsed arguments before execution
- Error Handling: Return clear error messages when functions fail
- Limit Scope: Define only necessary functions to reduce token usage
- Idempotency: Make functions safe to retry on failures
Build AI Agents
Learn how to combine function calling with conversation history for powerful agents.