Overview
Function calling (also called tool calling) allows LLMs to interact with external functions and APIs. LiteLLM standardizes function calling across 100+ providers using the OpenAI format.Basic Usage
Function Calling Parameters
List[Dict]
List of tool definitions. Each tool must have:
type: Always"function"function: Object withname,description, andparameters
Union[str, Dict]
Controls which tools the model can call:
"auto"(default): Model decides whether to call a function"none": Model will not call any functions{"type": "function", "function": {"name": "function_name"}}: Force specific function"required": Model must call at least one function
bool
Whether to allow multiple function calls in a single response. Default:
TrueTool Definition Format
Response Format
When a function is called, the response contains tool calls:Examples
Single Function Call
Complete Conversation Flow
Multiple Functions
Forcing Function Call
Parallel Function Calls
Function Calling with Streaming
Provider-Specific Examples
Advanced Usage
Function with Complex Parameters
Error Handling
Function Calling with Pydantic
Best Practices
- Clear descriptions: Write detailed function and parameter descriptions
- Use enums: Constrain parameter values when possible
- Handle errors: Always validate function arguments before execution
- Complete the loop: Send function results back to the model
- Consider parallel calls: Enable for independent operations
- Set tool_choice wisely: Use “auto” for flexibility, force calls when needed
Troubleshooting
Function Not Called
- Ensure the function description clearly states when to use it
- Try setting
tool_choice="required"to force a function call - Check if the model supports function calling
Invalid Arguments
- Validate the JSON schema in your tool definition
- Add detailed descriptions for each parameter
- Use
json.loads()with error handling
Multiple Unwanted Calls
- Set
parallel_tool_calls=Falseto limit to one call - Be specific in function descriptions about when to use each tool