Structured Outputs
20 minLesson 8 of 14
Function Calling
Enable LLMs to interact with external systems in a controlled, production-ready way
Learning goals
- •Understand function calling and tool use
- •Learn to define function schemas
- •Implement safe and effective function execution
What Is Function Calling?
Function calling allows LLMs to:
- Recognize when a function should be called
- Extract the correct parameters from natural language
- Return a structured function call for your code to execute
The model doesn't execute functions—it tells your code what to call.
Defining Functions
Functions are defined using JSON Schema:
const functions = [{
name: "get_weather",
description: "Get the current weather for a location",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and state, e.g., 'San Francisco, CA'"
},
unit: {
type: "string",
enum: ["celsius", "fahrenheit"],
description: "Temperature unit"
}
},
required: ["location"]
}
}];Clear descriptions help the model choose correctly.
The Function Calling Loop
The typical pattern:
- User sends a message
- Model decides to call a function
- Your code executes the function
- Send the result back to the model
- Model generates a final response
User: "What's the weather in Tokyo?"
↓
Model: {function: "get_weather", arguments: {location: "Tokyo, Japan"}}
↓
Your code: fetch weather API → {temp: 22, condition: "sunny"}
↓
Model: "It's currently 22°C and sunny in Tokyo."Common mistakes
×Poor function descriptions—the model relies on descriptions to choose functions
×Missing parameter descriptions—leads to incorrect parameter extraction
×Not handling function errors—always have fallback behavior for failed calls
×Trusting function calls blindly—validate parameters before executing
Key takeaways
+Function calling bridges natural language and structured API calls
+Clear function and parameter descriptions are critical for accuracy
+Always validate parameters before executing functions
+The model suggests calls—your code executes them safely