Back to all articles
5 MIN READ

Function Calling: How AI Uses External Tools

By Learnia Team

Function Calling: How AI Uses External Tools

This article is written in English. Our training modules are available in French.

Ask ChatGPT "What's the weather in Paris?" and it can actually check—not just guess from training data. How? Function calling lets AI models use external tools to take real actions.


What Is Function Calling?

Function calling (also called "tool use") is the ability for an LLM to request the execution of external functions based on user queries.

Traditional LLM Limitation

User: "What's the Bitcoin price right now?"

LLM (without tools): 
"As of my knowledge cutoff, Bitcoin was around $X..."
→ Outdated, possibly wrong

With Function Calling

User: "What's the Bitcoin price right now?"

LLM thought: "I need real-time data. I'll call get_crypto_price()"

Function call: get_crypto_price(symbol="BTC")
→ Returns: $94,521.32

LLM response: "Bitcoin is currently trading at $94,521.32"
→ Accurate, real-time

Why Function Calling Matters

1. Real-Time Information

LLMs have a knowledge cutoff date. Tools provide live data:

Weather, stock prices, sports scores, news
→ All require real-time access

2. Accurate Calculations

LLMs approximate math. Tools calculate precisely:

LLM: "15% of $3,847 is about $577"
Calculator tool: "15% of $3,847 is exactly $577.05"

3. Real-World Actions

LLMs generate text. Tools take action:

- Send an email
- Create a calendar event
- Update a database
- Make an API call
- Run code

4. Private Data Access

LLMs don't know your data. Tools can query it:

"What are my top customers this month?"
→ Tool queries your CRM
→ Returns actual customer data

How Function Calling Works

Step 1: Define Available Tools

You tell the AI what functions exist:

{
  "name": "get_weather",
  "description": "Get current weather for a location",
  "parameters": {
    "location": "string (city name)",
    "units": "celsius or fahrenheit"
  }
}

Step 2: AI Decides to Use Tool

Based on the query, the AI chooses:

User: "Should I bring an umbrella to London today?"

AI reasoning:
- User needs weather info for London
- I should call get_weather

Step 3: AI Generates Function Call

The AI outputs a structured request:

{
  "function": "get_weather",
  "arguments": {
    "location": "London",
    "units": "celsius"
  }
}

Step 4: Your System Executes

Your application runs the actual function:

result = get_weather("London", "celsius")
# Returns: {"temp": 12, "rain_chance": 85}

Step 5: AI Interprets Result

The result is sent back to the AI:

Weather data: 12°C, 85% chance of rain

AI response: "Yes, you should definitely bring an umbrella! 
London is expecting rain today with an 85% chance of 
precipitation. The temperature is a cool 12°C."

Common Tool Types

Information Retrieval

- Web search
- Database queries  
- API lookups
- Document retrieval

Calculations

- Math operations
- Unit conversions
- Date/time calculations
- Financial formulas

Actions

- Send messages (email, Slack, SMS)
- Create records (CRM, database)
- Schedule events (calendar)
- Trigger workflows

Code Execution

- Run Python/JavaScript
- Execute SQL queries
- Perform data analysis
- Generate visualizations

Real-World Examples

Travel Assistant

User: "Book me a flight to Tokyo next month"

Tools available:
- search_flights(origin, destination, dates)
- check_calendar(dates)
- get_user_preferences()
- create_booking(flight_id)

AI workflow:
1. Check calendar for availability
2. Get user preferences (airlines, seat type)
3. Search flights matching criteria
4. Present options to user
5. Create booking when confirmed

Data Analyst

User: "What were our sales trends last quarter?"

Tools available:
- query_database(sql)
- create_chart(data, chart_type)
- calculate_statistics(data)

AI workflow:
1. Query database for sales data
2. Calculate statistics (growth, averages)
3. Create visualization
4. Present insights with chart

Customer Support

User: "I want to return order #12345"

Tools available:
- lookup_order(order_id)
- check_return_eligibility(order_id)
- create_return_label(order_id)
- update_ticket(status)

AI workflow:
1. Look up order details
2. Check if return is eligible
3. Generate return label
4. Update support ticket

Function Calling vs RAG

| Aspect | RAG | Function Calling | |--------|-----|------------------| | Purpose | Retrieve knowledge | Take action | | Data | Static documents | Dynamic APIs | | Output | Text for context | Structured results | | Actions | Read-only | Can modify state |

They're often used together:

  • RAG provides knowledge context
  • Functions enable actions

Safety Considerations

The Risk

AI has access to: delete_database()

User: "Clean up old records"

Potential disaster if AI misinterprets

Mitigation Strategies

1. Confirmation for destructive actions
   "Are you sure you want to delete 500 records?"

2. Permission scoping
   AI can read orders, but not delete them

3. Rate limiting
   Max 10 API calls per conversation

4. Human-in-the-loop for sensitive actions
   Actions above $1000 require approval

Key Takeaways

  1. Function calling lets AI use external tools
  2. Enables real-time data, accurate calculations, and real actions
  3. Works through define → decide → call → execute → interpret
  4. Powers advanced use cases: assistants, agents, automation
  5. Requires careful safety design for sensitive actions

Ready to Build AI Agents?

This article covered the what and why of function calling. But building reliable AI agents requires understanding the complete agent architecture.

In our Module 6 — AI Agents & ReAct, you'll learn:

  • Complete ReAct agent architecture
  • Designing and implementing tool sets
  • Error handling and recovery patterns
  • Safety guardrails and human oversight
  • Production agent deployment

Explore Module 6: AI Agents & ReAct

GO DEEPER

Module 6 — AI Agents & ReAct

Create autonomous agents that reason and take actions.