Skip to slide
018 / 170

CH 02 · The Agent Loop · 06/11

Read as one page

Real 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. [1] while True with no counter. Each lap is one step(), which is your loop body: query() asks the model, execute_actions() runs what it asked for.
  2. [2] In your toy, a plain-text reply meant "done". Here it is a mistake. The tool-call parser raises FormatError when a reply has no tool call, names a tool other than bash, or has arguments with no command. 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 default max_consecutive_format_errors).
  3. [3] InterruptAgentFlow is 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. finally always calls save. That writes every message so far to disk only when output_path is set. It defaults to None. A crash still appends an exit message and then re-raises, so a run that was given a path keeps the file.
  4. [4] The loop returns only when the last message has role "exit". A crash re-raises instead, so run does not return. The normal exit is not written by the environment. _check_finished raises Submitted when the return code is 0 and, after leading whitespace is stripped, the first line is COMPLETE_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 with echo COMPLETE_TASK_AND_SUBMIT_FINAL_OUTPUT.
  5. [5] Your max_turns becomes three checks before every model call. The step cap compares step_limit with n_calls, and the time cap compares wall_time_limit_seconds with elapsed time. Both default to 0, which turns the check off. The cost cap is on by default: cost_limit is 3.0.
  6. [6] self.env runs each action in order (locally, in Docker, and so on). format_observation_messages turns 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.