Tool Use and AI Agents: How LLMs Call Functions and Act
By LatestLLM Editorial · Published · Facts verified · 8 min read
Also called: function calling, tool calling, AI agents, agentic loop, MCP, Model Context Protocol, ReAct
Before this: Next-token prediction, Context window
In one line
Tool use lets a language model request that an application run a function, such as search or a calculation, and agents repeat that loop until a task is finished.
Tool use and agents, simply and technically
In plain English
A language model can only produce text. It cannot check today's weather, open a file or multiply large numbers reliably on its own. Tool use gives it a way to ask. The model is told about a few tools, such as “calculator” or “search the web”, and when it needs one it writes a short, structured request instead of an answer. The software around the model carries out the request and hands back the result, and the model continues.
An agent is what you get when this happens repeatedly: the model plans a step, uses a tool, looks at what came back, decides the next step and carries on until the job is done. The model decides what to do; the surrounding software decides what is actually allowed to happen.
Think of it like this: Tool use is like a manager who can only write notes. She writes “please run this calculation and bring me the result” on a note, an assistant does it and returns the answer on another note, and she then writes her reply. She never touches the calculator, which means the assistant, not the manager, controls what actually happens. An agent is the same manager running through many notes to finish a project. The comparison fails on one point: a model can write a note asking for something harmful, so the assistant must be willing to say no.
Everyday example: A calculation the model should not do in its head
A user asks “What is 1,234 × 5,678?” and the model has a calculator tool.
Input:
What is 1,234 × 5,678?
Output:
Model asks calculator(“1234 * 5678”) → 7,006,652 → “1,234 × 5,678 = 7,006,652.”
What it shows: The model does not guess. It writes a tool request for the calculator with the expression, the application runs the calculator and returns 7,006,652, and the model replies with that number. The same pattern covers searching, reading files, calling a company's database or sending a message.
Technically
In the Anthropic Messages API format, a request lists tools, each with a name, a description and a JSON Schema input_schema. When the model wants a tool it returns stop_reason "tool_use" and a tool_use content block with an id, the tool name and the arguments as JSON. The application runs the function, then calls the API again with the full conversation plus a user message holding a tool_result block that carries the same tool_use_id. The model then continues, and the loop ends when it answers with stop_reason "end_turn". Other providers use similar structures under names such as function calling.
Models learn this behaviour in training: Toolformer (Schick et al. 2023) showed that language models can teach themselves to decide which APIs to call, with what arguments and how to use the results, and ReAct (Yao et al. 2022) interleaves reasoning traces with actions so plans can be updated from observations. Agents loop over these steps with memory, planning and many tools, often connected through standards such as the Model Context Protocol. Because tool results enter the prompt, untrusted content such as a web page can contain instructions that try to steer the model, a risk known as prompt injection.
Technical example: A tool-use round trip
The two messages of the loop for a calculator tool, built in plain Python: the model's tool_use block, the application's execution and the tool_result message. The tool_use id is shortened here (real ids are longer).
Code (python):
import json
tool_use = {"type": "tool_use", "id": "toolu_01", "name": "calculator",
"input": {"expression": "1234 * 5678"}} # what the model returns
def calculator(expression):
left, right = expression.split("*")
return int(left) * int(right) # the application does the work
result = calculator(tool_use["input"]["expression"])
tool_result_message = {"role": "user", "content": [
{"type": "tool_result", "tool_use_id": tool_use["id"], "content": str(result)}]}
print(json.dumps(tool_use))
print(json.dumps(tool_result_message))
Output:
{"type": "tool_use", "id": "toolu_01", "name": "calculator", "input": {"expression": "1234 * 5678"}}
{"role": "user", "content": [{"type": "tool_result", "tool_use_id": "toolu_01", "content": "7006652"}]}
What it shows: The model only ever produces the first line; everything after it is ordinary code. The result is returned in the second message with the matching tool_use_id so the model can tell which request it answers. 7,006,652 is computed, not remembered, which is why tools fix arithmetic and lookup errors that models make on their own.
How a tool call works
In plain English: The model is given a menu: the names of the tools, what each is for and what information each needs. When it decides to use one, it fills in the form. It does not run anything itself.
Technically:
Tools are declared with a name, a natural-language description and an input_schema in JSON Schema. The model's output is a structured block rather than prose, and Anthropic's documentation describes the flow: the model responds with stop_reason "tool_use" and one or more tool_use blocks, your code runs the operation and sends back a tool_result.
Descriptions and schemas are part of the prompt, so clear names and constraints improve whether the model picks the right tool and arguments. Providers may also run some tools on their own servers, such as web search or code execution, and return the results in the same response.
What is function calling?
The same idea under another name: the model outputs a structured request to call a function that the application defines and runs.
The agent loop
In plain English: One tool call answers one question. A longer job needs many, so the model is run in a loop. After each result it decides whether it has what it needs or must take another step.
Technically:
The control flow is a while loop in your code: send the conversation, if the response asks for tools run them and append the results, repeat; stop on a final answer, an error or a step limit. Each iteration resends the growing conversation, so tokens and cost accumulate and the context window eventually fills, which agents handle by summarising or truncating older steps.
ReAct (Yao et al. 2022) formalised interleaving reasoning with actions: reasoning traces help the model induce, track and update plans and handle exceptions, while actions let it consult external sources such as knowledge bases or environments. Modern reasoning models do the same inside their hidden thinking.
Everyday example: Booking a trip
An agent is asked to find a flight and a hotel.
What it shows: It searches flights, reads the results, picks one, searches hotels near the arrival time, checks availability and only then writes a summary. Each search is a tool call, and its result decides what the next step is.
Connecting tools
In plain English: Every app used to need its own custom wiring for every tool. A shared standard lets one tool plug into many applications, so a tool written once works with any compatible assistant.
Technically:
The Model Context Protocol (MCP) is an open standard for connecting AI applications to tools and data sources. A server exposes tools, resources and prompts, and any MCP-capable client can discover and call them. This moves the integration work from each application to each tool.
Whatever the connection method, the application remains responsible for execution: validating arguments, enforcing permissions, handling errors and timeouts, and returning clear results. Model-written arguments are untrusted input, just like user input.
Risks and limits
In plain English: A model that can act can also act wrongly, or be tricked into it. The text it reads from web pages, emails and files can include instructions meant for it, and it cannot always tell them apart from yours.
Technically:
Prompt injection occurs when untrusted content that the model reads, such as a web page or a tool result, contains instructions that override the user's intent. Mitigations include least-privilege tools, human confirmation for irreversible actions such as sending money or deleting data, separating trusted and untrusted content, and logging.
Agents also compound errors: a wrong step early can mislead everything after it, and long loops are expensive. Practical systems cap the number of steps, verify results with independent checks, and keep a person in the loop for consequential actions.
What is prompt injection?
An attack in which text the model reads, such as a web page, contains instructions that make it do something the user did not ask for.
Common misconceptions
Myth: The model runs the tools itself.
Reality: It only writes a request. Your application runs the function and returns the result, which is why the application can enforce rules.
Myth: An agent is a different kind of model.
Reality: It is a loop around a model: the same model, called repeatedly with tool results, usually with planning prompts and memory.
Myth: Tool results are trustworthy because they come from my own tools.
Reality: If a tool returns web, email or file content, that content can include instructions that try to steer the model.
Where you'll see this
- gpt-oss architecture: gpt-oss was trained to use a browsing tool, a Python tool and developer-defined functions, interleaved with its chain of thought.
Key takeaways
- Tool use lets a model request a function call as structured JSON; the application runs it and returns the result.
- The loop continues until the model replies without asking for a tool, signalled by stop_reason "end_turn".
- An agent is a model running this loop with planning and many tools; the application controls what is actually permitted.
- Tool results are untrusted input: they can contain instructions that try to hijack the model, so permissions and confirmation steps matter.
Frequently asked questions
What is tool use in LLMs?
A model's ability to request that an application run a function, such as a search or calculation, by emitting a structured call, and then use the returned result.
What is an AI agent?
A system in which a language model repeatedly plans, calls tools, reads the results and decides the next step until a task is finished.
What is function calling?
The name some providers use for tool use: the model outputs a JSON call to a function that you define and execute.
What is the Model Context Protocol?
An open standard that lets AI applications connect to tools and data sources through a common interface.
What is prompt injection?
An attack where content the model reads contains instructions that redirect it, which is why agents need least-privilege tools and confirmation for risky actions.
Sources
Facts last verified November 23, 2026.