Function Calling
LLM capability to generate structured calls to external functions based on natural language, enabling integration with APIs, databases, and real-world tools.
What it is
Function calling is the capability of an LLM to decide when to invoke an external function and generate the necessary arguments in structured format (typically JSON). The model doesn't execute the function — it generates the call specification that the host system executes.
This capability transforms LLMs from text generators into action orchestrators.
How it works
- Definition: the model is provided a schema of available functions (name, description, parameters)
- Decision: given a query, the model decides whether to call a function or respond directly
- Generation: if it decides to call, it generates JSON with the function name and arguments
- Execution: the host system executes the actual function
- Continuation: the result is returned to the model to formulate the final response
Example flow
User: "What's the weather in Madrid?"
Model generates:
{
"function": "get_weather",
"arguments": { "city": "Madrid", "units": "celsius" }
}
System executes get_weather("Madrid", "celsius") → "18°C, partly cloudy"
Model responds: "In Madrid it's 18°C with partly cloudy skies."
Relationship with MCP
The Model Context Protocol standardizes how models discover and call tools. Function calling is the underlying mechanism; MCP is the protocol that makes it interoperable across different systems.
Usage patterns
- Simple tools: calculator, unit conversion, date/time
- External APIs: weather, search, databases
- System actions: create files, send emails, execute code
- Parallel calls: multiple functions in a single response
- Chained calls: one function's result feeds the next
Considerations
- Validation: always validate generated arguments before execution
- Security: limit which functions are available based on context
- Error handling: the model should be able to interpret and recover from errors
- Clear descriptions: function description quality directly affects accuracy
Why it matters
Function calling is the mechanism that turns LLMs from text generators into agents that interact with the real world. Without it, models can only respond with text. With it, they can query databases, call APIs, and execute concrete actions.
References
- Function Calling - OpenAI — Official OpenAI documentation.
- Tool Use - Anthropic — Tool use guide with Claude.
- Function Calling — Gemini — Google, 2024. Function calling implementation in Gemini.