This is the module's project: a returns agent that handles one customer request after another. Almost everything is already in the file: the tools, the plan, the two budgets, the stop rules, and start_return with the shop's rules and the question to the customer.
Three things are missing. A loop that runs the agent for each request, a total cost over all the runs, and a log: a file where every run writes what it did, so you can still answer questions about it next week.
One entry, one line of JSON. A log file is opened in append mode ("a"), which adds at the end and keeps the earlier lines. A site's visit log could look like this after two entries:
{"page": "/products", "user": 7}
{"page": "/cart", "user": 7}
Your agent's log gets a line for the goal and the plan, a line for every tool call (the refused ones too), and a line that says why the run stopped. The finished program prints three traces, and then:
Total cost: 0.0166
Log:
…
{"step": 1, "tool": "start_return", "input": {"order_id": 4}, "ok": false}
{"stopped": "the agent called finish", "steps": 2, "cost": 0.0037}
…
{"step": 1, "tool": "find_orders", "input": {"email": "eva@example.com"}, "ok": true}
{"stopped": "the same call twice in a row: find_orders {\"email\": \"eva@example.com\"}", "steps": 2, "cost": 0.0037}
Your task: run the three requests in a loop, add up their cost, write write_log, call it at the start and the end of each run and for each tool call, and at the end print the total cost, rounded to 4 decimals, and the log file.
Assignment
Follow the Coding Tutorial to run the returns agent on three requests and to give it a total cost and a log.
Hintrun_agent returns a dictionary with "calls" and "cost". In the log, every entry is a dictionary turned into text with json.dumps, followed by a line break.
Free plan: 30 tutor messages. PRO is unlimited.
