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.
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:
- Any provider, same code. Run against Anthropic, OpenAI, OpenRouter, Modal/vLLM, or your own client without rewriting your agent.
- Your data and secrets stay yours. The library never reads your environment or a
.envfile. You pass in the configuration it uses. - Tools you can control. Local Python tools can require approval, and an approval hook can deny a call before it runs. Independent tool calls run at the same time, with results kept in order.
- Reliable by default. Retries, error types, usage data, and run limits behave the same way across every provider.
- Easy to audit. Conversations are saved as typed, serializable transcripts. Run summaries, event traces, and OpenTelemetry traces and metrics are available, and they record metadata only unless you choose otherwise.
- Testable offline. A scripted
FakeClientlets you check agent behavior without an API key.
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.