How to Give Claude a Tool with the Claude API in Python
Claude can't look things up on its own: it only knows what's in the conversation. A tool changes that. You describe a function your code can run, and when a question needs it, Claude asks you to run it, with the arguments filled in. This guide gives Claude one tool and reads its request, with real output from anthropic 2.14.0.
Syntax
tool = {
"name": "<tool name>",
"description": "<what it does and when to use it>",
"input_schema": {
"type": "object",
"properties": {"<argument>": {"type": "<type>", "description": "<what it is>"}},
"required": ["<argument>"],
},
}
response = client.messages.create(model=<model ID>, max_tokens=<limit>, tools=[tool], messages=<messages>)
| Part | What it means |
|---|---|
name | The tool's name. Letters, digits, _ and - only, up to 128 characters |
description | What the tool does and when Claude should use it. Claude decides from this |
input_schema | A JSON Schema for the tool's input: an object, its properties, and which ones are required |
tools=[...] | The tools Claude may ask for in this request |
When Claude wants a tool, the reply's stop_reason is tool_use, and its content holds a tool_use block. Anthropic's documentation lists its fields: id, "A unique identifier for this particular tool use block", name, "The name of the tool being used", and input, "An object containing the input being passed to the tool, conforming to the tool's input_schema."
What It Costs
The Claude API is paid: Claude Opus 5.5 costs $4 per million input tokens and $20 per million output tokens. Tools add input tokens: the tool definitions are sent with every request, and the API adds a system prompt for tool use, 286 tokens on Claude Opus 5.5. The run below used 419 input and 54 output tokens, about $0.003.
Prerequisites
- A Claude API key, in a
.envfile asANTHROPIC_API_KEY=.... - The Anthropic Python SDK in a uv project. This guide uses anthropic 2.14.0, added with
uv add anthropic, on Python 3.15.2.
Step 1: Write the Script
The example is a café's opening hours. Claude can't know them, so it needs a tool to find out:
import anthropic
client = anthropic.Anthropic()
opening_hours_tool = {
"name": "get_opening_hours",
"description": (
"Get the café's opening hours for one day of the week. "
"Use it whenever someone asks when the café is open."
),
"input_schema": {
"type": "object",
"properties": {
"day": {"type": "string", "description": "A day of the week, such as Monday."},
},
"required": ["day"],
},
}
response = client.messages.create(
model="claude-opus-5-5",
max_tokens=1024,
tools=[opening_hours_tool],
messages=[{"role": "user", "content": "Is the café open on Sunday?"}],
)
print("Stop reason:", response.stop_reason)
print("Blocks:", [block.type for block in response.content])
for block in response.content:
if block.type == "tool_use":
print("Tool call:", block.name, block.input)
print("Call ID:", block.id)

- The
descriptionsays what the tool does and when to use it. Anthropic's tool guide calls detailed descriptions "by far the most important factor in tool performance". input_schemasays the tool takes one argument,day, a string, and that it's required.- The script doesn't run any tool yet: it only prints what Claude asked for.
Step 2: Run It
uv run --env-file .env first_tool.py
Stop reason: tool_use
Blocks: ['tool_use']
Tool call: get_opening_hours {'day': 'Sunday'}
Call ID: toolu_01KUApMo49gGPeBiwiUdk9h6

Claude didn't answer the question. It stopped with tool_use and asked for get_opening_hours with {'day': 'Sunday'}: it turned "Sunday" in the question into the argument the schema asked for. The call ID is how you'll tell Claude which call a result answers. Running the tool and sending its result back is the next step, in How to Send a Tool Result Back to Claude in Python.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A space in the tool's name, like "opening hours" | 400: tools.0.custom.name: String should match pattern '^[a-zA-Z0-9_-]{1,128}$' | Use letters, digits, _ and -, like get_opening_hours |
An input_schema without "type": "object" | 400: tools.0.custom.input_schema.type: Field required | Start every schema with "type": "object" |
Forcing the tool with tool_choice={"type": "any"} on Claude Opus 5.5 | 400: tool_choice: type "tool" and "any" are not supported for this model. | Leave tool_choice out and say in the description when to use the tool |
| A one-line description that only names the tool | Claude has less to go on when deciding whether to call it | Say what it does, when to use it and what it returns |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Claude answers in text instead of calling the tool | Claude decided the question didn't need the tool | Say in the description when to use it. Anthropic's docs also suggest a system prompt such as "Use the tools to investigate before responding." |
FAQ
Does Claude run the tool?
No. Claude only asks for it: your code runs the function and sends the result back.
Can a reply hold text as well as the tool call?
Yes. Here the reply held only the tool_use block, but Claude can also write a sentence before it, so look for the block by its type rather than its position.
Can I give Claude several tools?
Yes. tools is a list, and Claude picks the one that fits from each tool's name and description.
Conclusion
A tool is a name, a description and an input schema in tools. Asked about Sunday, Claude stopped with tool_use and asked for get_opening_hours with {'day': 'Sunday'}, ready for your code to run.
Additional Resources
- Tool use with Claude — how tool use works, and what it costs.
- Define tools — tool names, descriptions and schemas.
- Handle tool calls — the
tool_useblock and what to send back.
