# Function calling

> How an AI model asks your code to run a tool and then uses the result.

- Level: Intermediate
- Topics: AI, Software
- URL: https://tidbit.legios.com.ar/function-calling/
- In Spanish: [Function calling](https://tidbit.legios.com.ar/es/function-calling/)

**Function calling**, also called **tool use**, lets an LLM do more than write text. You describe a few tools to the model: a name, what each one does and which arguments it takes, usually as a JSON schema. When the model decides a tool would help, instead of answering it replies with a structured request such as `get_weather(city="Rosario")`.

The model never runs anything itself. Your program receives that request, runs the real function (calls a weather API, queries a database, sends an email) and passes the result back. The model reads it and continues: it may call another tool or write the final answer. That loop, repeated, is what turns a chatbot into an AI agent.

OpenAI added function calling to its API in 2023 and the other big providers followed; Anthropic's docs call it tool use. The Model Context Protocol standardizes how tools are packaged so many apps can plug in the same ones. Because the model chooses which tool to call and with what arguments, tools that change things (paying, deleting, sending) deserve a confirmation step: a model can pick the wrong one, and text it reads along the way can try to trick it.

## An analogy

A manager who cannot leave the office but can fill out request slips. They write "check the warehouse for item 42", an assistant goes and does it, and comes back with the answer for the manager to use.

## Keep in mind

- The model outputs a structured request; your code runs the tool.
- Each tool is described by a name, a description and an argument schema.
- Call, run, return, repeat: that loop is the core of an AI agent.
- Ask for confirmation before a model triggers actions that are hard to undo.

## Go deeper

- [Tool use with Claude (Anthropic docs)](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/overview)
- [Function calling (OpenAI docs)](https://platform.openai.com/docs/guides/function-calling)

## Related terms

- [LLM](https://tidbit.legios.com.ar/llm/): A neural network trained on huge amounts of text to predict the next word.
- [JSON](https://tidbit.legios.com.ar/json/): The plain-text format programs use to hand each other structured data.
- [Database schema](https://tidbit.legios.com.ar/database-schema/): The blueprint of a database: which tables exist, their columns and how they connect.
- [API](https://tidbit.legios.com.ar/api/): The menu of things one program lets another program ask for.
- [Database](https://tidbit.legios.com.ar/database/): An organized store of data that programs can search and update safely.
- [Chatbot](https://tidbit.legios.com.ar/chatbot/): A program you talk to by typing or speaking, as if it were a person.
- [AI agent](https://tidbit.legios.com.ar/ai-agent/): An LLM that can take actions, check the results and keep going until the job is done.
- [OpenAI](https://tidbit.legios.com.ar/openai/): The company that made ChatGPT and turned AI into a household topic.
