Menu
CoddyTech

Validating Tool Input

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

Tool arguments come from the model, so treat them like any input from outside: check them before the tool runs. A schema says that guests is an integer; it cannot say that a table seats at most 8, or that an order must exist.

def check_booking(guests):
    if not isinstance(guests, int):
        return "guests must be a whole number."
    if guests < 1 or guests > 8:
        return "Tables seat 1 to 8 guests."
    return None

check_booking(12) returns Tables seat 1 to 8 guests. and check_booking(4) returns None.

When a check fails, do not run the tool. Send the message back as a tool_result with "is_error": True, so the model can tell the user or try different arguments. Write the message for the model: say what was wrong and what is allowed. Adding "strict": True to a tool (with "additionalProperties": False in its schema) makes the API guarantee the input matches the schema, which saves the type checks but never your business rules.

challenge icon

Challenge

Medium

The shop's assistant refunds whatever the model asks for. Add checks before refund_order runs.

  1. Fill in check_refund(order_id, amount). In this order, return:
    • Order <order_id> does not exist. when the id is not in ORDERS;
    • The refund amount must be more than 0. when the amount is 0 or less;
    • Order <order_id> only cost <price> dollars. when the amount is more than the order's price in ORDERS;
    • None otherwise.
  2. In the loop, call check_refund with the tool's input. When it returns a message, send that message as the result with "is_error": True and do not refund.

For example, for the input Refund order 1311 for 25 dollars the program prints:

Tool: refund_order {'order_id': 1311, 'amount': 25}
Answer: Sorry, I could not finish: Order 1311 only cost 18.5 dollars.

Try it yourself

import coddy_ai as anthropic  # in production: import anthropic

client = anthropic.Anthropic()

ORDERS = {1182: 64.0, 1205: 120.0, 1311: 18.5}

TOOLS = [
    {
        "name": "refund_order",
        "description": "Refund part or all of an order. Use it when a customer asks for money back on an order.",
        "input_schema": {
            "type": "object",
            "properties": {
                "order_id": {"type": "integer", "description": "The order number"},
                "amount": {"type": "number", "description": "The amount to refund, in dollars"},
            },
            "required": ["order_id", "amount"],
        },
    }
]


def refund_order(order_id, amount):
    return f"Refunded {amount} dollars on order {order_id}."


def check_refund(order_id, amount):
    # Return an error message, or None when the refund may run
    return None


request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            print(f"Tool: {block.name} {block.input}")
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": refund_order(**block.input)})
    messages.append({"role": "user", "content": results})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)

for block in response.content:
    if block.type == "text":
        print("Answer: " + block.text)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Tools and Agents

Practice on your own: Online Python compiler