Lesson 2

Function Calling

Giving LLMs abilities

The Problem: AI can talk, but it can't do things in the real world. It can tell you about the weather forecast formula, but it can't actually check today's weather for your city.

The Solution: Teach AI to Use Tools

Function calling is like teaching AI to use a phone. Instead of just talking, it can now "call" different services to get things done. The AI translates your request into a structured output that specifies which tool to invoke -- and that is what powers the ReAct pattern.

Think of it like a waiter in a restaurant:

  • 1. : You say: "I'd like a margherita pizza"
  • 2. : The waiter understands and writes: order_pizza(type="margherita")
  • 3. : The kitchen (tool) receives the order and makes the pizza
  • 4. : The waiter brings you the result

The AI "translates" your natural language into a structured function call!

Where Is This Used?

  • Smart Assistants: "Set an alarm for 7am"set_alarm(time="07:00")
  • Customer Service: "Cancel my order #12345"cancel_order(id=12345)
  • Data Analysis: "Show me sales for Q4"get_sales(quarter=4)
  • Home Automation: "Turn off the lights"control_lights(action="off")

Fun Fact: Function calling was a game-changer for AI! Before it, chatbots could only chat. Now they can actually do things — book flights, send emails, control smart homes. It's what makes AI truly useful as an assistant.

Try It Yourself!

Use the interactive demo below to see how AI converts your request into a function call. Watch the magic of translation happen in real time!

🔧Function Calling: How AI Uses Tools

🤖 By itself, AI can only generate text. But if you give it descriptions of tools (functions), it can decide when to call them and with what parameters.

Available tools (click any):
👆

Select a tool above to see an example

Key Insight

Function Calling is like giving AI a phone number for a service. AI doesn't do the work itself, it "calls" the right function and uses its response. This lets you connect AI to databases, APIs, file systems — anything!

📝How to write Function Calling prompts
System Prompt Example:
You are an assistant with access to tools.
When the user asks for something that requires a tool:
1. Identify which tool is needed
2. Call it with correct parameters
3. Use the result to answer

Available tools are provided in the tools parameter.

This prompt explains to the model that it has access to tools and how to use them.

Tool Definition Example (JSON Schema):
{
  "name": "get_weather",
  "description": "Get current weather in a city",
  "parameters": {
    "type": "object",
    "properties": {
      "city": {
        "type": "string",
        "description": "City name"
      },
      "units": {
        "type": "string",
        "enum": ["celsius", "fahrenheit"]
      }
    },
    "required": ["city"]
  }
}

JSON Schema describes the function parameters: which fields are required, what data types are accepted.

Prompt Writing Tips:
  • Use clear descriptions: AI needs to understand when to use each tool
  • Specify required parameters via "required" in JSON Schema
  • Add example values in description for better understanding
  • Test your prompts: verify that AI correctly selects and calls functions

Try it yourself

Interactive demo of this technique

Technique Comparison
Demo Mode
Pre-recorded responses
TaskBeginnerCoding

What is the current weather in Paris?

Without technique
Without technique
Prompt
What is the current weather in Paris?
Response

Paris usually has a mild climate. Right now it is probably around 18°C and cloudy.

Tokens:18/25
Time:340ms
Quality:
With Tool Use
With technique
Prompt
Use available tools to answer. Tools: - get_weather(city: string): returns current weather Question: What is the current weather in Paris?
Response

Calling a tool to get current data:

→ get_weather("Paris") ← { "temp": 12, "condition": "rain", "humidity": 85, "wind": "15 km/h" }

Currently in Paris it is 12°C, rainy, humidity 85%, wind 15 km/h. I recommend taking an umbrella!

🧠Model determined it needs real data, not guesses
👁️Calling get_weather("Paris") — accessing weather API
👁️Received exact data: 12°C, rain, humidity 85%
Answer formed from real data, not guesses
Tokens:58/82
Time:1400ms
Quality:
Why this works

Without tools the model is forced to "guess" the weather using general knowledge. Function Calling enables getting accurate real-time data.

1 / 2

Lesson Quiz

1 of 3

1.What is the primary purpose of function definitions in LLM function calling?

Practice Challenges

Create a free account to solve challenges

3 AI-verified challenges for this lesson

Related lessons:ReactTool Impact

This lesson is part of a structured LLM course.

My Learning Path