0%

A Small Agent · practice

Notice When the Agent Is Stuck

Lesson 5’s trace of the tool-loop run showed the model asking for list_notes with {} three times. The step limit stopped it, but only after two wasted steps, and with max_steps=6 it would have wasted five. The trace already holds what the program needs to notice sooner.

The same question gets the same answer

A tool call is a name and an arguments text. If the model asks for exactly the same name with exactly the same arguments as in an earlier step, the result will be the same as last time, and it is already in the conversation. Running it again brings no new information. A model that does this is going round in circles, and the loop can stop it at once instead of waiting for the step limit.

This is not true of every repeat in every program. A tool that reads the time, or a file that another program changes, could give a new answer to the same call. The course’s tools read fixed notes, so a repeat is always wasted here.

Build repeated_call

repeated_call(calls, trace) compares the calls of the current reply with every call in the trace:

  1. Collect the calls already made. For each entry in trace, and each call in its "tool_calls", append the tuple (call["name"], call["arguments"]) to a list.

  2. For each call in calls, build the same tuple and check whether it is in that list. If one is, return True.

  3. Otherwise return False.

A tuple holds the name and the arguments together, so in compares both at once: ("read_note", '{"note_id": "03-lists"}') matches only the same tool with the same arguments. The same tool with other arguments is a new question, and another tool with the same arguments is too.

The arguments are compared as text. That is exact and simple, and it is what the scripted model sends. A live model could ask for the same thing with the JSON spaced differently, {"note_id":"03-lists"} instead of {"note_id": "03-lists"}, and the texts would not match. Decoding both with json.loads before comparing would catch that. The step limit still catches whatever slips through.

Use it in the loop

In run_agent, call repeated_call(calls, trace) right after reading the calls, before appending the current step’s entry to trace. The current reply must be compared with earlier steps only. If you append first, every call matches itself and the agent is always stuck.

Then, after a reply that asks for tools, return {"answer": "", "stop_reason": "stuck", "trace": trace, "messages": messages} when it repeats a call. Put this check after the budget check and before the step limit. When a model repeats itself on its last allowed step, "stuck" says what went wrong; "max_steps" would only say that it ran out of steps. The repeated calls do not run.

Two identical calls in the same reply, with nothing like them in earlier steps, are not a repeat by this rule: neither call has been answered yet.

Run it

Run python agent.py. The demonstration now sends the errors question through scenario_client("tool-loop") with max_steps=6 and prints the trace:

A model that keeps asking, with max_steps=6:
Step 1 (48 tokens)
  list_notes {} -> 140 characters
Step 2 (83 tokens)
  list_notes {} (not run)
stop_reason: stuck

Two requests instead of six, and 131 tokens instead of what four more growing steps would cost. The earlier tool-loop run in the output, the one with max_steps=3, now stops the same way after two steps.

run_agent now has three guards: a step limit, a budget and a check for repeats. They sit in the loop in the order you added them. The project pulls them out into one function so the order is visible and testable.

Step 1 called read_note with {"note_id": "03-lists"}. Which call in step 2 makes repeated_call return True?

Why must run_agent call repeated_call before it appends the current step's entry to the trace?

On step 2 a live model asks for read_note with {"note_id":"03-lists"}, with no spaces. Step 1 had {"note_id": "03-lists"}. What does repeated_call return, and why?

Task

In agent.py, replace the unfinished body of repeated_call(calls, trace), then use it in run_agent.

  • repeated_call returns True when any call in calls has the same name and the same arguments text as a call in any entry of trace, and False otherwise. Collect (name, arguments) tuples from the trace and check each call with in.

  • In run_agent, call repeated_call(calls, trace) before you append the current step’s entry, so a call is compared only with earlier steps.

  • After a reply that asks for tools, return "stuck" when it repeats a call: after the budget check, before the step limit. Do not run the calls.

Run python agent.py to stop the looping model after two requests, then check your work.