# Agent Loop

The cycle at the heart of every agent: receive context, decide an action, execute it, observe the result, repeat until done.

**Leads to:** Agent Communication Protocols, Agent Executors, Agent Instruction Files, Agent Memory Systems, Analyst Agents, Conversation State, Subagents

**Rests on nothing.** A place to start.

*AI Agents · AI Agents*


## Why learn this

You use instructions every day to get things done: you tell a colleague to check a database, you ask a service to print a report, or you follow a checklist to troubleshoot your computer. But those are static commands; they do not change based on what they find. This concept explains how to give instructions that can adapt. When a system encounters an error it didn't expect, or finds new information, it needs to decide its next move in real time. Understanding this loop lets you build systems that don't just follow a script but actually figure out how to solve a problem step by step. For example, if you are building a tool that books flights, you need it to check prices, notice one is too high, search for alternatives, and then book the best one—not because you wrote a line of code for every possible flight combination, but because it can reason through each step.


## The intuition

Imagine you are planning a holiday. You do not sit down and write the entire itinerary in one go. You start with a goal: 'I want to visit Rome.' You look at what you have: dates, budget, preferences for hotels. You decide on your first action: search for flights. You observe the result: several options appear. Based on those results, you decide your next action: filter by price. You observe again: two flights are within budget. You pick one. Now you need a hotel. You decide to search for hotels near the airport. You observe: they are expensive. You adjust your plan: maybe stay slightly further away. You keep going—deciding, acting, observing—until you have all the pieces and can say, 'It is done.' An AI agent works exactly this way. It is not a single question-and-answer chatbot that guesses the whole answer at once. It is a cycle. The agent has a goal. It looks at the current situation (the context). It decides what to do next (such as searching a database, using a calculator, or writing text). It does that thing. It watches the result. That new information becomes part of the context for the next decision. This repeats until the agent decides it has achieved the goal or run out of attempts. The 'decision' part is done by a large language model, which reasons about the current situation just as you reason about your holiday plans. A traditional computer program follows fixed steps written by a programmer; an agent writes its own next step dynamically.


## An analogy

Think of the agent loop as a self-driving car navigating to a destination. You set the goal (the airport) and provide the starting point (the context). The car decides to steer left, then accelerates. It observes the road ahead via sensors. If it sees traffic, it adjusts its speed or changes lanes in the next decision step. It does not calculate every single inch of the journey at the start; it makes small decisions based on what it sees right now, updating its plan continuously until it arrives. Unlike a human driver who might consult a map, the car relies entirely on this real-time sense-decide-act cycle to move forward.


## Definition

The agent loop is the continuous process where a system observes its current context, decides on an action based on that observation, executes the action, observes the new result, and repeats these steps until the goal is reached or the system stops.


## How it connects

This concept sits at the center of 'AI Agents', which are systems that perform tasks rather than just answering questions. It provides the foundational engine for topics like 'Agent Executors', which handle the technical running of this cycle, and 'Agent Memory', which allows the system to retain information across these repeated steps.


## The mathematics

The agent loop itself doesn't require any advanced mathematics — it's a process, not an equation. However, there are two mathematical concepts that are useful to understand, because they describe how the loop behaves. First: the idea of an 'iteration'. When we say the agent loops, we mean it repeats the same set of steps. That's not a mathematical operation in the sense of arithmetic, but it's a process that can be described by a number — the number of steps. Second: a 'geometric series' is a concept from arithmetic that describes a sum where each term is a constant fraction of the previous one. This becomes relevant because, as your library notes say, 'per-step error rates compound' — meaning if an agent makes a mistake with probability, say, 1%, then after many steps the chance of at least one mistake grows. The mathematics of compounding is like interest on a loan: a small error rate at each step, repeated over many steps, produces a much larger overall error rate. That's the same arithmetic as compound interest. So we don't need calculus or algebra to understand that. The branch of mathematics this touches is actually just arithmetic (for counting steps and multiplying probabilities).

But the more important 'mathematical' part is the decision step — where the agent chooses an action. That decision is made by a neural network, which is built out of mathematical operations. But you don't need to understand those in detail to understand the loop. The key is that the decision is a function: it takes the context (which is a list of numbers) and outputs a choice (which is also a list of numbers, converted to a word). That's a concept from algebra — a function maps an input to an output.

The loop itself doesn't have a mathematical formula, but the termination condition can be expressed as: \[ \text{loop until } (\text{goal achieved}) \; \text{OR} \; (\text{step count} = \text{MAX\_STEPS}) \; \text{OR} \; (\text{model decides to stop}) \]

For the error compounding, if each step has an error probability \( p \) and there are \( n \) steps, the probability of at least one error is \( 1 - (1-p)^n \).


## A worked example

```
Let's say an agent's goal is to answer the question: 'What is 15% of 200?' The agent has a tool: a calculator that can multiply and divide. We want to see the loop in action, step by step.

Step 1: Step 1: Context: 'Goal: answer the question. Current data: (nothing yet).'   # The agent starts with just the goal and the prompt.
Step 2: Step 2: Decide: 'I need to calculate 15% of 200. I can use the calculator tool. Action: call calculator(200 * 0.15).'   # The model chooses an action based on its understanding. Here it decides to use a tool.
Step 3: Step 3: Execute: The calculator computes 200 * 0.15 = 30.   # This is the 'act' step — the tool runs.
Step 4: Step 4: Observe: The result '30' is returned to the agent.   # The agent receives the output.
Step 5: Step 5: Decide: 'I have the answer. Action: produce final answer '30'.'   # Now the agent sees that the goal is achieved, so it decides to stop and output the final answer.

Result: After 5 steps (including the final answer), the loop terminates because the agent decides it has achieved the goal. The answer is 30.
```


## From scratch, in Python

```python
import random

# This is a simplified but complete agent loop.
# We'll define a simple goal: find a number that is divisible by 7 and greater than 30.

# The 'context' is a list of messages.
# Each message is a dictionary with two keys: 'role' ('user', 'assistant', 'tool') and 'content' (the text).
context = [
    {"role": "user", "content": "Find a number that is divisible by 7 and greater than 30."}
]

def decide_next_action(context):
    """
    This is where the language model would make a decision.
    For this example, we'll use a simple rule: if we haven't tried a number yet, we'll guess a number.
    If we have a guess, we'll check it.
    """
    # Look at the last message in the context.
    last_message = context[-1]
    # If the last message is from the user, we start by making a guess.
    if last_message['role'] == 'user':
        return "call_tool", "check_number", {"number": 35}
    # If the last message is from the tool, we check the result.
    elif last_message['role'] == 'tool':
        # last_message['content'] is the tool's output, e.g., "True" or "False"
        if last_message['content'] == "False":
            # If the guess was wrong, make a new guess (increment by 7 to stay on multiples of 7)
            return "call_tool", "check_number", {"number": last_message['metadata']['number'] + 7}
        else:
            # If the guess was right, we are done.
            return "final_answer", str(last_message['metadata']['number'])
    else:
        # Fallback
        return "final_answer", "I don't know."

def execute_tool(tool_name, parameters):
    """This simulates a tool call. In reality, it would be a function like a calculator or a search engine."""
    if tool_name == "check_number":
        number = parameters['number']
        result = number % 7 == 0 and number > 30
        return str(result), {"number": number}  # Return the result and any metadata
    else:
        return "Error: unknown tool", {}

def agent_loop(context, max_steps=10):
    """The main loop."""
    step_count = 0
    while True:
        step_count += 1
        print(f"Step {step_count}:")
        
        # 1. Decide what to do
        action_type, action_name, parameters = decide_next_action(context)
        print(f"  Decision: {action_type} - {action_name} {parameters}")
        
        # 2. Execute the action
        if action_type == "call_tool":
            tool_result, metadata = execute_tool(action_name, parameters)
            # 3. Observe the result and add it to the context
            context.append({"role": "assistant", "content": f"I called {action_name} with parameters {parameters}"})
            context.append({"role": "tool", "content": tool_result, "metadata": metadata})
            print(f"  Tool result: {tool_result}")
        elif action_type == "final_answer":
            # We are done
            context.append({"role": "assistant", "content": action_name})
            print(f"  Final answer: {action_name}")
            return action_name
        else:
            raise ValueError("Unknown action type")
        
        # Check termination condition
        if step_count >= max_steps:
            print("Reached max steps, stopping.")
            return "No answer found"
        
# Run the agent
try:
    answer = agent_loop(context, max_steps=5)
    print("\nAgent finished. Answer:", answer)
except Exception as e:
    print("Error:", e)
```


## The same thing, with a framework

In a real framework like LangChain or a custom agent, you'd use a language model instead of the `decide_next_action` function. For example, in LangChain's `AgentExecutor`, the loop is roughly:

```python
from langchain.agents import AgentExecutor
# ...
agent_executor = AgentExecutor(agent=agent, tools=tools, max_iterations=5)
# The agent_executor 'run' method runs the loop internally.
```

Underneath, it does exactly what our code does: it passes the context to the model, gets back a decision (tool call or final answer), executes the tool, appends the result to the context, and repeats. The library just hides that loop.


## What the code does

This code is a complete, runnable agent loop. It's simple because the 'decision' is a set of rules, not a language model, but the structure is identical to a real agent.

- We start with a context, which is a list of dictionaries. Each dictionary represents a message. The 'role' tells us who said it ('user', 'assistant', 'tool'), and 'content' is the text. Our context starts with a single user request.
- The `decide_next_action` function is the brain. It looks at the last message in the context and decides what to do next. It returns a tuple: the type of action (either 'call_tool' or 'final_answer'), the name of the tool (if calling one), and any parameters.
- The `execute_tool` function simulates a tool. It takes a tool name and parameters, does some computation, and returns a string (the result) and a dictionary of metadata.
- The `agent_loop` function is the main loop. It has a step counter that increments each iteration. It calls `decide_next_action`, then executes the action, appends the assistant's and tool's messages to the context, and then decides whether to continue or stop (based on the action type or the step limit).
- The loop continues until the model (here, the rule-based function) decides to return a final answer, or until it hits `max_steps`.

**Key Python concepts used:**
- **List**: an ordered, changeable collection, e.g., `context = [...]`. Here it holds the conversation history.
- **Dictionary**: a collection that maps keys to values, e.g., `{"role": "user", "content": "..."}`. It lets us store structured data.
- **Function**: a named block of code that can be called, e.g., `def decide_next_action(context):`. It lets us reuse logic.
- **While loop**: repeats a block of code as long as a condition is true. Here, it runs until we break out by returning a final answer or hitting the max steps.
- **If/else**: conditionals that branch the code based on a condition, e.g., checking `last_message['role']`.


## Common misconceptions

**An agent is just a chatbot with a long system prompt** — If I just write a detailed prompt, ChatGPT can act like an agent. I don't need a loop.

A chat model without a loop can't call tools or see the results of those tools. It can't actually execute actions on the world — it only generates text. Even if the prompt tells it to 'use a calculator', it cannot actually calculate; it can only guess. The loop is what enables real tool use and observation. In fact: A true agent has a loop: it must have the ability to call tools, receive outputs, and iterate. A long prompt alone doesn't give the model that ability.

**The agent writes the whole plan upfront** — An agent decides all its steps at the start and then executes them.

In a true agent loop, the agent only decides the next action based on the current context. It doesn't see the future. The 'plan' is emergent — it changes as new observations are added. It's like a GPS that recalculates a route when you take a wrong turn, rather than following a static plan. In fact: The agent's decision is myopic: it only determines the next step. The sequence of actions is discovered through the loop, not predetermined.

**The loop is just a programming detail** — The agent loop is just an implementation detail — I don't need to understand it to use agent frameworks.

The loop is the core architecture. How you handle errors, set termination conditions, and manage context directly affects reliability and cost. Frameworks like LangChain just hide the loop, but if you don't understand it, you can't debug or optimize it. When things go wrong, you need to see the loop. In fact: The agent loop is the conceptual heart of an agent. Understanding it lets you build custom agents, fix problems, and know why a framework works the way it does.

**More steps always lead to better answers** — If the agent just loops a few more times, it will have a better chance of getting the right answer.

Each step introduces a chance of error. As your library notes, per-step error rates compound. More steps increase the probability of at least one failure, and they consume resources (money, time, tokens). There is a trade-off between exploration and reliability. In fact: The number of steps is a hyperparameter to tune. You want the minimum number of steps that gives an acceptable answer, not the maximum.


## In short

The agent loop is the cycle of receive context → decide → act → observe → repeat, until a termination condition. It's the engine behind every agent, from ChatGPT plugins to AutoGPT. Termination conditions are as important as the decision step. Per-step errors compound, so keeping loops short and adding verification is key. The loop is not just a mechanism — it's the architecture that gives agents their autonomy and their risk.


## Where this is used

The agent loop is not a concept confined to research papers or exotic frameworks — it's how virtually every modern AI agent works in production.

**In large language models (LLMs) as agents:** The most well-known example is OpenAI's ChatGPT with plugins or the newer 'GPTs'. When you ask it to 'find the cheapest flight to Mumbai next week', it doesn't just generate a text response. Internally, the LLM uses the agent loop: it might call a flight search API, receive the results in JSON, then decide to filter them, then call a map API to check distances, and so on, until it composes a final answer. The LLM's context is the list of messages — including the tool outputs — that it uses to make its next decision. Every time you see a tool being used in an LLM app, that's the loop. The 'code interpreter' feature is also a tool call — the model writes code, executes it, sees the output, and then continues. Without the loop, a chat model is just an answer generator; with the loop, it becomes a problem solver.

**In small language models running on devices:** On a phone or an edge device, the agent loop is often simplified and has harder limits. Because of memory and compute constraints, the context might be truncated to only the last few messages, and the number of steps is often capped very low (even less than 5) to keep latency acceptable. The loop itself is the same, but the termination conditions are more aggressive. For example, in a smart home assistant, the agent might have a two-step loop: it calls a speech recognition tool, then acts on the parsed command, without a lengthy back-and-forth. The error rates per step matter even more here because there's less room for retries.

**In retrieval-augmented generation (RAG):** RAG systems use the agent loop to fetch relevant information. The 'tool' is a document search. The agent first decides to search, observes the top results, and then may decide to search again with different terms, or to answer based on the retrieved text. This is a multi-step loop where the agent judges whether it has enough information. A naive RAG just retrieves once and then answers; an agentic RAG loops, improving the query based on what it sees.

**In agentic systems that plan and call tools:** This is the heart of the matter. A system like AutoGPT or BabyAGI runs a loop where the LLM is given a list of tools (e.g., web search, file editor, calculator) and a high-level goal. The LLM then decides which tool to call, observes the result, and plans the next step. The agent loop is what enables 'planning' — the plan is not pre-written; it's emergent from the cycle. The executor (like our `agent_loop` function) enforces the termination conditions: a maximum number of iterations, a timeout, or a request for human intervention. This is directly from your library's note: 'Beneath every framework sit the same primitives: message loop, tool calls, execution context, memory, planning.'

**In everyday applied machine learning:** Outside of LLM agents, the agent loop appears in reinforcement learning (an agent takes an action in an environment, observes the reward, and learns), and in robotics (perception → planning → action → observation). In classic ML, you might have a multi-armed bandit — which is a single-step loop — but more complex control tasks require the full cycle. So the agent loop is a universal architecture for any system that interacts with a changing world.

**What breaks when done badly:**
- **Per-step errors compound.** Your library says: 'Failure compounds across steps, so per-step error rates matter far more than in single-shot use.' Imagine a 5% error rate per step. Over 10 steps, the chance of at least one failure is not 5% — it's about 40%. This means a tiny error rate in each decision can ruin a long task. Developers must therefore keep loops short or add verification steps.
- **Runaway loops.** Without a maximum iteration cap, an agent can call a tool repeatedly forever, wasting resources and money. A developer must always set a `max_iterations` or a timeout.
- **Context blow-up.** Each tool result is added to the context, and LLMs have finite context windows. Over many steps, the context can exceed the limit, causing the system to fail. Truncation or summarization is needed.
- **Wrong termination condition.** If the agent stops too early, the answer is incomplete; if it stops too late, it wastes resources. Getting the right 'done' condition is hard.


---

Read this on the web: https://basavapurushottam.com/learn-ai/agent-loop
