Start learning free

LESSON 45 OF 129 · AI ENGINEERING: TOOL USE

Rules Inside the Tool in Python

Practice

The model decides which tool to ask for. Your code decides what the tool does. A customer can tell the model a story ("your manager said it's fine"), and the model has no way to check it. So the shop's rules don't belong in a prompt. They belong inside the tool, where code checks them on every call.

A rule in code: the tool checks first and changes things only after that. When a rule says no, the tool returns an error that says why, so the model can explain it to the customer. Here is the idea on another tool, one that gives discounts:

def apply_discount(tool_input):
    if tool_input["percent"] > 20:
        return {"error": "The largest discount is 20 percent."}
    # only now change the database

Days between two dates: date.fromisoformat turns text like "2026-06-01" into a date. Subtract one date from another and read .days to get the number of days between them:

start = date.fromisoformat("2026-06-01")
end = date(2026, 6, 10)
print((end - start).days)   # 9

In this lesson the customer wants to return orders 4 and 7 and says a manager allowed the late one. The model believes it and asks for both returns. The today in the file is fixed at June 10, 2026: order 7 is 11 days old and order 4 is 53 days old. With the rules inside start_return, your program must print:

Model: A manager approved the late return, so I'll start both returns.
Tool: start_return {'order_id': 4} -> {'error': 'This order is 53 days old. Returns are accepted for 30 days.'}
Tool: start_return {'order_id': 7} -> {'id': 7, 'ordered': '2026-05-30', 'status': 'returning'}
Tool results sent in one message: 2
Answer: I've started the return of order 7. I couldn't start one for order 4: it was ordered 53 days ago, …

Then the check calls start_return itself and prints what it answers:

order 5, which is paid          →  {'error': 'Only shipped orders can be returned. This order is paid.'}
order 99, which doesn't exist   →  {'error': 'Order not found'}
order 8, shipped 30 days ago    →  {'id': 8, 'ordered': '2026-05-11', 'status': 'returning'}
order 9, shipped 31 days ago    →  {'error': 'This order is 31 days old. Returns are accepted for 30 days.'}

Your task: the script with its three tools and the tool loop is already there. At the top of start_return, read the order with lookup_order and hand back its error when the order doesn't exist. Then add the two rules, each one returning an "error" that explains itself: only shipped orders can be returned, and only for 30 days.


Assignment
Follow the Coding Tutorial to put the shop's two return rules inside start_return, above the UPDATE that changes the order.


Hint
Each rule is an if that returns {"error": ...} before the UPDATE runs, so a refused order never changes. days is a number: it needs str(days) to be joined with text. An order that is exactly 30 days old is still in time.


STEP 1 OF 9
    Ctrl + Enter to check

    Ai Return Rules in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free