Function Calling / Tool Calling
A technical protocol and standard for LLM interaction with external software. Instead of free text, the model returns valid JSON containing the function name and typed arguments according to JSON Schema, enabling the backend to deterministically execute actions in real APIs.
1. Concept Overview & Systemic Problem
The language model is confined within its own context: it does not know the current exact time, cannot withdraw funds from an account, or check the status of a container in Docker.
Without a mechanism for interaction with code, a chatbot could only simulate actions, returning vague text: “I checked your balance, it’s $50” (even if the balance is $0).
Function Calling (Tool Calling) transforms the text generator into a systems orchestrator. The model analyzes the prompt, realizes it lacks data, and instead of text, generates machine-readable JSON:
{
"name": "fetch_user_balance",
"arguments": "{\"account_id\": \"ACC-9481\"}"
}
Mental model: if the LLM is the brain, then Function Calling is the nerve impulses sending commands to the muscles (your code and API) to perform physical actions.
┌─────────────────────────────────────────────────────────────┐
│ FUNCTION CALLING ARCHITECTURE │
├─────────────────────────────────────────────────────────────┤
│ 1. USER REQUEST: │
│ “What’s the weather in Lviv?” │
├─────────────────────────────────────────────────────────────┤
│ │ │
│ ▼ Request to LLM (with schema) │
├─────────────────────────────────────────────────────────────┤
│ 2. LLM RESPONSE (finish_reason: "tool_calls"): │
│ { "name": "get_weather", "args": { "city": "Lviv" } } │
├─────────────────────────────────────────────────────────────┤
│ │ │
│ ▼ Backend executes real API call │
├─────────────────────────────────────────────────────────────┤
│ 3. YOUR SERVER ➔ OpenWeatherMap ➔ Receives { temp: 18, rain: 0} │
├─────────────────────────────────────────────────────────────┤
│ │ │
│ ▼ Sends result back to LLM │
├─────────────────────────────────────────────────────────────┤
│ 4. FINAL RESPONSE TO USER: │
│ “It’s currently +18°C in Lviv, no precipitation, have a great day!” │
└─────────────────────────────────────────────────────────────┘
2. Practical Example: Schema Description and cURL Invocation
Here’s how to register a tool in the OpenAI / Anthropic Tool Calling standard:
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Block user card 5501"}
],
"tools": [
{
"type": "function",
"function": {
"name": "block_payment_card",
"description": "Securely block a user’s bank card in case of loss",
"parameters": {
"type": "object",
"properties": {
"card_id": {
"type": "integer",
"description": "Unique identifier of the card in the system"
},
"reason": {
"type": "string",
"enum": ["lost", "stolen", "fraud"],
"description": "Reason for blocking"
}
},
"required": ["card_id", "reason"]
}
}
}
],
"tool_choice": "auto"
}'
What the model returns:
{
"role": "assistant",
"content": null,
"tool_calls": [
{
"id": "call_abc123",
"type": "function",
"function": {
"name": "block_payment_card",
"arguments": "{\"card_id\": 5501, \"reason\": \"lost\"}"
}
}
]
}
3. Four Security Rules for Working with Function Calling
- Strict Argument Validation (Pydantic / Zod): Never blindly trust arguments from the JSON model. Always parse them through a validation schema before executing SQL queries or withdrawing funds.
- Control Gateway for Destructive Actions (Human-in-the-Loop): If a function performs an irreversible operation (
delete_database,transfer_funds), the backend must halt execution and request confirmation from a human. - Context Limitation for Tools: Do not pass hundreds of functions to the model simultaneously. This dilutes attention (Attention Saturation) and increases the likelihood of erroneous calls. Use dynamic filtering or MCP.
- Protection Against Infinite Loops: Always set a hard limit on the number of consecutive function calls (e.g., no more than 5 iterations per request).
4. Production Engineering Scenarios
Function Calling is a bridge connecting the world of statistical language modeling with the deterministic realm of programming. This mechanism is at the core of all modern AI agents, MCP servers, and autonomous developer assistants.
FAQ: Function Calling / Tool Calling
Related terms
Tool Schemas (Tools & JSON Schema)
A standardized formal description of tool interfaces for large language models using the JSON Schema standard. It includes the function name, a detailed textual description of its purpose, a list of required parameters, and their value types.
AI Agents (Autonomous Agents)
An autonomous system based on a large language model that not only responds to messages but independently plans a sequence of actions, utilizes external tools (browser, terminal, databases), and executes complex tasks without constant human oversight.
Agentic Loop: Steps of Thought ➔ Action ➔ Observation (ReAct)
A fundamental algorithmic pattern for autonomous agents, known as ReAct: Reasoning + Acting, consisting of an infinite cycle of three steps: 1) Thought — situation analysis; 2) Action — tool invocation; 3) Observation — result analysis and plan adjustment.
MCP (Model Context Protocol)
An open standard from Anthropic based on JSON-RPC 2.0 for unified bidirectional connection of AI assistants to external tools, databases, and system environments.