Inventory Agent
Part of the Tools and Agents section of Coddy's AI Engineering journey. Lesson 21 of 22.
Challenge
HardBuild an agent that checks and restocks a shop's inventory. The starter holds STOCK and reads the request.
- Define two tools.
check_stock: descriptionCheck how many units of a product are in stock, one required string propertyproductdescribed asWhich product.order_more: descriptionOrder more of a product from the supplier, the sameproductproperty and a required integer propertyquantitydescribed asHow many units to order. - Write the two functions. Both raise
ValueError("Unknown product: <product>")for a product not inSTOCK.check_stockreturns<product>: <count> in stock.order_moreraisesValueError("Quantity must be between 1 and 50")unless the quantity is a whole number from 1 to 50; otherwise it adds it toSTOCKand returnsOrdered <quantity> of <product>, stock is now <count>. - Run the agent loop (
model="claude-opus-5-5",max_tokens=500, at most 5 rounds). Print aTool:line (name, a space, input) for each call. AValueErrorgoes back as its message with"is_error": True. - At the end, print
Agent:and the reply text, thenStock: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 0Try 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