On its own, a language model can only produce text. It cannot check today's stock level, look up an order, send an email or run a calculation reliably. Tool use, also called function calling, changes that. You describe functions your application can run, Claude decides when to call them and with what arguments, your code executes them and Claude uses the results to continue.
Tool use is the foundation of every Claude agent, from a simple weather bot to Claude Code itself. This article explains exactly how it works and how to design tools that Claude uses well.
The core idea
Claude never runs your code directly. The flow is always:
1. You send a request that includes tool definitions.
2. Claude decides whether a tool would help. If so, it replies with a tool use block containing the tool name and arguments, and the stop reason is tool_use.
3. Your code runs the function with those arguments.
4. You send the result back in a tool result block.
5. Claude continues, either calling more tools or producing a final answer.
You stay in control of what actually executes. That is important for security and reliability.
Defining a tool
Each tool has three parts:
- name: a short identifier such as get_order_status.
- description: plain-language explanation of what the tool does, when to use it and what it returns.
- input_schema: a JSON Schema describing the parameters.
tools = [{
"name": "get_order_status",
"description": "Look up the current status of a customer order by its order ID. "
"Use when the user asks where their order is.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {"type": "string", "description": "e.g. ORD-10422"},
},
"required": ["order_id"],
},
}]
The description matters more than most developers expect. Claude reads it to decide when and how to use the tool. Anthropic's guidance emphasises detailed descriptions: what the tool does, when it should and should not be used, what each parameter means and any limitations.
Making the first call
response = client.messages.create(
model="claude-sonnet-5-5",
matokens=1024,
tools=tools,
messages=[{"role": "user", "content": "Where is my order ORD-10422?"}],
)
print(response.stop_reason) # tool_use
The response content will contain a tool use block with an id, the name get_order_status and an input object holding the order ID. It may also contain a short text block where Claude explains what it is about to do.
Returning the result
Your code runs the real function, then sends the result back. The new request must include the full conversation: the original user message, Claude's assistant message with the tool use block, and a new user message containing the tool result.
tool_block = next(b for b in response.content if b.type == "tool_use")
result = lookup_order(tool_block.input["order_id"])
messages = [
{"role": "user", "content": "Where is my order ORD-10422?"},
{"role": "assistant", "content": response.content},
{"role": "user", "content": [{"type": "tool_result",
"tool_use_id": tool_block.id, "content": result}]},
]
final = client.messages.create(model="claude-sonnet-5-5", matokens=1024,
tools=tools, messages=messages)
Claude now writes a natural answer using the tool result, for example telling the customer the order shipped yesterday and is expected on Friday.
The tool_use_id must match the id of the tool use block. Mismatched IDs are one of the most common causes of errors.
The tool use loop
Real tasks often need several tool calls. An agent loop keeps calling Claude until it stops asking for tools:
while True:
resp = client.messages.create(model=MODEL, matokens=2048,
tools=tools, messages=messages)
messages.append({"role": "assistant", "content": resp.content})
if resp.stop_reason != "tool_use":
break
results = [run_tool(b) for b in resp.content if b.type == "tool_use"]
messages.append({"role": "user", "content": results})
Here run_tool returns a tool result block for each tool use block. Always add a maximum step count in production so a confused agent cannot loop forever.
Parallel tool calls
Claude can request several tools in one response, for example checking the weather in three cities at once. Your code should run all of them, ideally concurrently, and return all the results together in a single user message, each with its matching tool_use_id.
If parallel calls cause problems for your application, the API offers a setting to disable them.
Controlling when tools are used
The tool_choice parameter controls Claude's behaviour:
- auto (default): Claude decides whether to call a tool.
- any: Claude must call one of the provided tools.
- tool: Claude must call a specific named tool.
- none: Claude must not call any tool.
Forcing a specific tool is a handy technique for structured extraction: define a tool whose schema is the data you want, and force Claude to call it. The arguments become your structured output.
Strict tool use
By default, Claude follows your schema very reliably, but small deviations are possible. For situations where any deviation would break your code, Anthropic offers strict tool use. Setting strict to true on a tool definition makes the API constrain generation so that tool inputs always match the schema. This guarantees the format of the arguments, not that the values are correct, so you still validate business rules.
Reporting errors from tools
Tools fail. A database might be down or an ID might not exist. Instead of crashing, return a tool result with is_error set to true and a helpful message. Claude can then apologise, try different arguments or ask the user for clarification.
Good error messages tell Claude what went wrong and what to do. "Order not found. Order IDs start with ORD- followed by five digits." is far more useful than "Error 404".
Write descriptions for a new colleague
Explain the tool as if to a smart new team member. Include what it does, when to use it, what it returns, formats and limitations.
Keep tools focused
One tool per clear action works better than a giant tool with a dozen modes. But avoid dozens of nearly identical tools, which confuse selection and consume context.
Use clear parameter names and enums
Prefer status with an enum of open, closed and pending over a free-text field. Enums reduce invalid input.
Return concise, relevant results
Tool results go into the context window. Returning a 5,000-row table wastes tokens and can bury the important data. Return only what Claude needs, and paginate or summarise large results.
Make results self-explanatory
Include field names and units. "price 450, currency INR" beats "450".
Make side effects explicit
Mark tools that change data or send messages clearly in their descriptions, and add confirmation steps in your application for risky actions.
Security considerations
Tool use connects a model to real systems, so treat it like any other integration with untrusted input.
- Validate arguments in your code. Never pass them directly into database queries or shell commands without validation and parameterisation.
- Apply permissions based on the real user, not on what the model claims.
- Limit capabilities. Give each agent only the tools it needs.
- Require human approval for destructive or financial actions.
- Beware prompt injection. Content from web pages, emails or documents returned by tools can contain instructions. Make sure your system prompt tells Claude to treat tool results as data, and keep critical permissions enforced in code.
- Log every tool call with arguments and results for audit.
Server tools from Anthropic
Besides your own client tools, Anthropic provides certain built-in tools that run on its side, such as web search, web fetch and code execution, depending on the model and account. You enable them by including them in the tools list, and Anthropic executes them for you. Check the documentation for which tools are available and how they are billed.
Common mistakes
- Vague descriptions, so Claude picks the wrong tool or none.
- Forgetting to include the assistant's tool use message before the tool result.
- Mismatched tool_use_id values.
- Returning huge results that blow the context window.
- No step limit on agent loops.
- Trusting arguments without validation.
- Not handling the case where Claude answers directly without calling a tool.
Key takeaways
- Tool use lets Claude request actions; your code performs them.
- Define tools with a clear name, detailed description and JSON Schema.
- Run a loop until stop_reason is no longer tool_use.
- Return concise results and helpful errors.
- Use tool_choice and strict mode when you need guarantees.
- Validate, authorise and log every call.
Master tool use and you have the building block for agents, integrations and every serious Claude application.
Related articles on MindStick
- Building AI Agents With Claude, Patterns That Work
- Model Context Protocol Explained, Connecting Claude to Your Data
- Structured JSON Output From Claude Reliably
- Debugging Tool Use Failures in Claude Agents
API parameters and built-in tool availability change over time. Check Anthropic's current tool use documentation before building production features.
Leave a Comment