Start learning free

LESSON 46 OF 129 · AI ENGINEERING: TOOL USE

Ask Before Acting

Practice

The rules inside a tool decide whether an action may happen. They can't tell whether the customer wants it. A model that tries to be helpful can go one step too far: the customer only asks "Can I still return order 7?" and the model starts the return. So before a tool changes anything, your code asks the person.

Confirm, then act: you know this from every "Are you sure?" window. The question names exactly what will happen, and nothing changes without a yes:

answer = input("Delete 3 files? (yes/no) ")
if answer != "yes":
    print("Nothing was deleted.")
else:
    delete_files()

In this lesson a function is ready for you: customer_confirms(question) shows a question to the customer and returns True for yes and False for no. In a real shop it would be two buttons in the chat. Remember that not flips a boolean: not False is True.

Two details matter. The question comes after the rules, so the customer is never asked about a return the shop wouldn't accept. And on a no the tool still answers: it returns an error, so the model learns what happened from the tool result and can tell the customer.

The customer in this lesson says no. Your program must print:

Model: Yes, order 7 can still be returned. I'll start the return now.
Customer is asked: Start the return of order 7?
Customer answers: no
Tool: start_return {'order_id': 7} -> {'error': 'The customer said no. Nothing was changed.'}
Tool results sent in one message: 1
Answer: No problem: I haven't changed anything. Order 7 can still be returned, so tell me when you want me to start.
Model calls: 2
Order 7 in shop.db: shipped

Then the check calls start_return itself. For order 4, which is too old, the customer gets no question at all. For order 7, with a customer who now says yes, the answer is {'id': 7, 'ordered': '2026-05-30', 'status': 'returning'}.

Your task: the script with its tools, its rules and the tool loop is already there. In start_return, below the two rules and above the UPDATE, build the question Start the return of order 7? from the order's id, ask it with customer_confirms, and when the customer does not confirm, return an "error" that says so.


Assignment
Follow the Coding Tutorial to make start_return ask the customer before it changes the order, and change nothing on a no.


Hint
The id is a number, so it needs str() to be joined with text. Take it from order, the row read from the database. The return for a no must sit above the UPDATE: a return ends the function, so the lines below it don't run.


STEP 1 OF 3
    Ctrl + Enter to check