
From "What's 15 plus 27?" to actual code execution - explained simply
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.
Simple explanation: Tool calling is when an AI decides to run actual code functions instead of just generating text.
Think of it like this:
add_numbers(15, 27) and gives you the real resultLet's use a simple calculator example. First, you write the actual function and tell the AI about it:
def add_numbers(a, b):
"""Add two numbers together"""
return a + b
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.
When you ask "What's 15 plus 27?", here's what happens inside the neural network:
AI thinks: "User mentioned numbers '15' and '27' with 'plus'"
AI recognizes: This is a math addition problem
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"
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)"
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."
# 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}"
The AI gets the result (42) and responds naturally:
"15 plus 27 equals 42."
# 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."
Before tool calling: AI could only talk about math With tool calling: AI can actually calculate, call APIs, run code, control systems
The AI doesn't have rules like "if addition → call add_numbers". Instead:
When you ask "What's 15 plus 27?", the AI:
add_numbers(15, 27)It's pattern matching and learning—not magic, but still pretty amazing.