Tool Use / Function Calling
Tool use lets Claude call functions you define — search, a calculator, your database, any API — and use the results. It's the foundation of every agent.
- How the four-step agentic loop works, from tool definitions to final answer
- How to define a tool in Python with name, description, and JSON-Schema input
- Why tool descriptions act as prompts that shape when and how Claude calls them
- How to validate inputs, return errors as results, and use server-side tools safely
The loop
Tool use is a conversation, not a single call. You hand Claude a menu of tools; Claude picks one and pauses; you run it and report back; Claude folds the result into its answer — repeating as needed.
- You include a list of tool definitions — each with a name, a description, and a JSON-Schema input.
- If Claude decides to use one, it returns a tool_use block with arguments and stops.
- You run the tool yourself and send the output back as a tool_result.
- Claude continues, possibly calling more tools, until it answers.
Defining a tool (Python)
A tool definition is just a name, a plain-language description, and a JSON-Schema for the input. Pass it in tools, then check stop_reason to know when Claude wants to act.
get_weather tool + first call
tools = [{
"name": "get_weather",
"description": "Get current weather for a city.",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
}]
msg = client.messages.create(
model="claude-sonnet-5", max_tokens=1024,
tools=tools,
messages=[{"role": "user", "content": "What's the weather in Rome?"}],
)
# If msg.stop_reason == "tool_use": run the tool, then send a tool_result back.Tips
Small choices in how you define and handle tools make a large difference in reliability.
- Descriptions are prompts. A clear tool
descriptionand parameter docs hugely improve when/how Claude calls it. - Validate inputs you receive before executing — never trust them blindly.
- Return errors as results. If a tool fails, send a
tool_resultdescribing the error so Claude can recover. - Server-side tools. Anthropic also offers built-in tools (e.g. web search, code execution, computer use) — check the docs for the current menu.
:::warning Tools = actions = risk A tool that takes real actions inherits a security model. Apply least privilege and keep a human in the loop for risky calls — see Securing Agents & Tools. :::
Check yourself
0/3- Tool use is a loop: send tool definitions, Claude returns a tool_use block and stops, you execute and return a tool_result, Claude continues until it answers.
- A tool definition is a name, a description, and a JSON-Schema input — pass it in tools and check stop_reason == tool_use.
- Descriptions are prompts; validate inputs before executing; return failures as tool_result errors so Claude can recover.
- Anthropic also offers server-side tools, and any tool that takes real actions needs least privilege plus a human in the loop.
Next
- Programmatic Tool Calling — let Claude call these same tools from Python inside a sandbox, cutting round-trips and keeping intermediate results out of context
- Building Agents on the API
- Structured Output
- MCP & Connecting to Tools