Skip to main content

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 with name, description, and parameters
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: True

Tool 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

  1. Clear descriptions: Write detailed function and parameter descriptions
  2. Use enums: Constrain parameter values when possible
  3. Handle errors: Always validate function arguments before execution
  4. Complete the loop: Send function results back to the model
  5. Consider parallel calls: Enable for independent operations
  6. 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=False to limit to one call
  • Be specific in function descriptions about when to use each tool