← Building with AI

What tools are

Describe your functions so the model can ask to use them.

Start this lesson

A model can't look up today's weather or your order history on its own. With tool calling, you describe some of your functions (name, what it does, what inputs it takes). When the model needs one, it doesn't run it itself. It replies with a request like 'please run get_weather with city = Oslo'. Your code runs the real function and sends the result back, and then the model writes its answer.

# To run on your own computer with an API key (it cannot run in this browser).
# First: pip install anthropic, and set the ANTHROPIC_API_KEY environment variable.
import anthropic
client = anthropic.Anthropic()

tools = [{
    "name": "get_weather",
    "description": "Get the current weather for a city.",
    "input_schema": {
        "type": "object",
        "properties": {"city": {"type": "string", "description": "City name, like Oslo"}},
        "required": ["city"],
    },
}]
reply = client.messages.create(model="claude-sonnet-5-5", max_tokens=500, tools=tools,
                               messages=[{"role": "user", "content": "Is it cold in Oslo?"}])
for block in reply.content:
    if block.type == "tool_use":
        print(block.name, block.input)   # get_weather {'city': 'Oslo'}

input_schema describes the inputs using JSON Schema. A clear description helps the model decide when to use the tool.

In these exercises the pretend model returns a dict: either {'type': 'text', 'text': ...} or {'type': 'tool_use', 'id': ..., 'name': ..., 'input': {...}}. That matches the shape of a real tool_use block.

The model only ever ASKS. Your code decides whether to run the tool. That is an important safety point.