OwlTheEngineer
AI Explained VisuallyEpisode 53 · AI

How AI function calling actually works: connecting LLMs to the real world

Why LLMs are pure text predictors that cannot run code directly, and how JSON schema tool calling allows models to query live databases and APIs.

Owl · 1 min read · 1 views
– comments
Ep 53 · AI
60s VISUAL BREAKDOWN
Episode 53
Watch the fast visual breakdown or discuss on social:

A common misconception about Large Language Models (LLMs) like GPT-4 or Claude is that they can directly browse the live internet, query SQL databases, or execute payment transactions.

In reality, LLMs are pure next-token predictors. They have zero access to external networks, file systems, or runtime execution environments.

Function Calling (Tool Calling) is the architectural mechanism that bridges this gap.


1. The Core Limitation: Models Predict Text, Not Code

When a user asks: “What is the current temperature in Tokyo and is it raining?”, the model cannot ping a weather satellite. Its training weights are frozen in the past.

[ User Prompt ] ──> [ Raw LLM ] ──> ❌ Hallucinates outdated guess
                                    ❌ Cannot execute GET api.weather.com

To give the model real-time capabilities, we do not let the model execute code—we turn the model into a structured parameter extractor.


2. The 4-Step Function Calling Lifecycle

[ User Prompt + JSON Tool Schema ]
                 │
                 ▼
          [ LLM Predictor ]
                 │
                 ▼ (Outputs JSON Tool Call)
       { name: "get_weather", args: { "city": "Tokyo" } }
                 │
                 ▼
        [ Your Backend App ] ──> (Executes Live SQL / API Call)
                 │
                 ▼ (Returns { temp: "22°C", rain: false })
      [ Injects Result to LLM ]
                 │
                 ▼
       [ "It is 22°C and sunny in Tokyo!" ]

Step 1: Providing the Tool Schema

Your application defines available tools using standard JSON Schema:

{
  "type": "function",
  "function": {
    "name": "get_weather",
    "description": "Fetch real-time weather for a given city",
    "parameters": {
      "type": "object",
      "properties": {
        "city": { "type": "string", "description": "City name" },
        "unit": { "type": "string", "enum": ["celsius", "fahrenheit"] }
      },
      "required": ["city"]
    }
  }
}

Step 2: The Model Generates a Tool Call

Instead of answering with conversational English, the model detects that get_weather satisfies the user’s intent. It generates a structured JSON payload:

{
  "tool_calls": [
    {
      "id": "call_982k",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"city\": \"Tokyo\", \"unit\": \"celsius\"}"
      }
    }
  ]
}

Step 3: Your Application Executes the Code

Your backend intercepts the tool call, parses the JSON arguments, and runs the real function in Python, Node.js, or Go:

const weather = await fetch(`https://api.weather.com?city=Tokyo&unit=celsius`);

Step 4: Formatting the Final Answer

The raw output is appended to the message history and sent back to the LLM, which converts the technical data into a friendly, natural language response for the user.


The Golden Rule of Function Calling

  • The LLM NEVER runs the function: It never opens a socket, runs a shell script, or executes SQL.
  • The LLM only decides: It selects which tool to call and accurately parses human input into typed JSON parameters.
  • Your backend retains 100% control: You validate permissions, sanitize database queries, and control execution safety.
SYSTEM DESIGN CHECK

Test Your Understanding

Quick architectural scenario challenge based on this breakdown.

SCENARIO

A 3rd-party payment gateway starts timing out with 504 errors during peak traffic.

Q1.How should a downstream dependency failure be handled to protect upstream service availability?

Comments –