LLM APIs & Integration / API Features

Function Calling

Intermediate [3/5]
Tool use Tool calling Function invocation

Definition

Function calling allows LLMs to output structured requests to execute external functions. Instead of generating text, the model outputs a function name and arguments in a structured format that your code can execute.

This enables LLMs to interact with external systems, APIs, databases, and tools in a controlled, reliable way.

Key Concepts

  • Function schema: JSON definition of available functions
  • Structured output: Model outputs function calls, not prose
  • Execution loop: Model requests → you execute → return results
  • Tool choice: Control when model can/must use functions

Examples

Flow
Function Calling Pipeline
User: "What's the weather in Tokyo?" Step 1: Define functions for the model functions = [{ "name": "get_weather", "description": "Get current weather for a city", "parameters": { "type": "object", "properties": { "city": {"type": "string"}, "units": {"type": "string", "enum": ["celsius", "fahrenheit"]} }, "required": ["city"] } }] Step 2: Model outputs function call (not text!) { "function_call": { "name": "get_weather", "arguments": "{\"city\": \"Tokyo\", \"units\": \"celsius\"}" } } Step 3: Your code executes the function result = get_weather("Tokyo", "celsius") # Returns: {"temp": 22, "condition": "cloudy"} Step 4: Send result back to model Model generates: "The weather in Tokyo is 22°C and cloudy."
Implementation
OpenAI Function Calling
from openai import OpenAI import json client = OpenAI() # Define tools tools = [{ "type": "function", "function": { "name": "search_products", "description": "Search for products in the catalog", "parameters": { "type": "object", "properties": { "query": {"type": "string"}, "max_price": {"type": "number"}, "category": {"type": "string"} }, "required": ["query"] } } }] # Call with tools response = client.chat.completions.create( model="gpt-4", messages=[{"role": "user", "content": "Find laptops under $1000"}], tools=tools, tool_choice="auto" # or "none" or {"type": "function", "function": {"name": "..."}} ) # Handle function call if response.choices[0].message.tool_calls: call = response.choices[0].message.tool_calls[0] args = json.loads(call.function.arguments) # Execute: search_products(**args)

Interactive Exercise

Design a Function Schema

Design a function schema for a "send_email" function that an LLM could call:

Pro Tips
  • Write clear descriptions—the model uses them to decide when to call
  • Use enums for constrained parameters (status, category, etc.)
  • Always validate arguments before execution
  • Consider parallel function calls for efficiency

Related Terms