Every call to the model sends the conversation again, so a long run gets expensive. To keep calls small, the loop in this lesson sends only the goal and the last two steps: that is what the function recent, already in the editor, does. The price: a fact found at step 1 is out of the model's view by step 4. Run the starting code and you'll see it: the summary no longer knows the customer.
The fix is a scratchpad: notes that stay while the messages come and go. The model can't reach a Python list. It can only ask for tools. So a tool function keeps things for it, like this one that saves a setting in a dictionary:
settings = {}
def set_language(tool_input):
settings["language"] = tool_input["language"]
return {"saved": True}
The function changes something your program keeps, and its result tells the model that it worked. Your scratchpad is two such tools: write_note adds a text to a list, and read_notes returns the whole list. A tool result is a message like any other, so the notes come back into the model's view exactly when it asks for them.
When your code is done, the agent writes down the customer and the status at step 2, reads its notes at step 5, and the summary names Ben Ortiz again:
Step 2
Tool: write_note {'text': 'Order 6: customer Ben Ortiz, status cancelled (not late).'} -> {'saved': 1}
…
Step 5
Tool: read_notes {} -> {'notes': ['Order 6: customer Ben Ortiz, status cancelled (not late).']}
Step 6
Summary: Order 6 isn't late: it was cancelled, so nothing was sent. Tell Ben Ortiz that …
Stopped: the agent called finish
Steps: 6 Cost: 0.0089
Model calls: 7
Messages in the last call: 5
The lookup_order result was still in the last call: no
The note with the customer's name was in the last call: yes
Your task: the agent and the recent function are already in the editor. Add a notes list that is emptied at the start of every run. Write the tools write_note and read_notes, describe them for the model and add them to functions. Then tell the agent in SYSTEM to save what it will need and to read its notes before it finishes.
Assignment
Follow the Coding Tutorial to give the agent a scratchpad: a notes list and the tools write_note and read_notes.
Hint
Both tool functions take tool_input, like the other tools, even though read_notes needs no input. A tool without inputs is described with an empty dictionary {}. The model uses the tools only after SYSTEM tells it why.
Free plan: 30 tutor messages. PRO is unlimited.
