Skip to main content

How to Give Claude a Tool with the Claude API in Python

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

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​

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>)
PartWhat it means
nameThe tool's name. Letters, digits, _ and - only, up to 128 characters
descriptionWhat the tool does and when Claude should use it. Claude decides from this
input_schemaA 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 .env file as ANTHROPIC_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:

first_tool.py
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 first_tool.py script in a macOS-style code window: a get_opening_hours tool with a description and an input schema that requires a day string, sent with the question Is the café open on Sunday?, then the stop reason, the block types and the tool call are printed.

  • The description says 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_schema says 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​

Terminal
uv run --env-file .env first_tool.py
Output
Stop reason: tool_use
Blocks: ['tool_use']
Tool call: get_opening_hours {'day': 'Sunday'}
Call ID: toolu_01KUApMo49gGPeBiwiUdk9h6

The output in a terminal: the stop reason is tool_use, the reply holds one tool_use block, and the tool call is get_opening_hours with day Sunday, with its call ID.

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​

MistakeWhat happensDo 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 requiredStart every schema with "type": "object"
Forcing the tool with tool_choice={"type": "any"} on Claude Opus 5.5400: 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 toolClaude has less to go on when deciding whether to call itSay what it does, when to use it and what it returns

Troubleshooting​

SymptomLikely causeFix
Claude answers in text instead of calling the toolClaude decided the question didn't need the toolSay 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​