Based Mdels
Blog

Learn · Product

What is an Agent Loop?

Think of a language model as a brain. It has knowledge and, in the case of reasoning models, a thought process. But this brain can't manipulate the environment around it and it has no memory of what happened before.

Here's what that looks like. If I ask the brain, "I'm vegetarian, what should I eat for dinner tonight?", it replies, "eggplant parmesan." Then if I say, "I like that, what restaurant makes a good one in SF?", the brain tells me it doesn't know what "one" refers to. It also has a second problem: even with the proper context, it has no way to search for restaurants in SF that serve eggplant parmesan, or to narrow the results to the ones with good reviews.

So what is an agent loop?

The agent loop is the most basic part of any harness. In fact, the agent loop is itself a harness.

A harness is the code around an LLM that manages tool use, memory, and state. It gives the model the ability to act on the environment around it. A coding agent has read and write tools to change files. A research agent has database query and web search tools to find information.

The agent loop is the innermost loop. It lets the model recall earlier turns of a conversation (its "context"), and it defines the tool spec for the agents built on top of it. In other words, a harness is an agent loop, plus tools, plus a way to evaluate how well the harness performs.

Figure 1. A harness is an agent loop, plus tools, plus evals.

Based Models' Agent Loop

Our Agent Loop is a Python library that manages conversations, tool execution, retries, and limits. You choose the prompts, the tools, the approvals, and the secrets. Agent Loop handles the turn-by-turn work in between, and it is built for teams that need to trust what their agents do:

Here is a user turn and an assistant turn, then a second turn that depends on the first:

import os

from agentloop import Agent, Transcript

transcript = Transcript()

with Agent.from_env(os.environ, tools=[]) as agent:
    transcript.add_user_message("I'm vegetarian, what should I eat for dinner tonight?")
    agent.converse(transcript)

    transcript.add_user_message("I like that, how do I make one?")
    agent.converse(transcript)

for message in transcript.messages:
    text = "".join(part.text for part in message.content if part.type == "text")
    print(f"{message.role}: {text}")

Example output (illustrative; your model's wording will differ):

user: I'm vegetarian, what should I eat for dinner tonight?
assistant: How about eggplant parmesan? It's hearty, vegetarian, and easy to make at home.
user: I like that, how do I make one?
assistant: Slice the eggplant, bread it, and bake it until golden. Layer it with tomato sauce and mozzarella, then bake until bubbly.

You only add the user's messages. Each call to converse() adds the assistant's reply to the transcript, so the loop at the end prints two full turns, in order. The second question never says what "one" is, but the transcript carries the first turn into the second, so the model knows. Give the same agent a search tool, and it can go one step further and find that restaurant in SF. Agent Tools adds provider-hosted web search for exactly this.

Agent Loop is open source (Apache-2.0). To try it yourself, start with Hello World, or read the full Agent Loop docs.