Menu
CoddyTech

Inventory Agent

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

challenge icon

Challenge

Hard

Build an agent that checks and restocks a shop's inventory. The starter holds STOCK and reads the request.

  1. Define two tools. check_stock: description Check how many units of a product are in stock, one required string property product described as Which product. order_more: description Order more of a product from the supplier, the same product property and a required integer property quantity described as How many units to order.
  2. Write the two functions. Both raise ValueError("Unknown product: <product>") for a product not in STOCK. check_stock returns <product>: <count> in stock. order_more raises ValueError("Quantity must be between 1 and 50") unless the quantity is a whole number from 1 to 50; otherwise it adds it to STOCK and returns Ordered <quantity> of <product>, stock is now <count>.
  3. Run the agent loop (model="claude-opus-5-5", max_tokens=500, at most 5 rounds). Print a Tool: line (name, a space, input) for each call. A ValueError goes back as its message with "is_error": True.
  4. At the end, print Agent: and the reply text, then Stock: and every product with its count, separated by a comma and a space.

For example, for the input Order 80 more of the office chair the program prints:

Tool: order_more {'product': 'office chair', 'quantity': 80}
Agent: Sorry, I could not finish: Quantity must be between 1 and 50
Stock: desk lamp 7, office chair 3, standing desk 0

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

STOCK = {"desk lamp": 7, "office chair": 3, "standing desk": 0}

request = input()

# Write your code below

All lessons in Tools and Agents

Practice on your own: Online Python compiler