Tutorials › Agentic AI › The Agent Loop

Agentic AI · Part 2 of 7

The Agent Loop

The loop as a program: the transcript it carries, how a trip works, and how it ends.

The loop is ordinary program code wrapped around a model. This sketch is illustrative and provider-neutral. Real SDKs differ in names and details, but the shape is the same.

def run_agent(goal, tools, max_steps=10):
    messages = [{"role": "user", "content": goal}]
    for step in range(max_steps):
        reply = call_model(messages, tools)      # one model call, sent the whole transcript
        messages.append(reply.message)           # the model's turn joins the transcript
        if not reply.tool_calls:                 # no tool requested: this is the final answer
            return reply.text
        for call in reply.tool_calls:            # the model may request several tools at once
            try:
                result = tools[call.name](**call.arguments)
            except Exception as err:
                result = "Error: " + str(err)    # the model sees the failure and can adapt
            messages.append(tool_result(call.id, result))
    raise RuntimeError("step limit reached")     # stop a run that never finishes

What happens on each trip

  1. Send the transcript. Each model call carries the conversation so far: the goal, the model's earlier replies, and every tool result. The transcript is the loop's state, and the program holds it between calls.
  2. Read the reply. The model either asks for one or more tools or gives a final answer.
  3. Run the tools and append the results. Each result is tied to the request it answers by an identifier, so the model can match them when it asked for several tools at once.

How the loop ends

A reply with no tool request is the final answer. Provider APIs signal this differently: one documents a loop that continues while the stop reason is tool_use and exits on any other, such as the end of the model's turn, a refusal, or the output token limit being reached. The sketch treats any reply without tool calls as final. A real loop also checks why the reply ended, because a reply cut off by the token limit is not an answer.

A step limit is the other exit. Anthropic's guidance recommends stopping conditions such as a maximum number of iterations to maintain control, and the OpenAI Agents SDK raises an error when a run exceeds its turn limit. Without a limit, a run that never reaches a final answer keeps calling the model.

When a tool fails

In the sketch, a failing tool does not stop the run. Its error text goes back as the tool's result, and the model decides what to do next. Anthropic's tool-use documentation has a field for flagging a result as an error and advises writing instructive messages: say what went wrong and what to try next, for example “Rate limit exceeded. Retry after 60 seconds.”

Who runs the loop

You rarely write the loop by hand. Provider SDKs include runners that execute it, and agent frameworks (see Multiple Agents and Frameworks) wrap it with state handling and routing. Some providers also run the loop on their own side for built-in tools such as web search or code execution, up to an iteration limit, and return the finished result.