Pype raises $1.2M in pre-seed funding
Pype
Pype

How AI Actually Calls Functions: The Simple Truth Behind LLM Tool Calling

D
Deepesh Agrawal
15 Sep 2025
blogImage

From "What's 15 plus 27?" to actual code execution - explained simply


The Magic You See Every Day

You ask ChatGPT "What's 15 plus 27?" and somehow it doesn't just give you generic math information—it actually runs a calculation and gives you the exact answer: 42. How does text become code execution? Let's break it down step by step.

What is Tool Calling, Really?

Simple explanation: Tool calling is when an AI decides to run actual code functions instead of just generating text.

Think of it like this:

  • Without tool calling: AI writes an essay about addition
  • With tool calling: AI runs add_numbers(15, 27) and gives you the real result

Step 1: You Define What the AI Can Do

Let's use a simple calculator example. First, you write the actual function and tell the AI about it:

Write the Tool Function

def add_numbers(a, b):
    """Add two numbers together"""
    return a + b

Tell the AI About Your Function

tools = [
    {
        "name": "add_numbers",
        "description": "Add two numbers together", 
        "parameters": {
            "a": {"type": "number", "description": "First number"},
            "b": {"type": "number", "description": "Second number"}
        }
    }
]

What this means: You have a real Python function, and you describe it to the AI so it knows when and how to use it.

Step 2: What Happens Inside the AI's "Brain"

When you ask "What's 15 plus 27?", here's what happens inside the neural network:

Layer 1-6: Basic Understanding

AI thinks: "User mentioned numbers '15' and '27' with 'plus'"
AI recognizes: This is a math addition problem

Layer 7-12: Making Connections

AI thinks: "15 + 27 = need to add numbers"
AI notices: I have an 'add_numbers' tool that can help
AI connects: "15" goes in parameter "a", "27" goes in parameter "b"

Layer 13-18: Decision Making

AI decides: "I should use the add_numbers tool"
AI extracts: a = 15, b = 27
AI plans: "I'll call add_numbers(a=15, b=27)"

Layer 19-24: Code Generation

AI generates:
{
    "tool_calls": [{
        "name": "add_numbers", 
        "parameters": {"a": 15, "b": 27}
    }]
}

Key insight: The AI learned these patterns from training data—no one programmed specific rules like "if addition mentioned, call add_numbers function."

Step 3: Your Code Executes the Function

# The AI tells you to call this function:
tool_call = {
    "name": "add_numbers",
    "parameters": {"a": 15, "b": 27}
}

# Your code executes it:
result = add_numbers(15, 27)  # Returns 42

# You send the result back to the AI:
response = f"The result is {result}"

Step 4: AI Gives You the Final Answer

The AI gets the result (42) and responds naturally:

"15 plus 27 equals 42."

The Complete Flow

# Complete example
from openai import OpenAI

client = OpenAI()

def add_numbers(a, b):
    return a + b

tools = [
    {
        "type": "function",
        "function": {
            "name": "add_numbers",
            "description": "Add two numbers together",
            "parameters": {
                "type": "object",
                "properties": {
                    "a": {"type": "number", "description": "First number"},
                    "b": {"type": "number", "description": "Second number"}
                }
            }
        }
    }
]

# User asks: "What's 15 plus 27?"
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "What's 15 plus 27?"}],
    tools=tools
)

# AI decides to call the tool
tool_call = response.choices[0].message.tool_calls[0]
function_name = tool_call.function.name
args = eval(tool_call.function.arguments)

# Execute your function
result = add_numbers(args['a'], args['b'])

# Send result back to AI
final_response = client.chat.completions.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": "What's 15 plus 27?"},
        response.choices[0].message,
        {"role": "tool", "content": str(result), "tool_call_id": tool_call.id}
    ]
)

print(final_response.choices[0].message.content)
# Output: "15 plus 27 equals 42."

Why This Is Powerful

Before tool calling: AI could only talk about math With tool calling: AI can actually calculate, call APIs, run code, control systems

The Magic Truth

The AI doesn't have rules like "if addition → call add_numbers". Instead:

  1. Pattern Recognition: During training, it saw thousands of examples of math questions → function calls
  2. Neural Learning: The 24+ layers learned to connect human language with structured function calls
  3. Emergent Behavior: Tool calling "emerges" from the neural network's learning, not explicit programming

Bottom Line

When you ask "What's 15 plus 27?", the AI:

  1. Processes your words through 24 neural layers
  2. Decides to call add_numbers(15, 27)
  3. Your code runs the function
  4. AI uses the result to give you a natural answer

It's pattern matching and learning—not magic, but still pretty amazing.