0%

A Small Agent · practice

Stop After a Fixed Number of Steps

Your run_agent stops when a reply asks for nothing. Nothing else stops it. That is fine while the model behaves, and a problem the first time it does not.

A model that keeps asking

The scripted model can misbehave on purpose. scenario_client("tool-loop") from client_calls.py returns a client that sends the header X-Scripted-Scenario: tool-loop with every request. Under that scenario, every reply asks for the same tool call again, however many rounds the conversation already holds. For the errors question it is list_notes with {}, forever. MODEL_SERVICE.md describes it.

A live model is not built to loop, but it can end up doing so: a tool keeps returning an error it does not understand, a result never contains what it hoped for, or it simply loses track. The scenario is a stand-in for those cases, made repeatable so you can test against it.

Send the errors question through that client to your current run_agent and it never returns. Each round adds an assistant message and a list of notes to the conversation, so every request is bigger than the one before, and the loop keeps paying for them until someone stops the program. A while True: loop needs an exit the program controls, one that does not depend on what the model says.

Count the steps

A step is one model call: one request and its reply. The simplest guarantee is a limit on the number of steps.

run_agent gets one more keyword-only parameter, max_steps, after system:

def run_agent(question, *, client, tools, system, max_steps):

Inside it:

  1. Before the loop, start a counter: steps = 0.

  2. After each call to create, add one: steps += 1.

  3. After reading the calls, a reply with no calls still returns "answered" as before.

  4. A reply that asks for tools is checked next. If steps >= max_steps, return {"answer": "", "stop_reason": "max_steps", "messages": messages}.

  5. Otherwise, add the assistant message and the tool results and go round again, as before.

The order in steps 3 and 4 matters. With max_steps=3, a third reply that answers the question is still an answer, so it is returned. Only a reply that wants a fourth step is stopped.

A stopped reply’s calls never run. There is no point: their results would only be useful in a fourth request, which the limit forbids. For the same reason, the stopped reply’s assistant message is not added to messages. The conversation you return ends with the last tool results, exactly as it was last sent, so it never contains a request for tools without their results.

Use >=, not ==. Both work while steps grows by one, but >= still holds if a later change to the loop ever skips a number.

Run it

Run python agent.py. The demonstration now passes max_steps=6 to the errors question, then asks it again through scenario_client("tool-loop") with max_steps=3:

The agent loop:
stop_reason: answered
answer: The errors note says that a finally block always runs, whether or not an exception was raised. An else block, by contrast, runs only when nothing was raised. (07-errors)
The conversation has 7 messages.
A model that keeps asking, with max_steps=3:
stop_reason: max_steps
The conversation has 6 messages.
Run stopped at the next function to build: run_agent gets its max_tokens_total parameter in Lesson 4.

The first run needs three steps and has six, so the limit changes nothing. The second makes three requests and stops. Its six messages are the system message, the question, and two rounds of an assistant message with its tool result; the third reply’s request for list_notes was dropped.

How many steps to allow is a judgment. Too few and a question that needs several lookups gets no answer; too many and a stuck model wastes calls before the limit catches it. The project uses 6 for the study assistant, twice what its questions need.

max_steps is 3, and the third reply answers the question with no tool calls. What does run_agent return?

With max_steps=3, the third reply asks for read_note. What happens to that call?

Task

In agent.py, give run_agent a keyword-only parameter max_steps, after system.

  • Count the steps: one step is one call to create. Start at 0 and add 1 after each call.

  • A reply with no tool calls still returns "answered", as before, even on the last allowed step.

  • After a reply that asks for tools, if the count has reached max_steps, return {"answer": "", "stop_reason": "max_steps", "messages": messages} without running those calls and without adding their assistant message.

  • Otherwise run the calls and go round again, as before.

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