Tool Use and Function Calling
How an LLM asks your code to run functions: tool definitions, JSON Schema inputs, tool_use and tool_result blocks.
What is it?
Tool use (or function calling) lets a model ask your app to run a function and return the result. The model can't browse, read your database, or reliably do arithmetic, but it can say: 'please call get_weather with {"city": "Paris"}'. Your code runs it and replies. This one mechanism turns a text generator into something that can look things up and act.
A tool definition has three parts:
- name: a short identifier like
get_weather(letters, digits, underscores, hyphens). - description: plain text saying what the tool does, when to use it, and what it returns. This matters most: the model picks tools mainly by reading descriptions.
- input_schema: a JSON Schema (a standard format for describing data shape) listing the arguments, their types, which are required, and allowed values (
enum).
The conversation then gets two new kinds of content blocks (a message's content can be a list of typed blocks instead of a string):
- tool_use (in the assistant's message):
{type: "tool_use", id: "toolu_...", name: "get_weather", input: {city: "Paris"}}. Theidis unique per call. - tool_result (in the next user message):
{type: "tool_result", tool_use_id: "toolu_...", content: "18°C and cloudy"}. Thetool_use_idmust match the call it answers. If the tool failed, addis_error: truewith an error message as content, so the model tries something else.
When the model wants a tool, stop_reason is tool_use. Append the assistant message to your history exactly as received (tool_use blocks included), run the tools, send one user message with the tool_result blocks, and call the model again.
Parallel tool calls: one assistant turn can hold several tool_use blocks (say, weather in Paris and Oslo). Run them all (in parallel if independent) and return all results in one user message, one tool_result per tool_use id. Splitting them across messages is a common bug.
tool_choice controls tool use. The default, auto, lets the model decide whether to call a tool or answer directly. On current Claude models, use auto plus clear instructions in the system prompt and tool descriptions ('Always use the calculator for arithmetic') instead of forcing a specific tool.
Designing good tools is a skill, sometimes called the agent-computer interface:
- Write descriptions like docs for a new colleague: what it does, when to use it and when not to, what the output looks like, and limits (max rows, units).
- Keep the set small and non-overlapping. Two tools that both 'search' confuse the model.
- Make arguments hard to misuse: enums instead of free text, clear units ('amount_cents'), and explicitly stated absolute or workspace-relative paths.
- Return short, useful output. A 2 MB JSON dump wastes context; return the fields that matter and say when results were truncated.
- Return helpful errors ('No file named todo.txt; available: notes.txt, plan.md') so the model can recover.
Tool use is also a handy way to get structured data: the model's input always follows your schema. For pure data extraction, dedicated structured-output features (see Structured Output) are usually simpler.
Explain like I'm 10
Picture a manager on the phone with an assistant at the office. The manager can't reach the filing cabinet, so they say: 'Please pull the file for order 42.' The assistant reads it back, and the manager decides what to ask next. The manager is the model, the assistant is your code, and the list of allowed jobs (with instructions for each) is your tool list.
Examples
Validate and dispatch tool calls like an API would send them (runnable)
// One tool definition, in the same shape you send to the API.
const tools = [{
name: "get_weather",
description: "Get the current temperature for a city. Use when the user asks about weather.",
input_schema: {
type: "object",
properties: {
city: { type: "string", description: "City name, e.g. Paris" },
unit: { type: "string", enum: ["celsius", "fahrenheit"], description: "Defaults to celsius" },
},
required: ["city"],
},
}];
// A tiny JSON Schema check (real projects use a library such as ajv or pydantic).
function validate(schema, input) {
const errors = [];
for (const key of schema.required || []) if (!(key in input)) errors.push("missing required field '" + key + "'");
for (const [key, value] of Object.entries(input)) {
const prop = schema.properties[key];
if (!prop) { errors.push("unknown field '" + key + "'"); continue; }
if (prop.type === "string" && typeof value !== "string") errors.push(key + " must be a string");
if (prop.enum && !prop.enum.includes(value)) errors.push(key + " must be one of: " + prop.enum.join(", "));
}
return errors;
}
const impl = {
get_weather: ({ city, unit = "celsius" }) => {
const c = { paris: 18, oslo: 4 }[city.toLowerCase()];
if (c === undefined) throw new Error("No weather data for " + city + ". Known cities: Paris, Oslo");
return unit === "celsius" ? c + " C" : Math.round(c * 9 / 5 + 32) + " F";
},
};
// Turn ONE tool_use block into ONE tool_result block - never throw.
function handleToolUse(block) {
const def = tools.find((t) => t.name === block.name);
if (!def) return { type: "tool_result", tool_use_id: block.id, content: "Unknown tool: " + block.name, is_error: true };
const errors = validate(def.input_schema, block.input);
if (errors.length) return { type: "tool_result", tool_use_id: block.id, content: "Invalid input: " + errors.join("; "), is_error: true };
try {
return { type: "tool_result", tool_use_id: block.id, content: impl[block.name](block.input) };
} catch (err) {
return { type: "tool_result", tool_use_id: block.id, content: err.message, is_error: true };
}
}
// Pretend the model asked for four tools in ONE turn (parallel tool calls).
const assistantContent = [
{ type: "text", text: "Let me check both cities." },
{ type: "tool_use", id: "toolu_1", name: "get_weather", input: { city: "Paris" } },
{ type: "tool_use", id: "toolu_2", name: "get_weather", input: { city: "Oslo", unit: "fahrenheit" } },
{ type: "tool_use", id: "toolu_3", name: "get_weather", input: { town: "Rome" } },
{ type: "tool_use", id: "toolu_4", name: "get_stock_price", input: {} },
];
const results = assistantContent.filter((b) => b.type === "tool_use").map(handleToolUse);
// ALL results go back in ONE user message:
const nextUserMessage = { role: "user", content: results };
for (const r of nextUserMessage.content) console.log(JSON.stringify(r));Each tool_use gets exactly one tool_result with the matching id. Errors are returned with is_error (not thrown) so the model can recover. All results go back together in one user message.
One full tool round trip with the Anthropic SDK
import anthropic
client = anthropic.Anthropic()
tools = [{
"name": "get_weather",
"description": "Get the current weather for a city. Use when the user asks about weather. "
"Returns temperature in Celsius and a short condition, e.g. '18 C, cloudy'.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "City name, e.g. 'Paris'"}},
"required": ["city"],
},
}]
def get_weather(city: str) -> str:
fake = {"paris": "18 C, cloudy", "oslo": "4 C, snow"}
if city.lower() not in fake:
raise ValueError(f"No data for {city}. Try a major city.")
return fake[city.lower()]
messages = [{"role": "user", "content": "Should I pack a coat for Paris and Oslo?"}]
# Turn 1: the model asks for tools
resp = client.messages.create(model="claude-opus-5-5", max_tokens=16000,
tools=tools, messages=messages)
print(resp.stop_reason) # "tool_use"
messages.append({"role": "assistant", "content": resp.content}) # keep tool_use blocks!
results = []
for block in resp.content:
if block.type == "tool_use":
try:
out = get_weather(**block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": out})
except Exception as e:
results.append({"type": "tool_result", "tool_use_id": block.id,
"content": f"Error: {e}", "is_error": True})
messages.append({"role": "user", "content": results}) # ALL results, ONE message
# Turn 2: the model uses the results to answer
resp = client.messages.create(model="claude-opus-5-5", max_tokens=16000,
tools=tools, messages=messages)
print(next(b.text for b in resp.content if b.type == "text"))Two calls: the first returns tool_use blocks, the second gets the results. Put these two calls in a loop and you have an agent (see The Agent Loop).
What the message history looks like after one tool round
[
{"role": "user", "content": "Should I pack a coat for Paris and Oslo?"},
{"role": "assistant", "content": [
{"type": "text", "text": "I'll check the weather in both cities."},
{"type": "tool_use", "id": "toolu_01A", "name": "get_weather", "input": {"city": "Paris"}},
{"type": "tool_use", "id": "toolu_01B", "name": "get_weather", "input": {"city": "Oslo"}}
]},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "toolu_01A", "content": "18 C, cloudy"},
{"type": "tool_result", "tool_use_id": "toolu_01B", "content": "4 C, snow"}
]}
]Roles still alternate user/assistant. Tool results use role 'user' because they come from your side. The ids are illustrative.
How it works
When you pass tools, the API turns your definitions into special instructions the model was trained to follow. If the model decides a tool would help, it writes a tool_use block instead of (or after) plain text and stops with stop_reason: "tool_use".
The model shapes input to match your JSON Schema, but treat it as untrusted input: validate types and ranges, check permissions, and never pass it straight to a shell or SQL query. Text in a web page or document the model read can trick it into requesting harmful actions. This is prompt injection, covered in AI Security.
Tool definitions go with every request and count as input tokens, so 50 verbose tools cost you on every call. Keep the list focused.
your app model
-------- -----
messages + tool defs ------> reads, decides
<------ tool_use {id, name, input}
validate input (stop_reason = tool_use)
run real function
tool_result {tool_use_id,
content, is_error?} ------> reads result
<------ final text
(stop_reason = end_turn)Why does it exist?
Models are frozen at training time and live in a text box. They don't know today's weather, your customer's order status, or what's in your repo, and they make arithmetic slips. Tool use gives them a structured way to ask for fresh data and trigger actions, while your code controls what actually runs.
When to use it
Use tools when the model needs information it can't know (live data, private data, search), exact computation (math, dates, code execution), or an action (create a ticket, write a file). A tool definition also gets you arguments in a guaranteed shape.
When not to use it
Don't wrap things in tools that could just go in the prompt: if the model always needs the same small document, include it. Don't expose dangerous capabilities (arbitrary shell, raw SQL, payments) without strict validation and approval. Don't create dozens of overlapping tools; merge or remove them.
Common mistakes
Writing one-line tool descriptions; the model picks tools by their descriptions, so vague ones cause wrong or missing calls.
Dropping the assistant's tool_use blocks from the history and sending only the text - the next request then has tool_results with no matching tool_use and fails.
Sending parallel tool results as separate user messages instead of one message with several tool_result blocks.
Raising an exception and crashing the program when a tool fails, instead of returning a tool_result with is_error: true.
Trusting tool inputs blindly - passing a model-supplied path or query straight to the file system, shell or database.
Returning huge raw outputs (whole web pages, full tables) that flood the context window.
Defining overlapping tools (search_docs and find_documents) so the model has to guess which one to use.
Practice exercises
- Easy:
Write a tool definition (name, description, input_schema) for
convert_currencywith amount, from and to fields, using enums for currency codes. - Easy:
In the runnable demo, add a
daysinteger field to get_weather and extend the validator to check numbers. Send an invalid value and confirm the error result. - Medium:
Rewrite three tool descriptions from a real API you know so that a new colleague could use them correctly without other docs. Include when NOT to use each.
- Medium:
Using the Python round-trip example, ask a question that needs no tool ('What is the capital of France?') and print the stop_reason. Explain the result.
- Hard:
Build a dispatcher in Python that runs independent tool calls from one turn concurrently with a thread pool, preserves the matching ids, applies a 10-second timeout per tool, and reports timeouts as is_error results.
Interview questions
Walk through one tool-use round trip.
You send messages plus tool definitions. The model returns an assistant message with one or more tool_use blocks and stop_reason tool_use. You append that assistant message unchanged, run each tool, then append one user message with a tool_result per tool_use id. You call the model again; it reads the results and either calls more tools or answers.
Why does the tool description matter so much?
The model decides whether and which tool to call almost entirely from names and descriptions. A good description states what the tool does, when to use it and not use it, argument meanings and units, and what the output looks like. Most tool-selection bugs are description bugs.
How should a tool failure be reported to the model?
Return a tool_result for that tool_use id with is_error set to true and a clear, actionable message (what failed and how to fix it). The model can then retry with different arguments or choose another approach instead of the program crashing.
How do you handle parallel tool calls?
Run every tool_use in the assistant turn (concurrently if independent) and send all tool_result blocks together in a single user message, each referencing its tool_use_id.
Is a tool's input safe to use directly?
No. It is model-generated and can be influenced by prompt injection from content the model read. Validate against the schema, enforce permissions and allow-lists, sanitize paths and queries, and require human approval for risky actions.
What does tool_choice auto mean?
The model decides on its own whether to call a tool or answer directly. On current Claude models you guide this with clear system-prompt instructions and descriptions rather than forcing a specific tool.