Menu
CoddyTech

Defining a Tool

Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 1 of 22.

A model can only write text. To let it look things up or act, you give it tools: functions in your program that it may ask you to run. You describe each tool with a name, a description and an input_schema, a JSON schema of its arguments, and pass the list as tools.

time_tool = {
    "name": "get_time",
    "description": "Get the current local time in a city",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string"}},
        "required": ["city"],
    },
}
response = client.messages.create(model="claude-opus-5-5", max_tokens=300,
    tools=[time_tool], messages=[{"role": "user", "content": "What time is it in Lima?"}])
print(response.stop_reason)
print(response.content[0].name, response.content[0].input)

This prints tool_use, then get_time {'city': 'Lima'}.

The model ran nothing: it answered with a tool_use block naming the tool and the arguments it chose, and the stop reason "tool_use". When the question needs no tool, it answers in text and stops with "end_turn", so check before you read a tool call.

challenge icon

Challenge

Easy

Give the model a weather tool and print what it asks for.

  1. Define a tool named get_weather with the description Get the current weather for a city and one required string property, city.
  2. Send the question with tools=[...], model="claude-opus-5-5" and max_tokens=300.
  3. Print Stop: and the stop reason.
  4. For every block whose type is "tool_use", print Tool: , the tool's name, a space and its input.

For example, for the input What is the weather in Oslo? the program prints:

Stop: tool_use
Tool: get_weather {'city': 'Oslo'}

A question that needs no weather prints only its stop line.

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

question = input()

# 1. Define a tool named "get_weather" with the description "Get the current weather for a city"
#    and one required string property, "city".

# 2. Send the question with tools=[...], model="claude-opus-5-5" and max_tokens=300.

# 3. Print "Stop: " and the stop reason, then "Tool: <name> <input>" for every tool_use block.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tools and Agents

Practice on your own: Online Python compiler