CH 02 · The Agent Loop · 06/11
Read as one pageReal code: mini-swe-agent, the toy loop finished
You just wrote agent_loop in about twenty lines. mini-swe-agent is that loop in production Python: the agent class fits in default.py. Watch what ends it, because a plain-text reply is not the end.
def run(self, task: str = "", **kwargs) -> dict:
# ... (7 lines: docstring, then a fresh message list with the system prompt and the task)
while True: # [1]
try:
self.step()
self.n_consecutive_format_errors = 0 # reset on any clean step
except FormatError as e: # [2]
# ... (14 lines: charge for the bad reply, count it, give up after too many in a row)
except InterruptAgentFlow as e: # [3]
self.add_messages(*e.messages)
# ... (3 lines: any other crash is recorded as an exit message, then re-raised)
finally:
self.save(self.config.output_path)
if self.messages[-1].get("role") == "exit": # [4]
break
return self.messages[-1].get("extra", {})
def step(self) -> list[dict]:
"""Query the LM, execute actions."""
return self.execute_actions(self.query())
def query(self) -> dict:
"""Query the model and return model messages. Override to add hooks."""
if 0 < self.config.step_limit <= self.n_calls or 0 < self.config.cost_limit <= self.cost: # [5]
raise LimitsExceeded(
# ... (14 lines: build the exit message, then the same check for wall-clock time)
self.n_calls += 1
message = self.model.query(self.messages)
self.cost += message.get("extra", {}).get("cost", 0.0)
self.add_messages(message)
return message
def execute_actions(self, message: dict) -> list[dict]:
"""Execute actions in message, add observation messages, return them."""
outputs = [self.env.execute(action) for action in message.get("extra", {}).get("actions", [])] # [6]
return self.add_messages(*self.model.format_observation_messages(message, outputs, self.get_template_vars()))
Source: SWE-agent/mini-swe-agent, src/minisweagent/agents/default.py (MIT)
What to notice
- [1]
while Truewith no counter. Each lap is onestep(), which is your loop body:query()asks the model,execute_actions()runs what it asked for. - [2] In your toy, a plain-text reply meant "done". Here it is a mistake. The tool-call parser raises
FormatErrorwhen a reply has no tool call, names a tool other thanbash, or has arguments with nocommand. The error text goes back to the model as a user message, and the run gives up only after 3 bad replies in a row (the defaultmax_consecutive_format_errors). - [3]
InterruptAgentFlowis an exception whose only job is to carry messages to append. Task finished, budget spent, time up: each is a subclass, and all go through this one line.finallyalways callssave. That writes every message so far to disk only whenoutput_pathis set. It defaults toNone. A crash still appends an exit message and then re-raises, so a run that was given a path keeps the file. - [4] The loop returns only when the last message has role
"exit". A crash re-raises instead, sorundoes not return. The normal exit is not written by the environment._check_finishedraisesSubmittedwhen the return code is 0 and, after leading whitespace is stripped, the first line isCOMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT. The agent loop appends that message. The rest of the output is the submission. The task instructions (instance_template, sent as the user message) tell the model to finish withecho COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT. - [5] Your
max_turnsbecomes three checks before every model call. The step cap comparesstep_limitwithn_calls, and the time cap compareswall_time_limit_secondswith elapsed time. Both default to0, which turns the check off. The cost cap is on by default:cost_limitis3.0. - [6]
self.envruns each action in order (locally, in Docker, and so on).format_observation_messagesturns each output into a history message.
Read it yourself: open environments/local.py and read _check_finished. Why must the return code be 0, and which part of the output becomes the submission? Then read parse_toolcall_actions in models/utils/actions_toolcall.py to see every way a reply becomes a FormatError.
Finished this idea?
Your learning progress stays in this browser.