Skip to main content

Function Calling / Tool Calling

FOR AI AGENTChatGPTClaude

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

  1. 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.
  2. 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.
  3. 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.
  4. 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.

/ Frequently Asked QuestionsSchema.org FAQPage

FAQ: Function Calling / Tool Calling

Categorically no! The model is an isolated text-mathematical processor. It has no direct access to your database or network. It only generates tokens in JSON format requesting your server: 'Execute get_user_balance(user_id=42)'. The actual code is always executed by your host backend.
/ Internal links
All terms

Related Ecosystem Resources

Explore practical workflows, core terminology, and engineering tools

Glossary Concept

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.

Glossary Concept

Tool Calling (Function Calling)

A low-level mechanism in language models that enables them to reliably generate validated parameters in JSON format for executing functions in external programming environments.

Practical Guide

Complete Guide to Higgsfield Cinema Studio 4.0: Scene Management, Soul 2.0, and API Automation

In-depth practical breakdown of the major Higgsfield update: Cinema Studio 4.0, consistent characters with Soul 2.0, API-based generation, and a ready-to-use UGC pipeline.

Practical Guide

Part II. AI Agent Architecture: From Model to Memory and Tools

A deep engineering dive: tokenization, KV-cache, sampling determinism, the training stack, quantization, context engineering, the safety stack, production RAG, ReAct, and multi-agent swarm systems.

News

Kimi K3.1 Pro: Moonshot Updates Model for 3D Tooling

Moonshot released Kimi K3.1 Pro with a focus on 3D tooling, updated post-training, and an architecture change aimed at better long-context efficiency.

News

Anthropic releases FlashProve in open source, a tool for provable inference

Anthropic published a prototype of an open cryptographic protocol called FlashProve that lets anyone verify a model response without access to model weights.