Tool Calling: How LLMs Use Tools (Function Calling Explained)
A model can't run code itself, so it asks the application to call a tool and send back the result.
On this page
This is Part 1 of our two-part series of articles on tool use.
A large language model (LLM) reads text and writes text. It can't look up a customer's order or send a refund. If an assistant has to do those things, the application around the model has to do them.
Tool use, also called tool calling or function calling, is how the model asks the application to do them. The application tells the model which actions it may request. When the model needs one, it replies with a structured request, in JSON, that names the action and its arguments, instead of an answer. The application decides whether to run the action, runs it, and sends the result back so the model can continue.
In an interview, when you're asked to design an AI assistant that takes actions, "I'd give the model some tools" is only the start of an answer. An interviewer will usually want to know who runs each tool, what your code checks before running it, and what stops the model from calling tools forever.
We'll answer those questions by building a support assistant for an online shop. This article covers how a tool call works, from the definition the model reads to the loop that ends with an answer. Part 2 covers running tools safely: when a person should approve a call, how to retry a call that timed out, meaning it got no reply in time, without doing the action twice, and how to handle prompt injection, instructions hidden in text that a tool returns.
A question the model can't answer alone
Fernleaf is an online homeware shop with a support assistant on its website. On the 20th of October 2026, a customer signs in to the website, opens the support chat and sends the assistant this message:
The lamp from my last order arrived cracked. Can I get my money back?
Fernleaf refunds an item within 30 days of delivery, to the original card, for the amount paid. Any refund over $50 needs approval from a support agent, a person on Fernleaf's support team. This customer has two recent orders:
| Order | Items | Paid | Delivered |
|---|---|---|---|
| FL-48213 | Ceramic table lamp ($84), linen cushion cover ($22) | $106 | 16th October 2026 |
| FL-47790 | Bamboo bath mat ($31) | $31 | 2nd October 2026 |
The orders are in Fernleaf's order database, and refunds go through its payment provider. The model has no access to either of them: it can't look up an order or send a refund. If the application sends it only the customer's message, the best the model can manage is a reply like this one (illustrative output, as are all the model's messages in this article):
I'm sorry the lamp arrived damaged. I can't look up orders, but Fernleaf's
support team can help. Please contact them with your order number.
The model knows what a refund is. But it doesn't know which order the customer means or what the lamp cost, and it has no way to send the money back. The application can do both. It can read the customer's orders from the order database, and it can ask the payment provider to refund a payment. So we'll let the model ask the application to do these things for it.
Describe the tools the model may use
The model can only ask for a tool it has been told about. So the application gives the model a list of tools, each an action the application is willing to run, described by three things:
- A name the model uses to ask for it.
- A description of what the tool does and when to use it.
- A schema for its arguments: the values the model has to supply when it asks.
Together, the name, description and schema make up the tool's definition.
The schema is written in JSON Schema, a standard format for describing the shape of JSON data. It lists each argument, its type, whether it's required and any limits on its value, such as a minimum and maximum.
Here's Fernleaf's tool for reading one order:
{
"name": "get_order",
"description": "Returns one of the signed-in customer's orders: its items, each with an item ID, name and amount paid, the total paid, the delivery date and any gift message. Use it when you need the details of an order whose ID you already know.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The order ID, such as FL-48213."
}
},
"required": ["order_id"]
}
}
The model never sees the code behind get_order. All it gets is the definition above: the name, the description and the schema. It reads the name and description to decide whether the tool fits what it needs next, and the schema to fill in the arguments. Of the three, the description has the most effect on whether the model picks the right tool. A good description says what the tool does, what it returns and when to use it. A useful test is to ask whether a new colleague could use the tool correctly from the description alone. Fernleaf's description passes. A vague description of the same tool, such as "Gets order info", would fail: it doesn't say whose orders the tool can read, or that the result includes what each item cost.
Now back to the customer's message. They wrote "my last order", not an order ID, so the model also needs a way to find orders:
{
"name": "find_orders",
"description": "Lists the signed-in customer's most recent orders, newest first, with each order's ID, item names and delivery date. Use it when the customer refers to an order without giving its ID, such as 'my last order'.",
"input_schema": {
"type": "object",
"properties": {
"limit": {
"type": "integer",
"description": "How many orders to return, from 1 to 10. Defaults to 5.",
"minimum": 1,
"maximum": 10
}
},
"required": []
}
}
find_orders has no argument for the customer. The application already knows who is signed in, so it adds the customer's ID itself when it runs the query. If the model had to supply the ID, it could get it wrong, or a customer could talk it into asking for someone else's orders.
With those two tools, the model can find the customer's orders and read one. To give the money back, it needs a third tool that changes something instead of reading:
{
"name": "request_refund",
"description": "Start a refund for damaged, missing or wrong items in one of the signed-in customer's orders. Use the order ID and item IDs from get_order.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The ID of the order, such as FL-48213."
},
"items": {
"type": "array",
"items": { "type": "string" },
"description": "The item IDs of the items to refund, as get_order lists them, such as itm_7QK2. Only the items the customer says are affected."
},
"reason": {
"type": "string",
"enum": ["damaged", "not_delivered", "wrong_item", "other"],
"description": "Why the customer wants a refund."
}
},
"required": ["order_id", "items", "reason"]
}
}
The model fills in the three arguments: order_id (which order), items (which items in it) and reason (why). It works them out from the customer's message and from what find_orders and get_order returned. items is a list because an order can hold several things and the customer may want money back for only some of them. Here, FL-48213 has a lamp and a cushion cover, and only the lamp broke. Each entry is an item ID, such as itm_7QK2 for the lamp, not the item's name. An ID points to exactly one line in the order. A name can be misspelt by the model, or shared by two items, such as two identical lamps in one order, and then the application can't tell which one to refund. So production systems pass IDs between tools, and the model copies them from get_order's result. reason uses an enum, a fixed list of allowed values, so the model has to pick one of the four reasons Fernleaf's refund records accept instead of writing its own. Each argument also has its own description, which tells the model where the value comes from.
When you design a tool, ask the model only for values it has to work out from the conversation. If your code already knows a value, your code fills it in. request_refund follows that rule. Like find_orders, it takes no customer ID, because the application knows who is signed in. It also takes no amount and no card: the application works out the amount from what the customer paid for those items, and sends the money back to the card they paid with. So the model can't get the amount wrong, and a customer can't talk it into a bigger refund.
Follow one tool call
The application sends the model the customer's message and the three tool definitions. The model reads the message and works out that it needs the customer's orders before it can answer. So instead of writing a reply for the customer, it sends back a tool call: a request for the application to run one of its tools. A tool call has three parts: the name of the tool, the arguments to run it with, and a call ID, a label the application uses to match the answer to the request. The reply also comes with a stop reason (some APIs call it a finish reason), which says the model stopped to ask for a tool. That's how the application knows the reply isn't an answer to show the customer.
{
"tool": "find_orders",
"call_id": "call_1",
"arguments": {}
}
The arguments are empty because limit is optional, so find_orders uses its default of 5 orders. Nothing has been looked up yet. The model has only asked, and the application decides whether to run the tool.
The tool call is text the model generated, so it can be wrong. A model can name a tool the application never offered, or send arguments that don't fit the schema, such as a limit of 50 or the word "five". So before running anything, the application checks the tool call in two ways. First, it looks up the name in its own list of tools: find_orders is one of the three it offered. Second, it checks the arguments against that tool's schema. This call has no arguments, which the schema allows because limit is optional. If the model had sent a limit, the check would make sure it was a whole number from 1 to 10, so a limit of 50 or "five" would fail. If either check fails, the application doesn't run the tool, and tells the model what was wrong instead. This call is valid, so the application looks up the signed-in customer's orders in the order database. It sends what it found back to the model as a tool result, labelled with the same call ID:
{
"call_id": "call_1",
"result": {
"orders": [
{ "order_id": "FL-48213", "items": ["Ceramic table lamp", "Linen cushion cover"], "delivered": "2026-10-16" },
{ "order_id": "FL-47790", "items": ["Bamboo bath mat"], "delivered": "2026-10-02" }
]
}
}
One reply from the model can contain several tool calls. The application then sends back one result for each call, with that call's ID on it, so the model can tell which result belongs to which call.
To send the result, the application makes a new request to the model. But the model doesn't remember the first request. Most model APIs are stateless: they keep nothing between requests. So if the application sent the result on its own, the model wouldn't know what it was for. Instead, the application sends the whole conversation again: the customer's message, the model's tool call, the tool result and the tool definitions. Now the model can read the customer's question and the list of orders together, and see that the lamp is in order FL-48213.
Each round adds a tool call and its result to the conversation, so every request is longer than the last. Models read and write text in tokens, small pieces of text such as a short word or part of a longer one. Providers charge for every token in each request, and longer requests take longer to answer, so a loop with many steps adds cost and latency, the time the customer waits for a reply. The whole conversation also has to fit in the model's context window, the most tokens it can read in one request.
Keep going until the model answers
In its next reply, the model asks for the details of both orders at once, with two tool calls:
[
{ "tool": "get_order", "call_id": "call_2", "arguments": { "order_id": "FL-48213" } },
{ "tool": "get_order", "call_id": "call_3", "arguments": { "order_id": "FL-47790" } }
]
Some providers call these parallel tool calls: one reply that contains more than one tool call. Neither lookup depends on the other, so the application can run both at the same time. It sends both results back in its next request, each with its call ID. Here's the result for call_2, shortened:
{
"call_id": "call_2",
"result": {
"order_id": "FL-48213",
"delivered": "2026-10-16",
"items": [
{ "item_id": "itm_7QK2", "name": "Ceramic table lamp", "paid": "84.00" },
{ "item_id": "itm_4MR8", "name": "Linen cushion cover", "paid": "22.00" }
],
"total_paid": "106.00"
}
}
The full result also includes the order's gift message, a note the customer can add at checkout.
Next, the model calls request_refund:
{
"tool": "request_refund",
"call_id": "call_4",
"arguments": {
"order_id": "FL-48213",
"items": [],
"reason": "damaged"
}
}
Two of the three arguments are filled in: order_id is FL-48213 and reason is damaged, but the items argument is an empty list. That fits the schema, because an empty list is still a list. But the application can't refund an order without knowing which items, so it rejects the call with an error result, a tool result that tells the model the call failed and why. The model asks the customer which item broke, and the customer answers: "Just the lamp." So the model calls request_refund again (call_5), this time with items set to ["itm_7QK2"], the item ID of the ceramic table lamp. The lamp cost $84, over Fernleaf's $50 limit, so the application doesn't send the refund straight away. Instead, it sends the model this result:
{
"call_id": "call_5",
"result": "The $84.00 refund for the ceramic table lamp is waiting for a support agent to approve it. The customer will get an email with the decision."
}
Now the model has what it needs, and it replies with text for the customer instead of another tool call:
I've asked for a refund of $84.00 for the ceramic table lamp. A member of
our support team will approve it, and you'll get an email with the decision.
Each of those replies came from one request to the model, and the application runs the requests in a loop, called the agent loop:
messages = [customer_message]
for step in range(MAX_STEPS):
reply = call_model(messages, tools)
messages.append(reply)
if not reply.tool_calls:
return reply.text # an answer or a question for the customer
# Check each call, run it, and return a result or an error result.
results = [run_tool(call) for call in reply.tool_calls]
messages.append(results)
return hand_over_to_support_agent(messages) # the loop ran out of steps
In each step, the application sends the conversation, reads the reply, and either stops or runs the tools and goes round again. A reply without tool calls ends the loop, whether it's the final answer or a question for the customer. When the customer replies, the loop starts again with the longer conversation.
The model decides when to stop, and it doesn't always stop. It can repeat a call that keeps failing, or keep looking things up without getting closer to an answer. So the application sets its own limits on the loop: a maximum number of steps, a time limit for the whole conversation, and a budget for tokens.
When a limit is reached, Fernleaf's application hands the conversation to a support agent and tells the customer that someone will reply, instead of showing an error. Passing a decision or a conversation to a person like this is often called human-in-the-loop (HITL).
A well-formed call can still be wrong
A tool call is well-formed when it names a tool the application offered and its arguments match that tool's schema. The model writes a tool call the same way it writes any other text, so it can get the call wrong. Here are three common mistakes:
- It asks for a tool that doesn't exist. For example, it calls
cancel_order, which Fernleaf never offered. - It asks for a real tool with invalid arguments. For example, it sends
reason: "broken", which isn't in the enum, or leaves outitems. - It asks for a real tool with valid arguments that are wrong. For example, it refunds the bath mat in FL-47790 because it misread which order was the last one. Or it refunds both items in FL-48213 when only the lamp broke.
The first two mistakes break the schema, and some model providers can stop them before they happen. In what's usually called strict mode, the provider limits what the model can write while it's writing a tool call, so the call always names one of your tools and its arguments always fit the schema. This technique is called constrained decoding.
Strict mode can't stop the third mistake, though. A refund for the bath mat in FL-47790 fits the schema perfectly, because FL-47790 is a real order ID and "damaged" is an allowed reason. The schema describes what a valid call looks like. It can't know which order the customer meant or which item broke.
So the application checks the values before it runs request_refund. It checks that:
- the order belongs to the signed-in customer;
- it was delivered no more than 30 days before the customer wrote;
- the call names at least one item, and every item ID belongs to that order;
- none of those items has already been refunded.
The application runs these checks in its own code. Before it runs request_refund, it reads order FL-48213 from the order database and compares the call with what's stored there: whose order it is, when it was delivered, which items it holds and which have already been refunded. Telling the model the rules isn't enough on its own.
Say Fernleaf adds "only refund orders delivered in the last 30 days" to the system prompt, the instructions the application sends to the model with every request. A customer writes about a vase delivered 45 days ago and insists it arrived broken. The model may still call request_refund, because an instruction is only text it reads, and it can misread it or be talked out of it.
The check in code doesn't depend on the model. It reads the delivery date from the order database on every call, so the 45-day-old order is rejected whatever the model decided.
Even with these checks, some mistakes still get through.
Say the model mixes up the orders and asks to refund the bath mat in FL-47790 instead of the lamp. The application checks the call: FL-47790 is this customer's order, it was delivered 18 days before they wrote, the bath mat is in it, and it hasn't been refunded. Every check passes, so the application refunds the bath mat.
The checks can't catch this mistake, because nothing in the order database says which item the customer wants back. Only the customer's message says that.
Avoid this
Don't say strict mode makes tool calls correct. At most, it makes them match the schema. Your code still has to check that the values are right for the user and allowed by your rules before it runs the tool.
When a call fails, tell the model why
call_4 failed the application's check, so the application sent back an error result instead of running the tool.
If the result says only "failed", the model doesn't know what went wrong. It may send the same call again, or tell the customer that refunds aren't working. Fernleaf's error result for call_4 says what was wrong and how to fix it:
{
"call_id": "call_4",
"is_error": true,
"result": "No refund started: no items given. Give the item IDs to refund from FL-48213, as listed by get_order: itm_7QK2 (Ceramic table lamp), itm_4MR8 (Linen cushion cover). If you're not sure which items the customer means, ask them."
}
is_error tells the model that the call failed. Some model providers have a field for this; with others, the text of the result says so. The message names the item IDs the model can use, and tells it to ask the customer if it isn't sure which one they mean. That's why the model asked which item broke instead of guessing.
A tool can also fail for reasons that have nothing to do with the call. If the order database doesn't respond within a few seconds, the call times out. The application sends an error result for that too, such as "The order system isn't responding. Try again in a few minutes.", and the model can tell the customer to try again later.
When code should choose the steps
Not every task needs the model to choose its tools.
Fernleaf's assistant needed to, because the customer wrote free text. Nothing in "The lamp from my last order arrived cracked" says which tool to call, so the model had to decide to look up the orders, read them, and then ask for the refund.
Now say Fernleaf adds a refund form to its website, where the customer picks the order and the item from a list. The application then has the order ID and the item ID from the start, and the steps are always the same: read the order, run the four checks, start the refund. Ordinary code can run those steps in that order. It needs no loop, and it does the same thing every time, so it's cheaper, faster and easier to test.
When code decides the order of the steps like this, it's called a workflow. A workflow can still call a model for one step, such as summarising what the customer wrote, but the code decides what happens next. When the model decides the steps, as Fernleaf's assistant does, it's called an agent. Use a workflow when you know the steps in advance, and an agent when they depend on what the customer writes. Fernleaf could use the form for most refunds and keep the assistant for messages the form doesn't cover.
Conclusion
With tool use, the model never runs anything. It reads each tool's name, description and argument schema, replies with a tool call, and the application decides whether to run it. The application sends the result back tagged with the call's ID, and the loop repeats until the model replies with text or the loop reaches a limit you set.
A tool call can be well-formed, naming a real tool with arguments that match its schema, and still be wrong. Schema checks, including strict mode where your provider supports it, catch unknown tools, missing arguments and values outside the schema, such as a reason that isn't in the enum. Your code checks that the values are right for this user and allowed by your rules: that the data the call points to belongs to the signed-in user, that the action fits your business rules, and that it hasn't already been done. When a check or a tool fails, the error result tells the model what went wrong, so it can correct the call or ask the customer.
In an interview, walk through one customer message end to end. Say which tools the model can call, what each definition contains, what your code checks before running a tool, and how the loop ends when the model keeps calling tools.
Fernleaf's assistant can now find an order and ask for a refund. A refund sends money, so Part 2 looks at running tools like request_refund safely: having a support agent approve larger refunds, retrying when the payment provider times out without refunding twice, using an idempotency key that marks each refund so it's made only once, handling text in a tool result that tries to give the model instructions (prompt injection), and logging every call.
Loading comments…