TopDev
ky-thuat Intermediate

What is Function Calling (Tool Use)?

The ability for an LLM to call external functions or APIs to fetch real data — not just generate text from memory.

Updated: May 5, 2026 · 2 min read

Function Calling (also called Tool Use) is the ability for an LLM to “call” external functions or APIs to fetch data or take actions in the real world — instead of just answering from its training knowledge.

The problem function calling solves

A pure-text LLM has three limitations:

  1. Doesn’t know recent data (after the training cutoff)
  2. Doesn’t know your private data (databases, files)
  3. Can’t TAKE ACTION (send email, place an order)

Function calling is the bridge between an LLM and real code.

How it works

User: "What's the weather in Hanoi today?"

LLM realizes: I need real data → call a function

LLM returns: { "function": "get_weather", "args": { "city": "Hanoi" } }

Your app runs the actual function → result: "32°C, light sun"

Feed the result back to the LLM

LLM responds naturally: "Hanoi is sunny with light clouds today, 32°C..."

Declaring a function (Claude example)

tools = [
  {
    "name": "get_weather",
    "description": "Get the current weather for a city",
    "input_schema": {
      "type": "object",
      "properties": {
        "city": { "type": "string", "description": "City name" }
      },
      "required": ["city"]
    }
  }
]

response = client.messages.create(
  model="claude-sonnet-4-7",
  tools=tools,
  messages=[{ "role": "user", "content": "Weather in Hanoi?" }]
)

The LLM decides on its own when to call a tool and when to answer directly.

Function calling vs MCP

  • Function calling: API call inside your code. Flexible, full control.
  • MCP: standardized way to connect tools across apps. Great when you want to share tool integrations.

→ MCP builds on the function calling idea, but using a shared protocol.

Common use cases

  • Real-time search: connect to Brave/Google Search
  • Code execution: run Python for accurate calculations
  • Database queries: SQL from natural-language questions
  • Email/Slack: agents that send messages
  • Calendar: schedule meetings

Things to watch out for when building

  • The LLM may call a tool incorrectly or with wrong arguments → validate on the server
  • Require user confirmation for actions with consequences (sending email, deleting files)
  • Cap the number of tool calls to avoid infinite loops
Tags
#function-calling#tool-use#agent