Menu
CoddyTech

Recap: Shop Assistant

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

challenge icon

Challenge

Hard

Build the shop assistant's agent loop. The starter defines two tools, the functions behind them in FUNCTIONS, MAX_STEPS and the first message. Both functions raise ValueError for an unknown product, and check_stock raises TimeoutError when the warehouse system is down.

  1. Write run_tool(block): run FUNCTIONS[block.name](**block.input) and return its tool_result. Catch ValueError and TimeoutError and return an error result with the message and "is_error": True. Print Tool: <name> <input> -> <output> for a result, or Tool: <name> <input> -> error: <message> for an error.
  2. Call the model (model="claude-opus-5-5", max_tokens=500, tools=TOOLS) and loop while it asks for tools: append the assistant turn, send all results in one user message, call again.
  3. Stop the loop when it has called the model MAX_STEPS times, and then print Stopped: no answer after 4 steps.
  4. Print Answer: and the final text when there is one, then Steps: and the number of calls.

For example, for the input Check stock for the desk lamp and the price of the desk lamp the program prints:

Tool: check_stock {'product': 'desk lamp'} -> desk lamp: 12 in stock
Tool: get_price {'item': 'desk lamp'} -> desk lamp: 40 dollars
Answer: Here is what I found:
- desk lamp: 12 in stock
- desk lamp: 40 dollars
Steps: 2

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

MAX_STEPS = 4
STOCK = {"desk lamp": 12, "bookshelf": 5, "office chair": 0}
PRICES = {"desk lamp": 40, "bookshelf": 85, "office chair": 65, "garden bench": 150}


def check_stock(product):
    if product == "garden bench":
        raise TimeoutError("The warehouse system is temporarily unavailable, try again")
    if product not in STOCK:
        raise ValueError(f"Unknown product: {product}")
    return f"{product}: {STOCK[product]} in stock"


def get_price(item):
    if item not in PRICES:
        raise ValueError(f"No price for {item}")
    return f"{item}: {PRICES[item]} dollars"


TOOLS = [
    {
        "name": "check_stock",
        "description": "Check how many units of a product are in stock",
        "input_schema": {
            "type": "object",
            "properties": {"product": {"type": "string", "description": "The product name"}},
            "required": ["product"],
        },
    },
    {
        "name": "get_price",
        "description": "Look up the price of an item in dollars",
        "input_schema": {
            "type": "object",
            "properties": {"item": {"type": "string", "description": "The item to price"}},
            "required": ["item"],
        },
    },
]
FUNCTIONS = {"check_stock": check_stock, "get_price": get_price}

request = input()
messages = [{"role": "user", "content": request}]

# Write your code below

All lessons in Tools and Agents

Practice on your own: Online Python compiler