The shop's orders live in shop.db, which only your script can open. Asked "Where is my order 4?", the model can only say it can't see them. You can't send it the database, but you can describe a function your script can run. That description is a tool. The model reads it and, when it needs it, answers with a tool_use block: the name of the tool and the input to call it with. The model never runs your code itself.
A tool is a dictionary with three keys: "name", a "description" of what it does and returns in plain words, and an "input_schema" that lists the values the model must send, with their types. For example, here is a tool that counts the stock of one product by its code:
check_stock_tool = {
"name": "check_stock",
"description": "Count how many of one product are in stock, by its code. Returns a number.",
"input_schema": {
"type": "object",
"properties": {"code": {"type": "string"}},
"required": ["code"],
},
}
You send your tools as a list, with tools=... in client.messages.create. A tool reaches the model only when it is in the request. The reply's content is then a list of blocks. Here the first one holds text, and the second one is the tool call, with its .name and .input.
With the tool sent, your program must print:
I'll look up order 4 for you.
lookup_order
{'order_id': 4}
Then the check shows the tools the model got:
The tools sent to the model: - name: lookup_order description: yes input_schema: object with order_id (integer, required)
Your task: the function lookup_order and the call to the model are given. Describe the function as lookup_order_tool, with an integer order_id that is required. Put the tool in a list tools, send the list in ask_model, and print the name and the input of the tool call.
Assignment
Follow the Coding Tutorial to describe lookup_order as a tool and send it to the model.
Hint
The model's answer doesn't change until tools=tools is in the request. The tool call is the second block of the reply, reply.content[1].
Free plan: 30 tutor messages. PRO is unlimited.
